sed append words

Hi all,

I have a file like

one   two  three for
five   six    seven eight
.....

Actually i need to append a label to the words that belong to the 2 column and get:

one   two_label  three for
five   six_label    seven eight
....

I was trying with sed inside vim but I can't figure out a way.
Any help please?
Thx in advance

$ ruby -ane '$F[1]=$F[1]+"_label";puts $F.join(" ")' file
$ awk '{$2=$2"_label"}1' file
1 Like
sed "s/  *[^ ]*/&_label/" file
1 Like

Thx a lot both

Why is the output not like that:

five   six_label    seven_label eight_label

Wh is _label appended only with the word "six" ?
If the output is like this:

five   six_label    seven_label eight_label

then where should the sed command be changed?

sed "s/  *[^ ]*/&_label/" file

doesn't have a global flag, therefore it matches only the first instance in each line.

sed "s/  *[^ ]*/&_label/g" file

Will just do what you originally thought

1 Like
awk '$2=$2 "_label" ' infile
sed 's|[^ ][^ ]*|&_label|2' infile
1 Like

Nice.

# cat file
onene   two  three for
five   six    seven eight
# sed 's/  *\(*.\)*  */\1_label&/2' file
onene   two_label  three for
five   six_label    seven eight

Regards
ygemici