Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cut from column to end of line

Tags:

bash

unix

I'm having a bit of an issue cutting the output up from egrep. I have output like:

From: First Last
From: First Last
From: First Last

I want to cut out the "From: " (essentially leaving the "First Last").

I tried

cut -d ":" -f 7

but the output is just a bunch of blank lines.

I would appreciate any help.

Here's the full code that I am trying to use if it helps:

egrep '^From:' $file | cut -d ":" -f 7

NOTE: I've already tested the egrep portion of the code and it works as expected.

like image 460
KyleCrowley Avatar asked Oct 17 '13 21:10

KyleCrowley


2 Answers

The cut command lines in your question specify colon-separated fields and that you want the output to consist only of field 7; since there is no 7th field in your input, the result you're getting isn't what you intend.

Since the "From:" prefix appears to be identical across all lines, you can simply cut from the 7th character onward:

egrep '^From:' $file | cut -c7-

and get the result you intend.

like image 88
Aaron Miller Avatar answered Sep 30 '22 23:09

Aaron Miller


The -f argument is for what fields. Since there is only one : in the line, there's only two fields. So changing -f 7 to -f 2- will give you want you want. Albeit with a leading space.

like image 43
grim Avatar answered Oct 01 '22 00:10

grim