Rish
Rish

Reputation: 826

add pipe signs around every word in file

I have the following file:

aaaa
bbbb
aaaabbbb

and I wish to have the following ouput:

|aaaa|
|bbbb|
|aaaabbbb|

I tried using:

for i in aaaa bbbb aaaabbbb
do
awk '{ sub ("${i}", "|"${i}"|", $1 }1' input
done

But I do not get the desired output. Any help is appreciated, Thanks.

Upvotes: 0

Views: 55

Answers (2)

Arjun Mathew Dan
Arjun Mathew Dan

Reputation: 5298

With sed:

sed 's/.*/|&|/g' File

.* will match the entire line. & => the pattern that was matched which will be .* (entire line). Add | around the matched pattern.

For inplace substitution: sed -i 's/.*/|&|/g' File

Upvotes: 1

Jotne
Jotne

Reputation: 41460

You can do

awk '{print "|"$0"|"}' file

or

awk '{$0="|"$0"|"}1' file

or as twalberg points out, you can use sed

sed 's/^\|$/|/g' file

This replace start anchor ^ or end anchor $ with |

Upvotes: 2

Related Questions