Question

I have a flat file1 in below format

Keyword:
First line

Keyword:
line1 
Line2

Keyword:
abc
def
jkl

Keyword:
123456
opertxt

I am using the below awk command and getting the output in file2

awk 'BEGIN { RS = "Keyword:" } {$1=$1} 1' file1 > file2

file2 looks like this

First line
line1 Line2
abc def jkl
123456 opertxt

I do not want to output like this, instead I want the output like the below pattern, the line with the keyword to be removed

First line

line1 
Line2

abc
def
jkl

123456
opertxt

can someone suggest a solution using awk or sed. Thanks

Was it helpful?

Solution

You can use either of these:

sed '/^Keyword:$/d' file

sed looks for lines being exactly Keyword: and deletes them.

awk '$0 == "Keyword:" {next} 1' file

It checks lines that are exactly Keyword: and skips them with next. Then 1 is True, so it performs the default awk action: {print $0}.

Or better and shorter (thanks Jaypal!):

awk '$0 != "Keyword:"' file

Output:

$ awk '$0 == "Keyword:" {next} 1' file
First line

line1
Line2

abc
def
jkl

123456
opertxt

$ sed '/^Keyword:$/d' file
First line

line1
Line2

abc
def
jkl

123456
opertxt
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top