Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using AWK to skip first line and pattern match for the rest

In the following text, I would like to skip the first line and put $ in front of lines starting with Part1. I have included my script, but it not working. Can you please help?

Input
------
Intro
Part1 Yellow
Part2 Red
Part3 Green
Part1 Yellow

Desired output:
--------------
$Part1 Yellow
Part2 Red
Part3 Green
$Part1 Yellow

Code:
awk 'NR>1 {$0~/Part1/($0="$ "$0)}1' myfile

Error:
awk: Syntax error  Context is:
>>>     NR>1 {$0~/Part1/(       <<<

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

With your shown samples please try following awk. Simple explanation would be, its skipping 1st line(FNR>1) condition AND its checking if a line starts with Part1 then its adding $ in front of current line’s value. Then mentioning 1 will print the edited/non-edited line.

awk 'FNR>1 && /^Part1/{$0="$"$0} 1' Input_file
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading