Grep one column of line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep one column of line
# 1  
Old 10-18-2013
Grep one column of line

Hi all:
I have the following input file:
Code:
function-v    such_as-hash-n    6.5328
function-n    such_as-head-n    2.5586
function-a    such_as-headphone-n    8.0794
function-n    such_as-health-n    3.1938
function-v    such_as-healthcare-n    5.2845
function-n    such_as-hearing-n    9.5367

I need to filter out all of the instances in the first column that do not contain "-n"

Thus, the desired output is the following:
Code:
function-n    such_as-head-n    2.5586
function-n    such_as-health-n    3.1938
function-n    such_as-hearing-n    9.5367

I have tried the following grep:

Code:
grep '([a-z]+)-!n' input > output

with no success.

Does anyone have a viable solution?
Thanks!
# 2  
Old 10-18-2013
Using grep:
Code:
grep -E "^[a-z]+-n" input

Using awk:
Code:
awk '$1~/-n/ input

This User Gave Thanks to Yoda For This Post:
# 3  
Old 10-18-2013
Code:
$ grep -E '^([a-z]+)-n ' input
 function-n    such_as-head-n    2.5586
function-n    such_as-health-n    3.1938
function-n    such_as-hearing-n    9.5367

Or in awk:
Code:
$ awk '$1~/n$/' input
function-n    such_as-head-n    2.5586
function-n    such_as-health-n    3.1938
function-n    such_as-hearing-n    9.5367

This User Gave Thanks to CarloM For This Post:
# 4  
Old 10-18-2013
Code:
$ grep '^[^-]*-n' file 
function-n    such_as-head-n    2.5586
function-n    such_as-health-n    3.1938
function-n    such_as-hearing-n    9.5367

This User Gave Thanks to Scott For This Post:
# 5  
Old 10-22-2013
Hi All:
I have an addendum to the question:
I wanted to filter some extra "noise" from the first column of my input.
Thus, I want to remove all non-alpha characters as well as all words that are less than 2 characters.
I have though to modify the proposed grep as follows:
Code:
grep '^[a-z][a-z][a-z][a-z]-n$' input

It does not produce an output. Can someone help me see where I am going wrong?
thanks!
# 6  
Old 10-22-2013
Hi,
I expect your first column is not below by end line ($) ?
And for less than 2 characters, you must have 3 '[a-z]' and not 4.
So, fix your expression:
Code:
grep '^[a-z][a-z][a-z]-n' input

But this work if you don't have uppers letters, so you can change with:
Code:
grep '^[[:alpha:]][[:alpha:]][[:alpha:]]-n' input

and simplified by:
Code:
grep '^[[:alpha:]]\{3,\}-n' input

{x,y} ==> for repeat charactere or expression: here 3 or more

Regards.
This User Gave Thanks to disedorgue For This Post:
# 7  
Old 10-22-2013
Thanks disedorgue:

The following solution works:
Code:
grep '^[[:alpha:]]\{3,\}-n' input

Yet, doesn not filter for upper-case. Is there a modification to
Code:
 [:alpha:]

which filters out those instances that contain an uppercase?

Last edited by owwow14; 10-22-2013 at 11:00 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep values from column 2 in reference of column 1

Gents Is it possible to update the code to get the desired output files from the input list. I called variable to the first column. I need to consider the first column as key to grep the values in the second column according to the desired request. input list (attached ) output1 ... (12 Replies)
Discussion started by: jiam912
12 Replies

2. Shell Programming and Scripting

How to awk or grep the last column in file when date on column contains spaces?

Hi have a large spreadsheet which has 4 columns APM00111803814 server_2 96085 Corp IT Desktop and Apps APM00111803814 server_2 96085 Corp IT Desktop and Apps APM00111803814 server_2 96034 Storage Mgmt Team APM00111803814 server_2 96152 GWP... (6 Replies)
Discussion started by: kieranfoley
6 Replies

3. Shell Programming and Scripting

How to grep an empty line in a specific column of a file?

Suppose i have the following data : cat file.txt 12431,123334,55353,546646,14342234,4646,35234 123123,3535,123434,132535,1234134,13535,123534 123213,545465,23434,45646,2342345,4656,31243 2355425,2134324,53425,342,35235,23434,234535 3423424,234234,65465,,2344,35436,234524,234... (7 Replies)
Discussion started by: Ravi Tej
7 Replies

4. Shell Programming and Scripting

Merge two files line by line and column by column

Hi All, I have two files having oracle query result. I want to merge to files line by line and also with column File1 23577|SYNC TYPE 23578|Order Number|ConnectionState 23585|Service State|Service NameFile2 23577|AR Alarm Sync 23578|A5499|9 23585|7|test_nov7Result... (18 Replies)
Discussion started by: Harshal22
18 Replies

5. Shell Programming and Scripting

grep based on pattern in a line and print the column before that

$ cat file.log Message Number = : Sending message 10:50:16^|^reqhdr.dummyid^=^02^|^reqhdr.timezone^=^GMT+05:30^|^DUMMYREQUEST^=^BH||||||||||||||||||$BD|OL|C|V||DummyAcctNo|02||24/12/2011|ST_DDM|DDM||||||||reqUUID110612105016$BT||||||||||||||||||$] Length I have the above line in the... (4 Replies)
Discussion started by: kalidass
4 Replies

6. Shell Programming and Scripting

1st column,2nd column on first line 3rd,4th on second line ect...

I need to take one column of data and put it into the following format: 1st line,2nd line 3rd line,4th line 5th line,6th line ... Thanks! (6 Replies)
Discussion started by: batcho
6 Replies

7. UNIX for Dummies Questions & Answers

Grep line to separate column

Hi friends I have a file like this I want output like this probably in excel sheet . Please help Waiting for reply Thanks a lot (14 Replies)
Discussion started by: umapearl
14 Replies

8. Shell Programming and Scripting

grep data on 2nd line and 3rd column

How do I grep/check the on-hand value on the second line of show_prod script below? In this case it's a "3". So if it's > 0, then run_this, otherwise, quit. > ./show_prod Product Status Onhand Price shoe OK 3 1.1 (6 Replies)
Discussion started by: joker_789us
6 Replies

9. Shell Programming and Scripting

Awk+Grep Input file needs to match a column and print the entire line

I'm having problems since few days ago, and i'm not able to make it works with a simple awk+grep script (or other way to do this). For example, i have a input file1.txt: cat inputfile1.txt 218299910417 1172051195 1172070231 1172073514 1183135117 1183135118 1183135119 1281440202 ... (3 Replies)
Discussion started by: poliver
3 Replies

10. Shell Programming and Scripting

print a line containing word in a column using grep

hi, how to print a row which contains a perticular word in its third column using grep, cut, or any thing else. thanks (2 Replies)
Discussion started by: useless79
2 Replies
Login or Register to Ask a Question