awk print Nth line after matching a pattern

 

By using awk we can print only Nth line after matching a pattern.

Lets say I have a file test.txt with following content and I want to extract 5 line after matching pattern.

$ cat test.txt

1
2
3
4
5
6
7
8
9

Use following awk code to extract Nth line:

Syntax:

$ awk 'c&&!--c;/pattern/{c=N}' file

where “pattern” is your input pattern and N is a line number to extract after matching pattern.

$ awk 'c&&!--c;/1/{c=5}' test.txt

Output:

6

In above example I am trying to extract 5th line after matching pattern, here my input pattern is 1 and 5th line after matching pattern is 6. So our output is 6.

awk – print last field

 

NF is built in variable in awk which stores Number of fields.

To get last field with awk use following example:

$ echo a b c | awk '{print $NF}'

Output:
c

By default awk will take any white space character(space, tab, …) to split.

To get last but one field with awk:

$ echo a b c | awk '{print $(NF-1)}'

Output:
b

To print only number of fields with awk:

$ echo a b c | awk '{print NF}'

Output:
3

awk/sed print from line number to end of file

I have file with 10 lines where content of first line in 1, second line 2, third line is 4 …… and tenth line is 10.

Here I will show some operations related to read read in lines based their line numbers.

Print from line number 5 to end of file:

$ awk 'NR>=5' inputFile
or
$ sed '1,4d' inputFile

Print from line number 4 to 9 or print lines between linen number 3 and 10:

$ awk 'NR>=4 && NR<=9'  inputFile
or
$ sed -n '4,9p' inputFile

Print all even lines numbers:

$ awk '{if(NR%2 == 0) {print $1}}' inputFile

Print all odd lines numbers:

$ awk '{if(NR%2 != 0) {print $1}}' inputFile

Useing bash variable with awk

 

We can use bash variable with awk script/command.

First define required variables:

VAR1="india"

VAR2="putin"

My input file names.txt with following content:

uk
us
india
obama
putin
vatican

I will use my previous posts example to print text between two patterns, so I will get text from india to putin with following awk expression uing variables VAR1 & VAR2 that are defined above:

$ awk "/$VAR1/,/$VAR2/ { print }" names.txt

or

$ awk "/${VAR1}/,/${VAR2}/ { print }" names.txt

Output:

For both command we will get same output

india
obama
putin

Similarly you can use bash variable with sed also. For more details refer url where I explained with example.

awk/sed get text between two patterns/strings

By using awk/sed command we can get text between two patterns/strings.

I have file with names.txt with following content:

uk
us
india
obama
putin
vatican

With following sed/awk commands I will show how to get text from india to putin:

With sed:

Syntax:

$ sed -n "/Pattern1/,/Pattern2/p" inputFile

Example:

$ sed -n "/india/,/putin/p" namex.txt

Output:

india
obama
putin

With awk:

Syntax:

$ awk "/Pattern1/,/Pattern2/ { print }" inputFile

Example:

$ awk "/Pattern1/,/Pattern2/ { print }" names.txt

Output:

india
obama
putin

Above awk example will work with gawk.

To install gawk use following command:

$ sudo apt-get install gawk

awk/sed print from pattern to end of file

 

By using awk/sed command we can print text from a given pattern to end of file.

I have file with names.txt with following content:

uk
us
india
obama
putin
vatican

With following sed/awk commands I will show how to print text from line “india” to end of file:

With sed:

Syntax:

$ sed -n '/Pattern/,$p' inputFile

Example:

$ sed -n '/india/,$p' namex.txt

Output:

india
obama
putin
vatican

With awk:

Syntax:

$ awk '/Pattern/,0' inputFile

Example:

$ awk '/india/,0' names.txt

Output:

india
obama
putin
vatican

Count number of lines in a file – Linux

 

There are different ways to count number of lines in a file.

I will show the some of commands to count number files in file.

With wc:

$ wc -l <inutFileName>

Counting number of lines with wc -l is most common and popular way.

With awk:

$ awk 'END{ print NR }' <inutFileName>

With sed:

$ sed -n '$=' <inutFileName>

With grep:

$ grep -c "" <inutFileName>

-Sany

Commad to get sum of numbers in a text file

To get some of numbers in a text file use following one liner

$ awk '{s+=$0} END {print s}' <inputTextFile>

or

$ cat inputTextFile | awk '{s+=$0} END {print s}'

Example:

Lets say my input.txt contains following lines:

$ cat input.txt

100
24
200

To get sum of the numbers in input.txt file use either of following command;

$ cat input.txt | awk '{s+=$0} END {print s}'

or

$ cat input.txt | awk '{s+=$0} END {print s}'

For both commands output is 324

-Sany