LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-12-2014, 09:55 AM   #1
totemtommie
LQ Newbie
 
Registered: Jul 2014
Posts: 11

Rep: Reputation: Disabled
grep vs awk


Hi,
I have a data table with 7 columns (;separated).
I want to grep string 999999 from column 5 only

grep -ie "999999" filename

gives me results from all columns


awk '{if($5 == "999999") print $0;}' filename

awk '$5 == "999999"' filename

awk '$5 ~ /999999/ {print $0;}' filename

ALL don't work. they give NO results (and no errors)

I am lost!
 
Old 08-12-2014, 10:04 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,840

Rep: Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823
And you'll stay that way until and unless you give us some data to judge your efforts by.
 
Old 08-12-2014, 10:05 AM   #3
totemtommie
LQ Newbie
 
Registered: Jul 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
It has something to do with how the data is stored and read. If I save the original file as text it DOES work...
But I really need to access the original file. Is there a different code for awk and semicolon separated strings maybe?
 
Old 08-12-2014, 10:07 AM   #4
totemtommie
LQ Newbie
 
Registered: Jul 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
1;1;1;var;731791;0;n
1;1;1;var;999999;0;n
1;1;1;var;731792;0;n
1;1;1;var;12345;999999;n
 
Old 08-12-2014, 10:08 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,840

Rep: Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823
Semicolon is not a standard field separator (FS in awk parlance).
Like I said, if you shown us some of the input file we'd have been able to hep better - "man awk" should point you to "-F" so you can specify your field sep.
 
Old 08-12-2014, 10:13 AM   #6
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,811
Blog Entries: 1

Rep: Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345
With that input, and the ifs being ;, you can do this:

Code:
awk 'BEGIN {FS=";"}; $5 == "999999" {print};' inputfile
which will only print the line with 999999 in the fifth column.

ouput:

Code:
[root@dev ~]# cat inputfile
1;1;1;var;731791;0;n
1;1;1;var;999999;0;n
1;1;1;var;731792;0;n
1;1;1;var;12345;999999;n

[root@dev ~]# awk 'BEGIN {FS=";"}; $5 == "999999" {print};' inputfile
1;1;1;var;999999;0;n

Last edited by szboardstretcher; 08-12-2014 at 10:14 AM.
 
1 members found this post helpful.
Old 08-12-2014, 10:13 AM   #7
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,811
Blog Entries: 1

Rep: Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345
With that input, and the ifs being ;, you can do this:

Code:
awk 'BEGIN {FS=";"}; $5 == "999999" {print};' inputfile
which will only print the line with 999999 in the fifth column.

ouput:

Code:
[root@dev ~]# cat inputfile
1;1;1;var;731791;0;n
1;1;1;var;999999;0;n
1;1;1;var;731792;0;n
1;1;1;var;12345;999999;n

[root@dev ~]# awk 'BEGIN {FS=";"}; $5 == "999999" {print};' inputfile
1;1;1;var;999999;0;n

Last edited by szboardstretcher; 08-12-2014 at 10:14 AM.
 
1 members found this post helpful.
Old 08-12-2014, 10:14 AM   #8
totemtommie
LQ Newbie
 
Registered: Jul 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
Yes that should be it. Now I have to find how to specify semicolon in the right code. That was not in the help function. Have an idea?
 
Old 08-12-2014, 10:16 AM   #9
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,811
Blog Entries: 1

Rep: Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345
I used the semicolon as the IFS,.. is that not what you wanted?
 
Old 08-12-2014, 10:17 AM   #10
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,027

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
Code:
[schneidz@hyper ~]$ cat totemtommie.txt 
1;1;1;var;731791;0;n
1;1;1;var;999999;0;n
1;1;1;var;731792;0;n
1;1;1;var;12345;999999;n
[schneidz@hyper ~]$ awk -F \; '$5 ~ /999999/ {print $5}' totemtommie.txt 
999999
although awk is a command it is really an interpretive language where syntax needs to be learned (like english).

man awk (page down to -F).

Last edited by schneidz; 08-12-2014 at 10:23 AM.
 
Old 08-12-2014, 10:20 AM   #11
totemtommie
LQ Newbie
 
Registered: Jul 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
Sorry yes I JUST missed that part.

MANY MANY MANY THANKS!

semicolons..tsss:-)
 
Old 08-12-2014, 10:25 AM   #12
totemtommie
LQ Newbie
 
Registered: Jul 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
And then the more simple code will also work...

awk -F \; '$5 == "999999"' filename


what a day:-)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Looking for tutorial - Awk Vs grep shivaa Linux - Newbie 15 11-02-2012 03:25 AM
using grep and awk in threads vkivinen Linux - Newbie 2 10-15-2012 02:46 PM
help with grep/sed/awk nikunjbadjatya Programming 8 02-17-2010 08:29 PM
Grep and AWK keogk Linux - General 10 01-04-2010 09:03 PM
newbie needs help for grep and awk parker Programming 1 08-12-2003 05:24 AM


All times are GMT -5. The time now is 05:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration