2

I have file file1.txt /file2.txt When I run the awk command with boundary search between "Type header call_header " and "END" it print the lines below END also . Example :-

File1.txt

Type header call_header 
abc , def , ghi , 
jkl ,mno 
END 
Define call_header 
type as call_header 

Fil2.txt

Type head call_header
data1, data2, voice ,
mms , mms2 
END
Define call_header 
type as call_header

I have tried:

awk '/^Type (header|head) call_header$/,/^END?$/'  {print} file1.txt

However, it is printing other lines also .

Need Below data only :

Type header call_header 
abc , def , ghi , 
jkl ,mno 
END 
3

Your awk invocation is incorrect:

awk '/^Type (header|head) call_header$/,/^END?$/'  {print} file1.txt

Here, awk would search for a file called {print}.

The whole script should be within the single quotes:

awk '/^Type (header|head) call_header$/,/^END?$/ {print}' file1.txt

Or, alternatively (shortening the first regular expression slightly and getting rid of { print } completely),

awk '/^Type head(er)? call_header$/,/^END?$/' file1.txt

Tested with OpenBSD awk, mawk and GNU awk.

With sed, this is remarkably similar to the above:

sed -nr '/^Type head(er)? call_header$/,/^END?$/p' file1.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.