Building Bash Muscle (7)

I’m digging into “cut” this time. In any platform, cut means you remove certain text and keep that in memory (clipboard) but cut in bash simply means “remove sections from each line of files.”

Here is my sample data in text file data.txt.

id,firstname,lastname
1,john,smith
2,miachel,jackson
3,foo,bar

It’s a comma delimited data. Let’s try to just list the id column using cut.

cut -d , -f 1 data.txt

Here is the result.

id
1
2
3

This means that you use comma as a delimiter and -f 1 means the first column.

We will get just the firstname and lastname like the following.

cut -d , -f 2-3 data.txt

Here is the result.

firstname,lastname
john,smith
miachel,jackson
foo,bar

You could save the filtered data into another file using “>”

cut -d , -f 2-3 data.txt > newdata.txt

Definitely grep is the way to filter data vertically.

cut -d , -f 2-3 data.txt | grep j

Here is the result.

john,smith
miachel,jackson

That’s it for today.

Happy bashing! No, that doesn’t sound so good… 🙂

Author: admin

A software engineer in greater Seattle area

Leave a Reply

Your email address will not be published. Required fields are marked *