How to Grep on the Last Column Only

https://unix.stackexchange.com/questions/383343/how-to-grep-on-last-column-only

How to grep on last column only? Asked 8 years, 2 months ago Modified 8 years, 2 months ago Viewed 9k times 1

I have a sometextfile.txt with ‘ ’ as delimiter.

I would like to grep the value from last column only which consist of word “apple”. How do i do it?

column1 column2 column3 column4 column5 column6 column7
aaaaaaa bbbbbbb ccccccc ddddddd eeeeeee fffffff apple

and grep whole row to a Newfile.txt

shell-scripttext-processingawkgrep

Share Improve this question Follow edited Aug 2, 2017 at 5:47 αғsнιη’s user avatar αғsнιη 41.9k1717 gold badges7575 silver badges117117 bronze badges asked Aug 2, 2017 at 5:19 user6308605’s user avatar user6308605 12122 silver badges55 bronze badges Add a comment 4 Answers Sorted by: 2

awk -F’ ’ ‘$NF == “apple” {print}’ sometextfile.txt > Newfile.txt
Using as the field separator, for each line of sometextfile.txt if the last field ($NF) equals “apple”, print the whole line. Redirect output to Newfile.txt

If you want to test for a specific column rather than the last column, use $n where n is the column number. In this example, $NF is equivalent to $7, and $6 would contain fffffff Share Improve this answer Follow edited Aug 2, 2017 at 5:54 answered Aug 2, 2017 at 5:23 cas’s user avatar cas 84k88 gold badges136136 silver badges205205 bronze badges

I am sorry, there shouldn't be '|' at front. Updated my question – 
user6308605
Commented Aug 2, 2017 at 5:45

Add a comment 2

The original question asked for a grep solution. To find the last column, search for a word which does not have the delimiter [^ ]+ forward to end-of-line $:
grep -Eo “[^ ]+$” infile.txt

Output:

column7 apple

Share Improve this answer Follow answered Aug 2, 2017 at 6:08 hschou’s user avatar hschou 2,9761414 silver badges1616 bronze badges Add a comment 1

The straight solution is to anchor your expression at the end with a $:

grep “ apple$” sometextfile.txt > Newfile.txt

If this doesn’t find the lines, the typical reason is a DOS-formatted text file with trailing carriage returns at the line endings. You can catch that case by allowing trailing non-printable characters at the end (supposing your regex support \s):

grep “ apple\s*$” sometextfile.txt > Newfile.txt

Share Improve this answer Follow edited Aug 2, 2017 at 6:12 answered Aug 2, 2017 at 5:51 Philippos’s user avatar Philippos 13.7k22 gold badges4242 silver badges8282 bronze badges

for some reason it does not work – 
user6308605
Commented Aug 2, 2017 at 5:54
The typical reason for this would be a DOS-formatted text file with a hidden carriage return at the end. Could that be? – 
Philippos
Commented Aug 2, 2017 at 6:07

Add a comment 0

Another awk approach:

awk -F| ‘$NF ~ /^apple$/’ infile.txt >toOutput.txt

Share Improve this answer Follow edited Aug 2, 2017 at 6:24 answered Aug 2, 2017 at 5:55 αғsнιη’s user avatar αғsнιη 41.9k1717 gold badges7575 silver badges117117 bronze badges

1
that works but in english, "consist of" means "equals", not "contains". – 
cas
Commented Aug 2, 2017 at 5:56
any better enhancement? @cas – 
user6308605
Commented Aug 2, 2017 at 6:03 

This will also accept pineapple or apples – Philippos Commented Aug 2, 2017 at 6:03 @user6308605 AFSHIN’s answer is almost the same as mine except that it uses a ~ regexp match rather than a strict == equivalence match (so will, e.g. also match pineapple and apples, and anything else containing apple). You weren’t 100% clear on this so that may or may not be what you actually want. ASHFIN’s version also relies on the default/implicit action of awk (which is to print the current line), while mine has {print} explicitly in the script - that makes no real difference, I only included print in mine to make it clearer what the script is doing. – cas Commented Aug 2, 2017 at 6:16

Updated: