How to Get Lines of a Text in AWK
The programming language AWK is designed to manipulate text files into reports. It is installed natively on most Unix and Linux operating systems. Use AWK to search for a particular string in a file, such as an error code in a log file, and return only those lines. AWK can also return the lines that do not match a search string. It can be used to return a certain number of lines from the top of a file. Get lines based on the line number or the number of characters contained in the line.
Instructions
-
-
1
Open a terminal or Konsole window to access a command prompt.
-
2
To print the lines that contain the string "search," type the line:
awk '/search/' filename
Replace "search" with the string or regular expression that you want to search for. Replace "filename" with the name of the file that you want to get the lines from .
-
-
3
To print the lines that do not contain the string "search," type the line:
awk '!/search/' filename
-
4
To print the first 15 lines of a file, type the line:
awk 'NR < 16' filename
.
-
5
To print lines 10 to 30, inclusive, type the line:
awk 'NR==10,NR==30' filename
-
6
To print lines that contain more than 100 characters, type the line:
awk 'length > 100'
-
1