Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Q. What do you mean by the CUT command in LINUX?
- A. It helps remove sections from:
- - Cut Line and file (by byte position, character, and field)
- - Cut, slice, and extract the command and text
- - Saves it to Standard output.
- Cut: Syntax
- -b select by bytes
- -c select by characters
- -f select by field
- -d select by delimiter/separator
- cut option [file_name]
- -b CUT BY BYTES
- 1. How to cut BYTES from Right.
- a). Print first 4 BYTES, cut rest
- cut -b 1,2,3,4 file.txt
- b). Print the first range of BYTES 1-5, cut rest
- cut -b 1-5 file.txt
- c). Print first 1-3 and 5-7 BYTES, cut rest
- cut -b 1-3,5-7 file.txt
- d). Print 3rd BYTES, cut rest
- cut -b 3 file.txt
- e) Print up to 3rd BYTES using -
- cut -b -3 file.txt
- f) Cut FIST BYTES, print rest
- cut -b 2- file.txt
- -C CUT BY CHARACTER
- 1. Print the 2nd and 5th character of each line, rest cut
- cut -c 2,5 file.txt
- 2. Print 2nd to 5th character, cut rest
- cut -c 2-5 file.txt
- -f CUT BY FIELDS RATHER THEN COLUMNS
- -d stands for delimiter (space, tab, commas (,), semicolon (;), quotes ( ", ' ), braces ({}), pipes (|), or slashes ( / \ )
- Example of fields:
- cat file.txt
- Hello world
- hi words
- -f 1 = Field 1 Hello and hi
- -f 2 = Field 2 world and words
- cat file.txt #(delimiter is space. there are 3 spaces in each line, represents first word" "2nd word" "3rd word)
- Delimiter is space (" ") -d " "
- hello wolds boy
- world is hi
- news is no
- is not cool body
- bad thing pain
- 1. Print all the 3rd words
- cut -d " " -f 3 file.txt
- 2. Print all the 2nd words
- cut -d " " -f 2 file.txt
- 3. Print 1st and 2nd-word
- cut -d " " -f 1,2 file.txt
- 4. Print 1st to 3rd-word
- cut -d " " -f 1-3 file.txt
- Delimiter is tab ($'\t') -d$'\t'
- hello wolds boy
- world is hi
- news is no
- is not a cool body
- bad thing pain
- 1. Print all the 3rd words
- cut -d$'\t' -f 1,3 file.txt
- 2. Print all the 2nd words
- cut -d$'\t' -f 2 file.txt
- 3. Print 1st and 2nd word
- cut -d$'\t' -f 1,2 file.txt
- 4. Print 1st to 3rd word
- cut -d$'\t' -f 1-3 file.txt
- Delimiter is : (":") -d ":"
- hello:wolds:boy
- world:is:hi
- news:is:no
- isnot:cool:body
- bad:thing:pain
- 1. Print all the 3rd words
- cut -d ':' -f 1,3 file.txt
- 2. Print all the 2nd words
- cut -d ':' -f 2 file.txt
- 3. Print 1st and 2nd-word
- cut -d ':' -f 1,2 file.txt
- 4. Print 1st to 3rd-word
- cut -d ':' -f 1-3 file.txt
- 5. Print 1st to 3rd-word and 3-4
- cut -d ':' -f 1-3,3-4 file.txt
- 6. Print 1st to 3rd
- cut -d ':' -f -3 file.txt
- 7. Print from 3rd to last
- cut -d ':' -f 3- file.txt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement