Skip to content
Linux Commands

icon picker
AWK and Cut Command

To print second column using awk (Based on space separated)
awk 'print $2' data(file name)

Printing two columns simultaneously
5
awk '{print $2,$4}' data

To get last column - $NF

awk '{print $NF}' data

NR - line number
NF - field

To find in files that are separated with , like csv
awk -F, '{print $4}' country.txt

conditions in awk
awk '{if($3>40000) print $0}' data

changing based on condition

awk '{if($2=="Pol"){$3=80000} print $0}' data

Searching something using awk
awk '/India(search string)/ {print $0}' country.txt

For Loop
awk 'BEGIN {for(i=0;i<=10;i++) print i;}'

While loop
awk 'BEGIN {while(i<10){i++; print "number is" i }}'


Cut Command


How to see only 2nd char?
cut -c2 file_name

How to see 1 to 4 char?
cut -c1-4 file_name

How to see only 2 and 4th char?
cut -c2,3 file_name

How to see data from CSV?
cut -d: -f 3 file_name

How to change the delimeter of output?
cut -d, -f 1- country.txt --output-delimiter="|"

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.