Reputation: 4950
I want AWK to process my file, and change only some lines. But it prints only rule-matched lines. So I've added a /*/ {print $0}
rule. But then I've got a duplications.
awk '/rule 1/ {actions 1;} /*/ {print $0}' file.txt
I want all lines in the output, with 'rule 1' lines changed.
Upvotes: 1
Views: 898
Reputation: 3791
I'll make a big edit, following Ed Norton latest explanations.
as Ed Morton pointed out, it can even be simplified as : changing lines with specific patterns, and then printing all lines
awk '/regex1/ {actions_1} 1' file.txt
(see his comments below for the reason why it's preferable to the one I proposed)
For the record, there exist ways to skip the rest of the processing for the current line, such as : continue
or break
if it is in a loop, or next
if it is in the main loop.
See for example : http://www.gnu.org/software/gawk/manual/html_node/Next-Statement.html#Next-Statement
Upvotes: 1
Reputation: 9936
Or assign the result of actions 1 to $0:
awk '/rule 1/{$0=actions 1}1' file.txt
for example:
awk '/rule 1/{$0=$1}1' file.txt
Upvotes: 0
Reputation: 54542
Adding a 1
to the end of the script, forces awk
to return true, which has the effect of enabling printing of all lines by default. For example, the following will print all lines in the file. However, if the line contains the words rule 1
, only the first field of that line will be printed.
awk '/rule 1/ { print $1; next }1' file
The word next
skips processing the rest of the code for that particular line. You can apply whatever action you'd like. I just chose to print $1
. HTH.
Upvotes: 3