Tuesday, 15 December 2015

Advance Concepts Of CUT Command in Unix/Linux

In this article we will see some of the advance feature of CUT command. Please follow previous post Practical Example of CUT command in UNIX/Linux for CUT command basics for more clarity if you have already not followed. We will use below example Input file for our explanation:

Input File: F_Input_Data.txt
10001|A1|HR|10000|USA
10002|A2|FIN|20000|USA
10003|A3|NSS|30000|IND
10004A4SEC40000USA


How to ignore lines that do not contain delimiter
$ cut -s -d"|" -f3,5 F_Input_Data.txt

Output: cut has ignored the 4th line from input file as line is not delimited.
HR|USA
FIN|USA
NSS|IND


How to cut all fields except the ones specified or invert field selection
$ cut --complement -d"|" -f1,4,5 F_Input_Data.txt

Output: cut has returned 2nd & 3rd field
A1|HR
A2|FIN
A3|NSS

10004A4SEC40000USA

How to specify delimiter to be used in output
$ cut --output-delimiter="," -d"|" -f1,4,5 F_Input_Data.txt

Output: cut will return 1st, 4th and 5th field and will list the output in comma(,) separated as given below
10001,10000,USA
10002,20000,USA
10003,30000,IND

10004A4SEC40000USA

All Fields Except the Specified Fields and lines which are not having delimiter
$ cut -d'|' --complement -s -f3  F_Input_Data

Output: has return all the filed except 3rd field and the 4th line (as line is not delimited)
10001|A1|10000|USA
10002|A2|20000|USA
10003|A3|30000|IND

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...