$ cat file.txt
unix or linux os
is unix good os
is linux good os
####按照位置截取字符
$ cut -c4 file.txt
x
u
l
$ cut -c4,6 file.txt
xo
ui
ln
$
$ cut -c4-7 file.txt
x or
unix
linu
$
####To print the first six characters in a line, omit the start position and specify only the end position.
$ cut -c-6 file.txt
unix o
is uni
is lin
$
####To print the characters from tenth position to the end, specify only the start position and omit the end position.
$ cut -c10- file.txt
inux os
ood os
good os
$
####If you omit the start and end positions, then the cut command prints the entire line.
$ cut -c- file.txt
unix or linux os
is unix good os
is linux good os
$
####Write a unix/linux cut command to print the fields using the delimiter?
####You can use the cut command just as awk command to extract the fields in a file using a delimiter.
####The -d option in cut command can be used to specify the delimiter and -f option is used to specify the field position.
####This command prints the second field in each line by treating the space as delimiter.
$ cut -d' ' -f2 file.txt
or
unix
linux
$
####You can print more than one field by specifying the position of the fields in a comma delimited list.
####The command prints the second and third field in each line.
$ cut -d' ' -f2,3 file.txt
or linux
unix good
linux good
$
####Note: If the delimiter you specified is not exists in the line, then the cut command prints the entire line. To suppress these lines use the -s option in cut command.
####Write a unix/linux cut command to display range of fields?
####You can print a range of fields by specifying the start and end position.
$ cut -d' ' -f1-3 file.txt
unix or linux
is unix good
is linux good
$
####The above command prints the first, second and third fields. To print the first three fields, you can ignore the start position and specify only the end position.
$ cut -d' ' -f-3 file.txt
unix or linux
is unix good
is linux good
$
####To print the fields from second fields to last field, you can omit the last field position.
$ cut -d' ' -f2- file.txt
or linux os
unix good os
linux good os
$
####Write a unix/linux cut command to display the first field from /etc/passwd file?
####The /etc/passwd is a delimited file and the delimiter is a colon (:). The cut command to display the first field in /etc/passwd file is
cut -d':' -f1 /etc/passwd
####The input file contains the below text
$ cat filenames.txt
logfile.dat
sum.pl
add_int.sh
$
####Using the cut command extract the portion after the dot.
$ cat filenames.txt | cut -d'.' -f2
dat
pl
sh
$
####First reverse the text in each line and then apply the command on it.
$ rev filenames.txt | cut -d'.' -f1
tad
lp
hs