Removing characters from beginning of multiple files

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Removing characters from beginning of multiple files
# 1  
Old 01-09-2017
Removing characters from beginning of multiple files

Hi,

I have been searching how to do this but I can't seem to find how to do it. Hopefully someone can help.

I have multiplr files, 100's example 12345-zxys.213423.zyz.txt. I want to be able to take all these files and remove the first '12345-' from each of the files. '12345-' these characters are uniqe on each file but of the same length.

I would appreciate if someone can help.

Thanks in advance.

Regards
Israr
# 2  
Old 01-09-2017
Please post what have you tried, where are you struck.

Here is a sample.

Code:
ls 12345-zxys.213423.zyz.txt | while read file
do
newfilename=`echo $file | sed 's/^.*-//'`
mv $file $newfilename
done

This User Gave Thanks to matrixmadhan For This Post:
# 3  
Old 01-09-2017
hi matrixmadhan,

Thank you so much for quick response. If I run this will it only do fir single file or will it for the batch?
# 4  
Old 01-09-2017
I have added sample script for a single file, please change it to multiple files like

Code:
ls file*

Please give it a try and let us know if there are any problems.
This User Gave Thanks to matrixmadhan For This Post:
# 5  
Old 01-09-2017
Yes thats worked exactly as I wanted. so I replaced ls file* with ls *.txt. For benefit of many who have not used sed command and learning. can you please break the command down?

Many thx
# 6  
Old 01-09-2017
You might want to consider shell's parameter expansion (no external cammnd like sed necessary):
Code:
for FN in *.txt; do echo mv $FN ${FN#*-}; done

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Selecting text on multiple lines, then removing a beginning and end patterns

I have a file similar to the below. I am selecting only the paragraphs with @inlineifset. I am using the following command sed '/@inlineifset/,/^ *$/!d; s/@inlineifset{mrg, @btpar{@//' $flnm >> $ofln This produces @section Correlations between seismograms,,,,}} ... (5 Replies)
Discussion started by: Danette
5 Replies

2. Shell Programming and Scripting

Removing a ? from multiple files

Hi all, I have about 1.8 million files in a directory structre, that contain a ? on the end, for example: /testdocs/1/mar/08/08/images/user/{1234-1234-1234-1234}0? Is there a way to go through the testdocs folder, recursively, and remove the ? from all docs that have one on the end? ... (11 Replies)
Discussion started by: tirmUK
11 Replies

3. Shell Programming and Scripting

Removing carriage returns from multiple lines in multiple files of different number of columns

Hello Gurus, I have a multiple pipe separated files which have records going over multiple Lines. End of line separator is \n and records going over multiple lines have <CR> as separator. below is example from one file. 1|ABC DEF|100|10 2|PQ RS T|200|20 3| UVWXYZ|300|30 4| GHIJKL|400|40... (7 Replies)
Discussion started by: dJHa
7 Replies

4. UNIX for Dummies Questions & Answers

Renaming Multiple Files by removing characters

Hi I would like to rename Multiple files in a Unix Directory using Ksh Command. Eg ATT8-2011-10-01 00:00:00-MSA-IMM-SINGLE_AND_FAMILY_COVERAGE-DED-$2000-X114817.PDF needs to be renamed as ATT8-2011-10-01-MSA-IMM-SINGLE_AND_FAMILY_COVERAGE-DED-$2000-X114817.PDF Basically the time... (2 Replies)
Discussion started by: pchegoor
2 Replies

5. Shell Programming and Scripting

Removing hyphen from beginning and end of a word only.

It is very simple to remove a hyphen from a word anywhere in that word using a simple sed command (sed -i 's/-//g' filename), but I am not able to figure out how to do this: For example, apple -orange tree pipe- banana-shake dupe- What my output should look like: apple orange tree... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

6. Shell Programming and Scripting

Append Multiple files with file name in the beginning of line

Hi, I have multiple files having many lines like as bvelow: file Name a.txt abc def def xyz 123 5678 file Name b.txt abc def def xyz 123 5678 I would like to append files in the below format to a new file: file Name c.txt (7 Replies)
Discussion started by: rramkrishnas
7 Replies

7. Shell Programming and Scripting

Removing one or more blank characters from beginning of a line

Hi, I was trying to remove the blank from beginning of a line. when I try: sed 's/^ +//' filename it does not work but when I try sed 's/^ *//' filename it works But I think the first command should have also replaced any line with one or more blanks. Kindly help me in understanding... (5 Replies)
Discussion started by: babom
5 Replies

8. UNIX for Dummies Questions & Answers

Removing Lines Shared by Multiple Files

Hey everyone, I have a question about comparing two files. I have two lists of files. The first list, todo.csv, lists a series of compounds my supervisor wants me to perform calculations on. The second list, done.csv, lists a series of compounds that I have already performed calculations on.... (2 Replies)
Discussion started by: Stuart Ness
2 Replies

9. Shell Programming and Scripting

Removing from beginning to first : using awk

A have a file npt02-sr40-syn-dc0p014-32x24drw.log:0. Best Value = 0.00144914 npt02-sr40-syn-dc0p014-32x24drw.log:1. Best Value = 0.00115706 npt02-sr40-syn-dc0p014-32x24drw.log:2. Best Value = 0.00094345 npt02-sr40-syn-dc0p014-32x24drw.log:3. Best Value = 0.000925552... (11 Replies)
Discussion started by: kristinu
11 Replies

10. Shell Programming and Scripting

Removing M^ from multiple files

to do this i usually type dos2unix <file> -o <file> and this will remove the M^ from the end of each file. well i have over 100 files that someone copied that i need. how do i remove the M^. i saw a perl script but i am not familiar with .pl at all really (7 Replies)
Discussion started by: deaconf19
7 Replies
Login or Register to Ask a Question