Linux‎ > ‎

cut command ueful examples

Thank you for visiting this page, this page has been update in another link Cut command useful examples on Linux
cut is a command from coreutility which allows you to print selected parts of lines from each FILE to standard output.

Here is an example file from lscpu -p option
# Socket,CPU,Core,Address,Online,Configured,Polarization
0,0,0,,Y,,
0,1,1,,Y,,
0,2,2,,Y,,
0,3,3,,Y,,
0,4,4,,Y,,
0,5,5,,Y,,
1,6,6,,Y,,
1,7,7,,Y,,
1,8,8,,Y,,
1,9,9,,Y,,
1,10,10,,Y,,
1,11,11,,Y,,

Example #1, print the second field
# cut -d',' -f2 testfile
CPU
0
1
2
3
4
5
6
7
8
9
10
11

Option -d and -f
       -d, --delimiter=DELIM
              use DELIM instead of TAB for field delimiter

       -f, --fields=LIST
              select only these fields;  also print any line that contains no delimiter character, unless the -s option is specified

Following 4 flexible options you can choose or tailer it more by youself for you own needs.
# cut -d',' -f2,5 testfile
CPU,Online
0,Y
1,Y
2,Y
...

# cut -d',' -f2-5 testfile
CPU,Core,Address,Online
0,0,,Y
1,1,,Y
...

# cut -d',' -f2- testfile
CPU,Core,Address,Online,Configured,Polarization
0,0,,Y,,
1,1,,Y,,
...

# cut -d',' -f-2 testfile
# Socket,CPU
0,0
0,1
...

Same rule applys to -b and -c
# cut -c1 testfile
#
0
...

# cut -c1,5 testfile
#c
00
...

# cut -c1-5 testfile
# Soc
0,0,0
...

# cut -c-5 testfile
# Soc
0,0,0
...
# cut -c5- testfile
cket,CPU,Core,Address,Online,Configured,Polarization
0,,Y,,
1,,Y,,
...

-b and -c
       -b, --bytes=LIST
              select only these bytes

       -c, --characters=LIST
              select only these characters

Rest options are easy.

       -n     with -b: don't split multibyte characters

       --complement
              complement the set of selected bytes, characters or fields

       -s, --only-delimited
              do not print lines not containing delimiters

       --output-delimiter=STRING
              use STRING as the output delimiter the default is to use the input delimiter




Comments