Use sed or gsub command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use sed or gsub command
# 1  
Old 11-06-2008
Use sed or gsub command

Hi there,

I'm trying to write a bash script and I have some difficulties...

I have multiple files, which have the following names:
file_1.txt
file_2.txt
...
file_26.txt


Within each file there is some information, like:
(in the file_1.txt) name of the file: file_name_1_info.hdr
(in the file_2.txt) name of the file: file_name_2_info.hdr
...
(in the file_26.txt) name of the file: file_name_26_info.hdr


I need to replace the information that is included in each file with this:
(in the file_1.txt) name of the file: file_01_tag.hdr
(in the file_2.txt) name of the file: file_02_tag.hdr
...
(in the file_26.txt) name of the file: file_26_tag.hdr


I tried to use either the sed or the gsub command in a for loop, for example something like this:

Code:
for ((i=1;i<=26;i++))
do
sed 's/file_name_${i)_info.hdr/file_${i}_tag.hdr/g' file_${i}.txt > filetag_${i}.txt
done

But unfortunately it does not recognise the variable within the sed command...

Is there any way to do this? Maybe it's easy, but I'm a beginner in bash scripting!

Thanks a lot in advance!

giorgos
# 2  
Old 11-06-2008
This should work:
Code:
#!/bin/sh

count=0

until [ $count = "26" ]; do
        count=$(expr $count + 1)
        sed 's/file_name_${count}_info.hdr/file_${count}_tag.hdr/g' file_${count}.txt > filetag_${count}.txt
done

# 3  
Old 11-06-2008
Use double quotes around the sed code:

Code:
sed "s/file_name_${i)_info.hdr/file_${i}_tag.hdr/g" file_${i}.txt > filetag_${i}.txt

# 4  
Old 11-07-2008
Thanks a lot! I'll test it at work and I'll let you know!!

Thank you both for your response!
# 5  
Old 11-07-2008
It worked perfectly! Thanks a lot! You've saved me a lot of time!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk gsub command to replace multiple spaces

Hi Forum. I'm trying to cleanup the following data elements (To remove any occurences of commas and any extra spaces) while preserving the <TAB> delimiter using awk gsub but I have not been successful. Original Data: 4365 monte des source rue,, ,<TAB>trevost<TAB>QC Desired Data:... (1 Reply)
Discussion started by: pchang
1 Replies

2. Shell Programming and Scripting

Masking with gsub command

My file "test.dat" data as below Requirement is to mask(replace) all english characters with "X" EXCEPT first 7 characters of every line. my command awk '{gsub("]","X")}1' test.dat looks not working properly, Appreciate any suggestion... (6 Replies)
Discussion started by: JSKOBS
6 Replies

3. Shell Programming and Scripting

File creation using awk and gsub command

I have input file 04000912|100:|||||]|101:||]|creDate:1451876825000|1441324800000:]|1444003200000:]|1446595200000:]|1449187200000:]|1451865600000:] I have to get output as below ID|Re_Date|Re_Value|Re_date 04000912|100|40.0|44 04000912|100|50.0|55 04000912|100|60.0|66... (4 Replies)
Discussion started by: shabeena
4 Replies

4. UNIX for Dummies Questions & Answers

Output of sed command to another sed command

Hi All, I'm relatively new to Unix scripting and am trying to get my head around piping. I'm trying to take a header record from one file and prepend it to another file. I've done this by creating several temp files but i'm wondering if there is a cleaner way to do this. I'm thinking... (10 Replies)
Discussion started by: BigCroyd
10 Replies

5. Shell Programming and Scripting

sed and awk giving error ./sample.sh: line 13: sed: command not found

Hi, I am running a script sample.sh in bash environment .In the script i am using sed and awk commands which when executed individually from terminal they are getting executed normally but when i give these sed and awk commands in the script it is giving the below errors :- ./sample.sh: line... (12 Replies)
Discussion started by: satishmallidi
12 Replies

6. Shell Programming and Scripting

awk gsub

Hi, I want to print the first column with original value and without any double quotes The output should look like <original column>|<column without quotes> $ cat a.txt "20121023","19301229712","100397" "20121023","19361629712","100778" "20121030A","19361630412","100838"... (3 Replies)
Discussion started by: ysrini
3 Replies

7. Shell Programming and Scripting

Trouble with passing Variable from bash to awk gsub command

Would really appreciate it if someone could point out my mistake in this line of code, i've been staring blankly at it trying everything i can think of some time now and coming up with nothing. #!/bin/bash echo "Enter Username" read Username awk -F: -v var=${Username} '/^var:/... (9 Replies)
Discussion started by: Nostyx
9 Replies

8. Shell Programming and Scripting

using sed or gsub to substitute characters!

Is there a way to substitute the URL-encoding references of ( & and ` ) with their actual appearance? for example.... %26 is & say I want to convert every %26 in my file to &..... awk '{gsub(/%26/,"&");print}' Is there a way to do this? I also want to be able to convert ` too! (3 Replies)
Discussion started by: puttster
3 Replies

9. Shell Programming and Scripting

Loop with sed command to replace line with sed command in it

Okay, title is kind of confusion, but basically, I have a lot of scripts on a server that I need to replace a ps command, however, the new ps command I'm trying to replace the current one with pipes to sed at one point. So now I am attempting to create another script that replaces that line. ... (1 Reply)
Discussion started by: cbo0485
1 Replies

10. Shell Programming and Scripting

Gsub and nawk

Hello I have problem with reg-expr and function gsub(); File that I want to preprocess look like this: int table ; printf(" variable : ", variable) ; Using nawk I try something like this: for ( .... ) { line = $0 reg_expr = "\.\=]*" "" variable "" "\.\=]*" ; gsub( reg_expr... (1 Reply)
Discussion started by: scotty_123
1 Replies
Login or Register to Ask a Question