使用 AWK 跳过第一行并对其余部分进行模式匹配

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

在下面的文字中,我想跳过第一行并将 $ 放在以 Part1 开头的行的前面。我已经包含了我的脚本,但它不起作用。你能帮忙吗?

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

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

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

Error:
awk: Syntax error  Context is:
>>>     NR>1 {[=10=]~/Part1/(       <<<

使用您展示的示例,请尝试以下 awk。简单的解释是,它跳过第一行(FNR>1)条件并检查一行是否以 Part1 开头,然后在当前行的值前面添加 $。然后提到 1 将打印 edited/non-edited 行。

awk 'FNR>1 && /^Part1/{[=10=]="$"[=10=]} 1' Input_file

如果您想要 to skip the first line 而不是打印它,我会在您的代码中进行以下更改:

awk 'NR>1 {if ([=10=] ~ /^Part1/) [=10=]="$"[=10=];print}' file

或更简洁:

awk 'NR > 1 {if (/^Part1/) [=11=]="$"[=11=];print}' file
$Part1 Yellow
Part2 Red
Part3 Green
$Part1 Yellow