Cut Command Examples and Usage in Linux
Cut command is used to select or remove the sections of text from each line of files. We believe that “cut” command is not being used to its maximum potential. Today, we have come up with tutorial based on cut command and how to use it to maximum potential. Cut command plays a very important role while writing the shell scripts and is a very useful command while fetching the desired output from a file.
Practical Examples
We are using file “demo.txt” for explaining various cases of “cut” command. Kindly carefully observe the content written in file “demo.txt”. All the examples displayed below are using this file.
$ cat demo.txt Karl,234,Africa,987654321,Welcome@1,Sunday Mary,123,Europe,897645312,Goodbye@1,Monday John,678,Canada,789456123,Welcome@1,Tuesday Lisa,456,Hungry,890567342,Goodbye@1,Wednesday Paul,567,Greece,998723451,Welcome@1,Thrusday Nina,789,London,768934251,Goodbye@1,Friday
1. Cut the first character of all Columns from a file.
In below case, we are selecting the 1st character of all columns from file demo.txt and output is printed to the console.
$ cut -c1 demo.txt K M J L P N
2. Cut the first character of top three lines from a file.
In this case, we are selecting the 1st character from top three lines using “head” and printing the output to the console.
$ head -n 3 demo.txt | cut -c1 K M J
3. Cut the first character of bottom three lines from a file.
Here, we are selecting the 1st character from last three lines using “tail” and printing the output to the console.
tail -n 3 demo.txt | cut -c1 L P N
4. Cut specific character placed in a specific line of a file.
In this case, We are selecting the 1st character specifically from 3rd line of the file and printing the output to the console.
$ awk 'NR==3' demo.txt | cut -c1 J
5. Cut characters by describing character range.
In below case, characters ranging from 1st position to 15th position are extracted, using cut command and output is printed to the console.
$ cut -c1-15 demo.txt Karl,234,Africa Mary,123,Europe John,678,Canada Lisa,456,Hungry Paul,567,Greece Nina,789,London
6. Cut all the characters before the nth character Number.
Here, we are extracting the data for all the characters listed before the 4th character, it also includes the 4th character and prints the output to the console.
$ cut -c-4 demo.txt Karl Mary John Lisa Paul Nina
7. Cut all the characters after the nth character number.
In this case, we are extracting the data of all the characters listed after the 4th character, it also includes the 4th character and prints the output to the console.
$ cut -c4- demo.txt l,234,Africa,987654321,Welcome@1,Sunday y,123,Europe,897645312,Goodbye@1,Monday n,678,Canada,789456123,Welcome@1,Tuesday a,456,Hungry,890567342,Goodbye@1,Wednesday l,567,Greece,998723451,Welcome@1,Thrusday a,789,London,768934251,Goodbye@1,Friday
8. Cut command usage by complement pattern.
In this case, We are using -c option with –complement, The –complement option selects the inverse of the options passed to sort. Instead of printing the 1st character of all the columns listed in the file, it will print inverse of it. Hence, everything except the 1st character would be printed to the console.
$ cut -c1 --complement demo.txt arl,234,Africa,987654321,Welcome@1,Sunday ary,123,Europe,897645312,Goodbye@1,Monday ohn,678,Canada,789456123,Welcome@1,Tuesday isa,456,Hungry,890567342,Goodbye@1,Wednesday aul,567,Greece,998723451,Welcome@1,Thrusday ina,789,London,768934251,Goodbye@1,Friday
What is delimiter?
A delimiter is a sequence of one or more characters used to specify the boundary between plain text or other data streams. Read More about delimiter.
9. Cut Command Usage Based on a Delimiter in Conjunction with the Field Option.
In this case, We are using comma sign (“,”) as a delimiter and trying to extract the 1st field of all the columns from file demo.txt.
$ cut -d"," -f1 demo.txt Karl Mary John Lisa Paul Nina
10. Cut command usage by complement pattern based on the delimiter.
In this –complement option is used. Therefore, the inverse of above example is printed to the console.
$ cut -d"," --complement -f1 demo.txt 234,Africa,987654321,Welcome@1,Sunday 123,Europe,897645312,Goodbye@1,Monday 678,Canada,789456123,Welcome@1,Tuesday 456,Hungry,890567342,Goodbye@1,Wednesday 567,Greece,998723451,Welcome@1,Thrusday 789,London,768934251,Goodbye@1,Friday
11. Cut multiple fields by defining the field number.
Here, we are extracting the 1st and 5th field of all columns from file demo.txt. Boundaries of starting and ending of the field are based on delimiter used. After successful execution of the command, the output is printed to the console.
$ cut -d"," -f1,5 demo.txt Karl,Welcome@1 Mary,Goodbye@1 John,Welcome@1 Lisa,Goodbye@1 Paul,Welcome@1 Nina,Goodbye@1
12. Cut multiple fields by defining the range.
In this case, we are extracting the fields ranging from 2nd field to 5th field, from file demo.txt. Boundaries of starting and ending of the field are based on delimiter used. After successful execution of the command, the output is printed to the console.
$ cut -d"," -f2-5 demo.txt 234,Africa,987654321,Welcome@1 123,Europe,897645312,Goodbye@1 678,Canada,789456123,Welcome@1 456,Hungry,890567342,Goodbye@1 567,Greece,998723451,Welcome@1 789,London,768934251,Goodbye@1
13. Cut all the fields before the nth field number.
In this case, we are extracting the data of all the fields before the 2nd field, it also includes the 2nd field and prints the output to the console.
$ cut -d"," -f-2 demo.txt Karl,234 Mary,123 John,678 Lisa,456 Paul,567 Nina,789
14. Cut all the fields after the nth field number.
In this case, we are extracting the data of all the fields listed after the 2nd field, it also includes the 2nd field and prints the output to the console.
$ cut -d"," -f2- demo.txt 234,Africa,987654321,Welcome@1,Sunday 123,Europe,897645312,Goodbye@1,Monday 678,Canada,789456123,Welcome@1,Tuesday 456,Hungry,890567342,Goodbye@1,Wednesday 567,Greece,998723451,Welcome@1,Thrusday 789,London,768934251,Goodbye@1,Friday
If you are a good observer, then you must have noticed that the “delimiter value” is also passed to the output, which is printing on the console after every successful execution of the cut command. We can also modify the value to delimiter as per our requirement.
15. Cut command to modify the value of output delimiter.
Here, we are extracting all the fields listed from and after the 2nd field. We are using the –output-delimiter with the command which modifies the value of delimiter from “comma sign” to “space”.
$ cut -d"," -f2- demo.txt --output-delimiter=' ' 234 Africa 987654321 Welcome@1 Sunday 123 Europe 897645312 Goodbye@1 Monday 678 Canada 789456123 Welcome@1 Tuesday 456 Hungry 890567342 Goodbye@1 Wednesday 567 Greece 998723451 Welcome@1 Thrusday 789 London 768934251 Goodbye@1 Friday
15.1 Cut command to modify the value of output delimiter
In this case, we are extracting the 1st and 3rd field of top two columns from file demo.txt. We are using the –output-delimiter option with the command which changes the value of delimiter from “comma sign” to “is equal to” in the output.
$ head -n 2 demo.txt | cut -d"," -f1,3 --output-delimiter=' = ' Karl = Africa Mary = Europe
15.2 Cut command to modify the value of output delimiter
In this case, we are extracting the 1st and 3rd field of last two columns from file demo.txt. We are using the –output-delimiter option with the command which changes the value of delimiter from “comma sign” to “addition sign” in the output.
$ tail -n2 demo.txt | cut -d"," -f1,3 --output-delimiter=' + ' Paul + Greece Nina + London
Production Environment Example
Here we are getting the output of username, UID and its home directory from /etc/passwd file and printing the output to the console.
$ head -5 /etc/passwd | cut -d":" -f1,4,6 root:0:/root daemon:1:/usr/sbin bin:2:/bin sys:3:/dev sync:65534:/bin
Here we are modifying the value of output delimiter rest everything is same as of above example.
$ head -5 /etc/passwd | cut -d":" -f1,4,6 --output-delimiter=' = ' root = 0 = /root daemon = 1 = /usr/sbin bin = 2 = /bin sys = 3 = /dev sync = 65534 = /bin
We have tried to explain all major possible cases of “cut” command. These cases can be used for writing shell scripts, getting required output and with brains can be used to maximum potential. Like and share the post if you guys find this useful. Stay tuned for future update. Thanks!
Recent Comments