Awadhesh Kumar
Awadhesh Kumar

Reputation: 11

How to append a character at the end of each word in a file using awk or sed?

Hi I have a file in UNIX with data as below:

cat file.txt

obj1,obj2,obj3\
obj3,obj2,obj4,obj1\
obj1\
obj3,obj4

I want each words to replaced by _N and I want to be output as below:

obj1_N,obj2_N,obj3_N\
obj3_N,obj2_N,obj4_N,obj1_N\
obj1_N\
obj3_N,obj4_N

Upvotes: 1

Views: 895

Answers (4)

Carlos Pascual
Carlos Pascual

Reputation: 1126

With GNU awk you can get the output you write.

awk '{print gensub(/([0-9])/, "\\1_N", "g", $0)}' file

obj1_N,obj2_N,obj3_N\
obj3_N,obj2_N,obj4_N,obj1_N\
obj1_N\
obj3_N,obj4_N

This code:

  • gensub function here with g for global search and replace.
  • regexp with the parentheses ([0-9]) for retrieving the digit with \\1 in the substitution part.
  • the end _N you want here at the right of \\1 without space between "\\1_N"
  • the focus is in $0

The GNU awk manual:

gensub provides [...] the ability to specify components of a regexp in the replacement text. This is done by using parentheses in the regexp to mark the components, and then specifying `\n' in the replacement text, where n is a digit from one to nine.

  • The same result if you declare in an initial -v the '_N' and then its name with \\1
awk -v end="_N" '{print gensub(/([0-9])/, "\\1"end, "g", $0)}' file

obj1_N,obj2_N,obj3_N\
obj3_N,obj2_N,obj4_N,obj1_N\
obj1_N\
obj3_N,obj4_N

Upvotes: 0

thanasisp
thanasisp

Reputation: 5975

With sed

sed -r 's/[A-Za-z0-9_]+/&_N/g' file

In the above command, the word is defined as any sequence of letters, numbers and underscores. You can add or remove any characters into there to customize your own word.

Upvotes: 1

Awadhesh Kumar
Awadhesh Kumar

Reputation: 11

sed 's/,/_N/g' file.txt | sed 's/$/_N/'

First sed command replaces comma to _N and second sed command replace end of line to _N

Upvotes: 0

kraftwerk28
kraftwerk28

Reputation: 36

cat file.txt | sed 's/\(\w\)\(\W\|$\)/\1_N\2/g'

The (\W|$) means "Match non-word or end-of-string

Upvotes: 0

Related Questions