How to use Bash’s cut to modify contents of a file

Posted in Bash on October 25, 2016 · 1 min read
Can you cut a file's contents with Bash?

Today's post we will use the excellent Ipsum generator Bacon Ipsum to test how to modify a file's contents with Bash cut.

We start by taking a single paragraph from Bacon Ipsum and opening a text editor, I will be using BBEdit, and placing each sentence on it's own line:

Bacon ipsum dolor amet fatback beef frankfurter salami pork ham hock ground round chuck kielbasa short ribs turkey.
Landjaeger chuck alcatra jowl prosciutto doner.
Shoulder pork tenderloin sirloin salami.
Chuck prosciutto ham brisket filet mignon ball tip pancetta, t-bone corned beef short loin ground round beef.
Chuck beef ribs ham corned beef short loin, filet mignon jerky flank.
Picanha strip steak drumstick beef, tail turkey capicola pancetta shankle short loin.
Drumstick landjaeger chicken swine tongue turkey tri-tip sirloin shank boudin andouille tenderloin.

and saving the file contents to our Desktop and naming it bacon.txt. After creating our test subject file we should point to the location of bacon.txt which I have placed on my Desktop.

After pointing our terminal to the location of the text file with cd we can now use Bash's cut. Like many commands in the terminal don't forget to check if a manual exists with man. If we wanted to read Cut's manual you would type man cut and to exit the man page you would hit q.

cut can be somewhat complicated to understand so let's break down the syntax:

cut [-b] [-c] [-f list] [-n] [-d delim] [-s] [file]

which stands for:

-b # bytes
-c # characters
-f # separated by a delimiter
-list # a comma separated or blank separated list
-n # used with -b and suppresses splits
-d # delimiter
-s # bypasses lines which contain no field delimiters

I commonly use -c and -d so that is what I will be showing you. In the terminal we will start with the command:

cut -d' ' -f1 bacon.txt

which should generate:

Bacon
Landjaeger
Shoulder
Chuck
Chuck
Picanha
Drumstick

A little explanation of what is going on with cut -d' ' -f1 bacon.txt is we are taking the space delimiter -d' ' and specifying the location of the space with -f1. You may have seen other examples of -d' ' used but they are all the same when targeting space, here is a list in case you are wondering what the variations are:

-d ' '
-d " "
-d \
-d' '
-d" "
"-d "
'-d '
d\

If you want to get the third word used on each line change the command to:

cut -d' ' -f3 bacon.txt

the output:

dolor
alcatra
tenderloin
ham
ribs
steak
chicken

So we played with getting a word based on a certain amount of spaces but if you want a variation or length of characters with cut we use -c:

cut -c 1-10 bacon.txt

it will pull the first 10 characters including space:

Bacon ipsu
Landjaeger
Shoulder p
Chuck pros
Chuck beef
Picanha st
Drumstick

if we wanted to just get a character only we can modify or cut with:

cut -c1 bacon.txt

and that get the first character of every line:

B
L
S
C
C
P
D

If we wanted to get everything after cutting out from so many spaces you would include an -:

cut -d' ' -f2- bacon.txt

which should result in:

ipsum dolor amet fatback beef frankfurter salami pork ham hock ground round chuck kielbasa short ribs turkey.
chuck alcatra jowl prosciutto doner.
pork tenderloin sirloin salami.
prosciutto ham brisket filet mignon ball tip pancetta, t-bone corned beef short loin ground round beef.
beef ribs ham corned beef short loin, filet mignon jerky flank.
strip steak drumstick beef, tail turkey capicola pancetta shankle short loin.
landjaeger chicken swine tongue turkey tri-tip sirloin shank boudin andouille tenderloin.

This is a brief explanation of what you can do with Bash's cut but stick around as we will be creating a few projects in the future with it.

Help the site go ad free:
Storm Trooper