Bash how to append word to end of a line?
Solution 1
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')
Solution 2
How about just using awk
:
awk -F= '/address/{gsub(/:/," ");print $2,"eth0"}' file
Demo:
$ cat file
junk line
address=192.168.0.12:80
address=127.0.0.1:25
don not match this line
$ awk -F= '/address/{gsub(/:/," ");print $2,"eth0"}' file
192.168.0.12 80 eth0
127.0.0.1 25 eth0
Or just with sed
:
$ sed -n '/address/{s/:/ /g;s/.*=//;s/$/ eth0/p}' file
192.168.0.12 80 eth0
127.0.0.1 80 eth0
Solution 3
All you need is:
awk -F'[=:]' '{print $2, $3, "eth0"}' file.txt |
while IFS= read -r ip port eth
do
printf "ip=%s, port=%s, eth=%s\n" "$ip" "$port" "$eth"
done
Always use IFS= and -r when using read unless you have a very specific reason not to. google for why.
Solution 4
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
Palace Chan
Updated on July 05, 2022Comments
-
Palace Chan almost 2 years
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?
-
Palace Chan about 11 yearsCool, and how do I loop over the results in the end? The spacing causes me to loop over each word and not each line!
-
Chris Seymour about 11 years
Bash
,grep
,cut
,tr
andsed
no no no... What you want can be achieved with eithersed
orawk
alone. -
FatalError about 11 years@sudo_O While I share your sentiment to an extent, this really isn't that bad :P. It's in the unix bloodline to chain together a ton of small utilities like that, that's why they exist.
-
Palace Chan about 11 yearsThat's awesome, I always forget the
<(
stuff and was doing$(
-
Ed Morton about 11 years@FatalError UNIX is not about chaining together a ton of small utilities when one small utility will do the job.
-
Calvin Taylor about 5 yearsI see that this doesn't call anything per line as indicated, but this answers the title question.
-
Vishnu Kumar about 3 yearswhat does & do here?
-
Calvin Taylor about 3 yearsit's basically shorthand for everything from the LHS into the replacement RHS. take another look at the example or try it for yourself. =) It's great for pre/post pending.