Rename contents inside multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename contents inside multiple files
# 1  
Old 02-16-2010
Rename contents inside multiple files

Hi,
I have 100 files in a directory.

Each file have the following format

Code:
>CtbRe01234
  fdfjdhfkdfkd
>CtL2B0456
  gjfgfkgjfkgjfk
>CmdrE05768
   fghdjskksllfk

Like this I have many files in the directory.

What I want is; rename the header content in each file such that the above file will look below;

Code:
>CtbRe
  fdfjdhfkdfkd
>CtL2B
  gjfgfkgjfkgjfk
>CmdrE
   fghdjskksllfk

That is, rename >CtbRe01234 to >CtbRe or in other words, retain only the first 5 character (excluding >) as headers of the content in each file.

Remember, this 5 character header may include a digit in some cases like in >CtL2B.

Please let me know the best way in awk or using regular expressions to edit the header content in each of the files in a directory.

LA
# 2  
Old 02-16-2010
Code:
awk '/^>/{$0=substr($0,1,6)}1'

# 3  
Old 02-16-2010
Thanks,
Does this can be applied to the whole directory. Let me know
# 4  
Old 02-16-2010
Sorry, I forgot explain XD.

/^>/{$0=substr($0,1,6)}
If a line starts with ">" then cuts the line to 6 chars
1
For all the lines execute the dafault action, this is print $0
# 5  
Old 02-16-2010
Code:
$ cat data
>CtbRe01234
  fdfjdhfkdfkd
>CtL2B0456
  gjfgfkgjfkgjfk
>CmdrE05768
   fghdjskksllfk

sed 's/^\(>.....\).*$/\1/' data
>CtbRe
  fdfjdhfkdfkd
>CtL2B
  gjfgfkgjfkgjfk
>CmdrE
   fghdjskksllfk

# 6  
Old 02-16-2010
Sure Lucky Ali, you can apply to multiple files just need a FOR

Code:
for curFile in `ls test*txt` ; do 
	cat $curFile | awk '/^>/{$0=substr($0,1,6)}1' > temp.tmp
	mv temp.tmp $curFile
done



---------- Post updated at 10:47 AM ---------- Previous update was at 10:44 AM ----------

Good sed alister, you can even manke it a little bit shorter

Code:
sed 's/^>.....//' data

and with -i you can work in the same file so you won't need a temp file like me

I like it

Code:
for curFile in `ls test*txt` ; do 
	sed -i 's/^>.....//' $curFile
done

# 7  
Old 02-16-2010
Quote:
Originally Posted by kcoder24
Good sed alister, you can even manke it a little bit shorter

Code:
sed 's/^>.....//' data

and with -i you can work in the same file so you won't need a temp file like me

I like it
Nope. You can't do that. That will delete the 6 characters you want to keep. Regarding the -i option, that's fine but it's non-standard. If portability to other systems is ever a concern, stay away from -i.

Regards,
alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename multiple files in one go

OS : Oracle Linux 6.8 shell : bash As shown below, I have multiple files like below (query1-extract_aa, query1-extract_ab, query1-extract_ac, ....) $ ls -l total 235680 -rw-rw-r-- 1 reportusr reportusr 30M May 3 11:25 query1-extract_aa -rw-rw-r-- 1 reportusr reportusr 30M May 3 11:25... (5 Replies)
Discussion started by: kraljic
5 Replies

2. Shell Programming and Scripting

SBATCH trinity for multiple files and rename/move the output files

Hey guys, I have wrote the following script to apply a module named "trinity" on my files. (it takes two input files and spit a trinity.fasta as output) #!/bin/bash -l #SBATCH -p node #SBATCH -A <projectID> #SBATCH -n 16 #SBATCH -t 7-00:00:00 #SBATCH --mem=128GB #SBATCH --mail-type=ALL... (1 Reply)
Discussion started by: @man
1 Replies

3. Shell Programming and Scripting

For loop inside awk to read and print contents of files

Hello, I have a set of files Xfile0001 - Xfile0021, and the content of this files (one at a time) needs to be printed between some line (lines start with word "Generated") that I am extracting from another file called file7.txt and all the output goes into output.txt. First I tried creating a for... (5 Replies)
Discussion started by: jaldo0805
5 Replies

4. Shell Programming and Scripting

Rename multiple files

Hi, In my directory I have many files, for e.g. file_123 file_124 file_125 file_126 file_127 Instead of renaming these files one by one, I would like to rename them at a same time using same command... they should appear like 123 124 125 126 127 What command(awk or ls or... (3 Replies)
Discussion started by: juzz4fun
3 Replies

5. Shell Programming and Scripting

Rename files that are inside zip file

Hello to all, I have a zip file with any name like FileName.zip, within the zip file there are more than 30 files with different extensions in the following format. FileName_BMN_ROSJ.txt FileName_THEUS.jpg . . . FileName_KWPWP.shx I would like to unzip the file and rename each file... (2 Replies)
Discussion started by: Ophiuchus
2 Replies

6. Shell Programming and Scripting

Rename the multiple files

Hi I need to reanme the multiple file using unix script I have multiple file like: sample_YYYYMMDD.xls test new_YYYYMMDD.xls simple_YYYYMMDD.xls I need to rename this file sample.xls testnew.xls SIMPLE.xls thanks (8 Replies)
Discussion started by: murari83.ds
8 Replies

7. UNIX for Dummies Questions & Answers

help with multiple files rename...

Hi everyone, I'm very green in Linux. Please help me to solve my problem. I have thousands of files and I want to change their names. They have naming convection: prefix_date_date+1_suffix.nc prefix: ext-GLORY date_date+1: 20020101_20020102 and two types of suffix: gridV_R20020130 and... (3 Replies)
Discussion started by: makikicindy
3 Replies

8. Shell Programming and Scripting

now to rename multiple files

I have several hundred files in one directory which I need to move to another directory with the new extension, for example: /bb/data/rptmgr* are in the source directory need to be moved to /bb/data55/rptmgr*.new Is there an efficient way to do it? Thanks -A (4 Replies)
Discussion started by: aoussenko
4 Replies

9. Shell Programming and Scripting

rename multiple files

Hi, can anyone have a ksh script to rename multiple files (ie to remove .Z extension of the files) can someone correct this? for i in *.Z do var1 = substr($i, 1,at(".Z",$i)-1) mv $i $var1 done Thanks.. Antony (13 Replies)
Discussion started by: antointoronto
13 Replies

10. UNIX for Dummies Questions & Answers

Rename multiple files

Hello, I want to rename multiple files at a time and I don't know how to do it. I have various ".mp3" files, like "band name - music name.mp3" and I want to remove the "band name" from all files. Anybody knows how to do it using shell script or sed or even perl? Thanks (7 Replies)
Discussion started by: luiz_fer10
7 Replies
Login or Register to Ask a Question