Hopelessone
Hopelessone

Reputation: 337

How to join next line if certain character is there in AWK?

In a text file (windows) i have:

sometexthere"
nothingtodohere
yessomethinghere"
etc

Using AWK (in Ubuntu) how to delete the apostrophe " at the end of the line, replace it with a semi colon : and join the next line?

so it looks like this:

sometexthere:nothingtodohere
yessomethinghere:etc

Upvotes: 3

Views: 85

Answers (1)

Juan Diego Godoy Robles
Juan Diego Godoy Robles

Reputation: 14945

This way:

awk '1' RS='"\n' ORS=':' yourfile

Just set your record sep. to double quotes plus break line, and your output record sep. to the join character.

For DOS line breaks just adjust the regex:

awk '1' RS='"\r\n' ORS=':' yourfile

Note: what that 1 means?

Short answer, It's just a shortcut to avoid using the print statement. In awk when a condition gets matched the default action is to print the input line, example:

$ echo "test" |awk '1'
test

That's because 1 will be always true, so this expression is equivalent to:

$ echo "test"|awk '1==1'
test
$ echo "test"|awk '{if (1==1){print}}'
test

Upvotes: 3

Related Questions