verlager
verlager

Reputation: 876

How to replace a string in awk containing a linefeed?

I want to process a text file using awk to replace "\n1-0" with " 1-0".

**Wrong is:

awk '{gsub("\n1-0", " 1-0"); print}' temp.txt >$TARGET

How can this be done? Would sed be a better choice?

Upvotes: 1

Views: 38

Answers (1)

user000001
user000001

Reputation: 33367

You could set the record separator to nothing:

$ cat file
tbname  id  department
xyz 20  cic
1-0 xyz 21  csp
xyz 22  cpz
abc 25  cis
abc 26  cta
abc 27  tec

$ awk -v RS= '{gsub("\n1-0", " 1-0")}1' file
tbname  id  department
xyz 20  cic 1-0 xyz 21  csp
xyz 22  cpz
abc 25  cis
abc 26  cta
abc 27  tec

Upvotes: 2

Related Questions