====== BASH - Commands - cut - Cut using Characters ======
Cut a section of text based on characters.
Useful when handling large file processing tasks.
----
===== Cut a Single Character from the Input Stream =====
echo "cutting text from input" | cut -c 1
The above command cuts the first character from the standard input and displays it in the terminal.
In this case, it is “c“.
----
===== Cut Specific Characters from the Input Stream =====
echo "cutting text from input" | cut -c 1,3
This command will cut only the first and third characters of the input string and show them.
Remember not to exceed the character limit of your string.
----
===== Cut a Range of Characters from the Input Stream =====
echo "cutting text from input" | cut -c 1-12
Cuts characters from the first position to the twelfth position.
The result will be “cutting text“.
----
===== Cut a Single Character from a Text file =====
cut -c 1 test.txt
Displays only the first characters of each of the five rows of our file test.txt.
It is equivalent to the command cat test.txt | cut -c 1
and provides the same result as we would get when using the byte flag.
----
===== Cut Specific Characters from the a File =====
cut -c 7,10 test.txt
Cuts only the seventh and tenth characters of each five rows.
You can specify any character positions as long as they fall within the range of available characters.
----
===== Cut a Range of Characters a Text File =====
cut -c 1-12 test.txt
Outputs the first one to twelfth characters of each line in the test.txt file.
The cut command in Unix behaves the same when cutting a range of characters and range of bytes.
----
===== Cut from a Specific Character to the End of the Input Stream =====
echo "cutting text from input" | cut -c 5-
The above cut command will cut the text starting from the fifth byte to the end of the string.
It can be beneficial when you need to cut from a specified character position until the end of the input stream.
Simply change the value after b while keeping the trailing – on.
----
===== Cut from a Specific Character to the End of the File =====
cut -c 5- test.txt
This command will start cutting each of the five rows of the test.txt file from their fifth character position and will finish after reaching the end of every line.
The trailing hyphen(-) is mandatory for this kind of operation.
----
===== Cut a Specified Amount of Characters Starting from beginning of a string =====
echo "cutting text from input" | cut -c -5
This command will only cut the first five character positions of our input.
Remember to add the preceding hyphen(-) else the output will not be the same as you expect.
----
===== Cut from the First Character to a Specified Position from a File =====
cut -c -5 test.txt
This cut command in Unix will cut the first five characters of each line from the file test.txt.
Notice how the hyphen(-) is being used for the commands 21-24 in this list.
----
===== Cut the First few Characters in Alphabetical Order =====
cut -c 1-5 test.txt | sort
Cuts the first five bytes of each row alphabetically.
The sort command doesn’t require any options when sorting alphabetically.
----
===== Cut the First few Characters in Reverse Order =====
cut -c 1-5 test.txt | sort -r
Cuts the first five characters from each row and will show them after sorting in reverse.
----
****************************************************************************************************************************************
****************************************************************************************************************************************
****************************************************************************************************************************************
****************************************************************************************************************************************
****************************************************************************************************************************************
----
===== Character Cuts =====
==== Cut a single character ====
cut -c 3
abcdef
returns:
c
----
==== Cut a range of characters ====
A range must be provided in each case which consists of one of N, N-M, N-(N to last) or -M (first to M)
cut -c 2-4
abcdef
Press CTRL+D to stop inputting
returns:
bcd
----
==== Cut a variety of characters ====
cut -c 2,4,7
alongtext
returns:
lne
----
==== Cut characters up to a specific position ====
cut -c -2
abcdef
returns:
ab
----
==== Cut characters from a specific position ====
cut -c 2-
abcdef
returns:
bcdef
----
==== Cut various characters ====
cut -c 1,6-9,16-
alongtextwithnospaces
returns:
atextspaces
----