Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grep, then cut from a delimited column file?

Tags:

linux

grep

bash

cut

I have a file with multiple rows, each row delimited with | into multiple columns. I can grep for a certain row, and I can cut for a certain column, but I can't figure out how to do both.

grep '^1001' customer

grabs rows starting with 1001, from a file named customer

cut -d "|" -f 3 customer

cuts column 3 from all the rows in customer file.

So....

grep '^1001' customer | cut -d "|" -f 3 customer
like image 822
Andrew Tsay Avatar asked Dec 26 '22 08:12

Andrew Tsay


1 Answers

Simply omit the filename when you call cut and it'll use the output of grep as its input:

grep 1001 customer | cut -d "|" -f 3

It is also worth noting that grep 1001 doesn't grab rows starting with 1001; it grabs rows containing 1001.

like image 97
NPE Avatar answered Jan 13 '23 14:01

NPE