Thursday, April 11, 2019

UNIX cut command with examples

Cut by selected bytes

The cut utility allows you to cut text by selecting the bytes you want to extract from the string.

sam@asus:~/unix% echo "test" | cut -b 1 
t
sam@asus:~/unix% echo "test" | cut -b 1,2,3
tes

Cut by selected byte range

You can select a range of bytes to extract from a string using the ‘-’, which specifies a range rather than a single byte.

sam@asus:~/unix% echo "test" | cut -b 1-4
test
sam@asus:~/unix% echo "test" | cut -b 1-2,3-4
test
sam@asus:~/unix% 

Cut by selecting characters

Selecting a character with the cut utility is performed with the ‘-c’ option. It extracts one or more characters from a string.

sam@asus:~/unix% echo "test" | cut -c 1
t
sam@asus:~/unix% echo "test" | cut -c 1,2
te

Cut by selecting character range

Selecting a character range with the ‘-c’ is achieved like the ‘-b’ using the ‘-’ character to indicated a range of characters.

sam@asus:~/unix% echo "test" | cut -c 1-3
tes
sam@asus:~/unix% echo "test" | cut -c 1-2,3-4
test
sam@asus:~/unix% 

Cut based on delimiter

Cut gives you the ability to split up a string based on a delimiter you specify. Using the ‘-d’ option along with the ‘-f’ option which tells cut which fields you wish to print on screen. Out data set contains names separated by ‘;’. By default cut separates the string based on the tab delimiter.

sam@asus:~/unix% cut -d ";" -f 1 sample2.txt
Edan
James
Clinton
Dean
Dawn
sam@asus:~/unix% 

If you wanted to print the second field of the string delimiter by a ‘;’ you to do as so:

sam@asus:~/unix% cut -d ";" -f 2 sample2.txt
Carney
Quinn
Wilkinson
Graves
Owen
sam@asus:~/unix% 

The ‘-d’ option can also cut multiple fields and print them them.

sam@asus:~/unix% cut -d "," -f 1,2 sample3.txt
Edan,Carney
James,Quinn
Clinton,Wilkinson
Dean,Graves
Dawn,Owen
sam@asus:~/unix%

or can print a range

sam@asus:~/unix% cut -d "," -f 1-3 sample3.txt
Edan,Carney,Jerry
James,Quinn,Bob
Clinton,Wilkinson,Winston
Dean,Graves,Carrey
Dawn,Owen,Samatha
sam@asus:~/unix% 

Printing a different output delimiter

Cut gives you the ability to set a new output delimiter instead of using the input delimiter by default.

sam@asus:~/unix% cut -d "," -f 1,2 --output-delimiter='//' sample3.txt
Edan//Carney
James//Quinn
Clinton//Wilkinson
Dean//Graves
Dawn//Owen
sam@asus:~/unix% 

No comments:

Post a Comment

Exploiting Weak WEBDAV Configurations

The server we are going to audit has the following fingerprint. 80/tcp open http Apache httpd 2.2.8 ((Ubuntu) DAV/2) Next we need t...