Home > Enterprise >  awk command: if line doesn't starts with a character remove new line on before line
awk command: if line doesn't starts with a character remove new line on before line

Time:12-20

Trying to use awk command to implement this rule: if line doesn't starts with "O|" or "A|" or "S|" I want to remove new line on before line

I have this file in input (input.txt)

O|field1|field2
O|field1|
field2
A|field1|
field2
S|field1|
field2
O|field1|field2
O|field1|field2
O|field1|
field2
A|field1|
field2
S|field1|
field2
O|field1|field2

I want this output

O|field1|field2
O|field1|field2
A|field1|field2
S|field1|field2
O|field1|field2
O|field1|field2
O|field1|field2
A|field1|field2
S|field1|field2
O|field1|field2

Executing this code

awk '/^O\|/ || /^A\|/ || /^S\|/ {printf "%s", $0; next} 1 {print}' input.txt > output.txt

It returns

O|field1|field2O|field1|field2
A|field1|field2
S|field1|field2
O|field1|field2O|field1|field2O|field1|field2
A|field1|field2
S|field1|field2
O|field1|field2

Somebody can help me please?

CodePudding user response:

This awk should work for you:

awk -F'|' 'NF==3 && $3 == "" {p = $0; next}
      {print (NF == 1 ? p $1 : $0); p = ""}' file

O|field1|field2
O|field1|field2
A|field1|field2
S|field1|field2
O|field1|field2
O|field1|field2
O|field1|field2
A|field1|field2
S|field1|field2
O|field1|field2

CodePudding user response:

Something like this, which tests the layout of the records, might be better for you than testing the values of the fields:

$ awk -v RS='([^|]*[|]){2}[^|]*\n' '{$0=RT; gsub(/\n/,""); print}' file
O|field1|field2
O|field1|field2
A|field1|field2
S|field1|field2
O|field1|field2
O|field1|field2
O|field1|field2
A|field1|field2
S|field1|field2
O|field1|field2

The above uses GNU awk for multi-char RS to just define a record as being 3 fields separated by |s and ending in a newline, then removes any newlines from each record before printing it.

  • Related