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

Time:03-11

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/(       <<<

CodePudding user response:

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
  • Related