sed to replace pattern with filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed to replace pattern with filename
# 1  
Old 10-19-2012
sed to replace pattern with filename

Hi all,

I'm trying to replace a pattern/string in about 100 files with the filename using following commands but getting nowhere:

for f in *.fa; do sed "s/^>.*/>$f/g" $f > $f_v1.fa; done

for f in *.fa; do sed 's/^>.*/>`echo $f`/' > $fa_v1.fa; done

Basically I want to change any line that starts with > to >filename.

Thanks for any suggestion.
# 2  
Old 10-19-2012
Code:
 
for f in *.fa; do sed 's/^>.*/>'$f'/' $f > ${f}_v1.fa; done

# 3  
Old 10-19-2012
Quote:
Originally Posted by rdrtx1
Code:
 
for f in *.fa; do sed 's/^>.*/>'$f'/' $f > ${f}_v1.fa; done

This solves my problem. Thank you.

By the way can you explain the single quotation '$f'? I have tried with double quotation and it simply replaced the line with "$f". And why the curvy bracket at the end? Apology if the answers are very obvious. I'm actually a biologist. Thank you.
# 4  
Old 10-19-2012
To single quotes gets the shell variable $f as part of the sed script between the first and last quote.
# 5  
Old 10-19-2012
Curvy bracket is used to distinguish the actual variable name. Without curvy bracket is will consider the whole as variable name: $f_v1.fa; but with curvy bracket it will consider only $f as variable name: ${f}_v1.fa;
# 6  
Old 10-19-2012
Quote:
Originally Posted by bipinajith
Curvy bracket is used to distinguish the actual variable name. Without curvy bracket is will consider the whole as variable name: $f_v1.fa; but with curvy bracket it will consider only $f as variable name: ${f}_v1.fa;
To be precise, without the braces, the variable name ends with the character before the dot, ${f_v1}.fa, since the dot is not allowed in a variable name [1].

Regards,
Alister

1. ksh does allow dots in a variable name, but those names MUST appear within braces
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed replace pattern

I have a file with multiple lines, all in the same format. For each line, I need to replace the sequence of digits after the last : with a new value, but keep the single quote at the end of the line. Example: Input: ( two lines of file) Name: 'text1:200/text2:1.2.3.4' Name2:... (19 Replies)
Discussion started by: Beginner101
19 Replies

2. Shell Programming and Scripting

sed command to replace two character pattern with another pattern

Not able to paste my content. Please see the attachment :-( (2 Replies)
Discussion started by: vivek d r
2 Replies

3. UNIX for Dummies Questions & Answers

Sed- Replace space in filename by a \

`echo $file | sed 's/ / /g'` Hey guys I want help in converting the spaces in my file names to '\ ' . Example: UK maps --> UK\ maps Could someone please help me. I have tried the following sequences already (none of them work): 1)s/ /\ /g 2)s/ /\\ /g 3)s/ /\\\ /g Can someone... (7 Replies)
Discussion started by: INNSAV1
7 Replies

4. Shell Programming and Scripting

Replace everything but pattern in a line using sed

I have a file with multiple lines like this: <junk><PATTERN><junk><PATTERN><junk> <junk><PATTERN><junk><PATTERN><junk><PATTERN><junk> Note that 1. There might be variable number occurrences of PATTERN in a line. 2. <> are just placeholders, they do not form part of the pattern. I need... (4 Replies)
Discussion started by: flatley
4 Replies

5. Shell Programming and Scripting

Pattern Replace using sed or awk

Hi , My file have data like 4:ALMOST NEVER PR 1925836 5:NEVER PR W DDA 5857610 6:NEVER PR WO DDA 26770205 but i want to replace the spaces before last numric digits out put should be like this 4:ALMOST NEVER PR=1925836 5:NEVER PR W DDA=5857610 6:NEVER PR WO... (7 Replies)
Discussion started by: max_hammer
7 Replies

6. Shell Programming and Scripting

How to replace the last pattern using sed?

myfile: AAAaaa BBBbbb CCCccc AAAeee DDDddd how to replace the last AAA as EEEEE using sed? like this: AAAaaa BBBbbb CCCccc EEEEEeee DDDddd (14 Replies)
Discussion started by: vistastar
14 Replies

7. Shell Programming and Scripting

Sed Replace repeating pattern

Hi, I have an sqlplus output file using the character ';' as a delimiter and I would like to replace the fields without datas (i.e delimited by ';;') by ';0;' Example: my sqlplus output: 11;22;33;44;;;77;; What I would like to have: 11;22;33;44;0;0;77;0; Thanks in advance for your... (2 Replies)
Discussion started by: popesk
2 Replies

8. Shell Programming and Scripting

Replace a filename with full path using sed

hi, i need to replace a line a file with a new raw device location.. original file.. /opt/sybase/ASE1502/ASE-15_0/bin/dataserver \ -d/data/TST_AKS1/sybdevices/master.dat \ -e/logs/sybase/TST_AKS1/SFO_TST_AKS1.log \ -c/apps/sybase/ASE1502/ASE-15_0/TST_AKS1.cfg \... (2 Replies)
Discussion started by: aksaravanan
2 Replies

9. Shell Programming and Scripting

SED Search Pattern and Replace with the Pattern

Hello All, I have a string "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031", and I just want to extract LLSV1, but I dont get the expected result when using the sed command below. # echo "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031" | awk '{print... (4 Replies)
Discussion started by: racbern
4 Replies

10. Shell Programming and Scripting

need help on sed (replace string without changing filename)

I have awhole bunch of files and I want to edit stringA with stringB without changing the filename. I have tried the following sed commands: sed "s/stringA/stringB/g" * This will print the correct results but does not actually save it with the new content of the file. when I do a cat on... (5 Replies)
Discussion started by: jjoves
5 Replies
Login or Register to Ask a Question