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

I have a file with several lines, each line with a separator | into multiple columns. I can grep for a specific row, and I can cut a specific column, but I cannot figure out how to do this.

 grep '^1001' customer 

captures lines starting at 1001 from a file named customer

 cut -d "|" -f 3 customer 

cuts column 3 from all rows in the client file.

So....

 grep '^1001' customer | cut -d "|" -f 3 customer 
+4
source share
2 answers

Just leave the file name when you call cut and it will use the grep output as its input:

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

It's also worth noting that grep 1001 does not capture lines starting at 1001; it captures lines containing 1001.

+5
source

It is better to use this task as follows and avoid several commands using the protocol :

 awk -F "|" '$1==1001{print $3}' customer 
+3
source

All Articles