Call each line in a file as an argument to another command in Bash
Hi All,
I am new to bash script so please done mind if my question is trivial. I am trying to build a script where I need to call each line of a file , use it as input to sed and generate output. This should follow for rest of the lines as well. I was using the below one but doesnt seems to be working. Could someone give me a hand in debugging this or provide anyother solution ? old="hi" new="bye" while I= read -r a; do sed -i -e "/^$a/s/$old/$new/" /tmp/test.txt done </tmp/tacfile.txt |
Not sure what the I= is for, but you'll need a space after /^$a/, before s/$old/$new/
[edit] No it seems to work as this (without the space) It won't generate output with the -i option (edit file in place) |
Hi!
Please use code tags when posting code, it makes for much easier reading. It seems to me that you are complicating things. Given this infile: Code:
hi foo Code:
#!/bin/bash Code:
sed 's/hi/bye/g' in.txt HMW |
Quote:
And as shown by HMW, provide some input, actual output and expected output, plus any error messages if any. Also, in future, please use [code][/code] tags around code / data so it is more legible and maintains formatting. |
Quote:
I mean you created an unnecessary loop, therefore you have now loop (of sed) inside loop (of while). That is very, very ineffective, this is something called "bad practice". A much better solution would be (yes, I know you know it, so just to repeat): Code:
sed "s/$old/$new/" in.txt |
Quote:
Quote:
|
Not sure I follow well but
Code:
sed "s/$old/$new/" in.txt Code:
sed "/^$a/s/$old/$new/" in.txt |
Guys Thanks a lot for the response. I should have been more clear in my point. Sorry about that.
The reason why I use sed is not to change the sting directly. Example. In file test.txt, hi My name is Pradeep bye My name is Pradeep sed -i -e "/^hi/s/Pradeep/spa/" So wherever i find "hi" as first occurance of a line, I need to change "Pradeep" to "Spa". Here "Hi" is not static where I have list of variables which i need to call from a file /tmp/tacfile.txt. |
So your script works :)
|
Not exactly. For some reason its changing all instance regardless of my condition (hi or bye)
|
could someone know why its not filtering properly. Usage of sed is not proper or anything? I ran manually where it works. Only as a whole file its not
|
Maybe remove -i option and redirect output to a temp file then move temp file to test.txt
edit: wrong approach, I was thinking of capturing output from loop but the test.txt content will repeat after each loop iteration. I would be curious to see test.txt and tacfile.txt content though Another idea would be to build match pattern from tacfile.txt something like: ^something|^thing|^this|^that Code:
old="hi" |
changed the condition to " sed -i -e "/$a/ s/$old/$new/ ; /$a/! s/$old/$old/" which did the work.
Thanks all for helping |
Well that makes little sense as to why you would need to go that way. Are you sure the file(s) being edited aren't originally Windows files? The dodgy line endings could be messing up sed.
|
They are not windows file. Just a text file on linux.
|
All times are GMT -5. The time now is 02:54 PM. |