05 CUT - kumar159man/MyShellLearning GitHub Wiki
Cut command is used to grab something from command output or a file content
With cut c,b and f flags are used
In UTF-8 encoding c-character and b-bytes are same
Let's display first character of the file
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cat cut.txt This is line one This is line two This is line three This is line four This is line five myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cut -c 1 cut.txt T T T T T
Let's grab the first byte of the file
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cut -b 1 cut.txt T T T T T
Let's display first 10 charecters
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cut -c 1-10 cut.txt This is li This is li This is li This is li This is li
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cut -c -10 cut.txt This is li This is li This is li This is li This is li
Display all characters
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cut -c 1- cut.txt This is line one This is line two This is line three This is line four This is line five
By default cut has tab as a field delimiter
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cut -f 1 cut.txt This This This This This myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cut -f 1- cut.txt This is line one This is line two This is line three This is line four This is line five myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cut -f -3 cut.txt This is line This is line This is line This is line This is line
-d should be used to specify the delimiter
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cut -d " " -f 4 cut.txt one two three four five
If we want to get the result with other delimiter use --output-delimiter flag
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cut -d " " -f -3 cut.txt --output-delimiter="|" This|is|line This|is|line This|is|line This|is|line This|is|line
Let's have the content of the file as
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cat cut.txt This is line one This is line two This is line three This is line four This is line five This is line one delimited by tab This is line two delimited by tab This is line three delimited by tab
File has both tab and space as a delimiter let's use cut operations
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cut -d " " -f -3 cut.txt This is line This is line This is line This is line This is line This is line one delimited by tab This is line two delimited by tab This is line three delimited by tab myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cut -f -3 cut.txt This is line one This is line two This is line three This is line four This is line five This is line This is line This is line
In order to get a proper out put s flag is used
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cut -d " " -sf -3 cut.txt This is line This is line This is line This is line This is line myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cut -sf -3 cut.txt This is line This is line This is line