I have executed a command in bash to retrieve some addresses from a file like this:
grep address file.txt | cut -d'=' -f2 | tr ':' ' '
yields:
xxx.xx.xx.xxx port1
xxx.xx.xx.xxx port2
and I would like to append ' eth0' to each of those output lines and then ideally for loop over the result to call a command with each line. Problem I'm having is getting that extra string in the end to each line. I tried:
| sed -e 's/\(.+)\n/\1 eth0/g'
which didn't work..and then supposing I got it there, if I wrap it in a for loop it won't pass in the full lines since they contain spaces. So how do I go about this?
Using '>>' with 'echo' command appends a line to a file. Another way is to use 'echo,' pipe(|), and 'tee' commands to add content to a file.
5. Which command appends text at the end of the current line? Explanation: To append text at end of current line use 'A' command.
I came here looking for the same answer, but none of the above do it as clean as
sed -i 's/address=.*/& eth0/g' file
Search and replace inline with sed for lines begining with address, replace with the same line plus 'eth0'
eg.
sed -i 's/address=.*/& eth0/g' file; cat file
junk line
address=192.168.0.12:80 eth0
address=127.0.0.1:25 eth0
don not match this line
You can match $
to append to a line, like:
sed -e 's/$/ eth0/'
EDIT:
To loop over the lines, I'd suggest using a while
loop, like:
while read line
do
# Do your thing with $line
done < <(grep address file.txt | cut -d'=' -f2 | tr ':' ' ' | sed -e 's/$/ eth0')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With