O'Reilly logo

Learning AWK Programming by Shiwang Kalkhanda

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

String operation on selected lines with AWK

In this section, we understand the workings of one-liners for various string operations on lines of a file with AWK:

  • Append the text in the matching line: In this example, we match the specified regular expression in the current input line. If a match is found, we append the desired text in the $0 variable and print; otherwise, we print the current input line only. On finding a match after printing a line with appended text, we use the next statement to start processing the next input line and skip the subsequent statement, as follows:
$ awk '/regex/{print $0 " **ADDITIONAL TEXT **";next}{print}' <FILENAME>

     Or:

$ awk '/maruti/{print $0 " **ADDITIONAL TEXT **";next}{print}' cars.dat
  • Insert ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required