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

yv5phkfx  于 2023-06-30  发布在  Shell
关注(0)|答案(2)|浏览(246)

在下面的文本中,我想跳过第一行,并将$放在从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 {$0~/Part1/($0="$ "$0)}1' myfile

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

dojqjjoe1#

使用所示示例,请尝试以下操作awk。简单的解释是,它跳过了第一行(FNR>1)条件,并且检查一行是否以Part1开始,然后在当前行的值前面添加$。然后提到1将打印编辑/未编辑的行。

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

guz6ccqo2#

如果你想跳过第一行并且不打印它,我会在你的代码中做这样的修改:

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

或者更简洁:

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

相关问题