LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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, 08: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, 09:04 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
And you'll stay that way until and unless you give us some data to judge your efforts by.
 
Old 08-12-2014, 09: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, 09: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, 09:08 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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, 09:13 AM   #6
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
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 09:14 AM.
 
1 members found this post helpful.
Old 08-12-2014, 09:13 AM   #7
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
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 09:14 AM.
 
1 members found this post helpful.
Old 08-12-2014, 09: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, 09:16 AM   #9
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
I used the semicolon as the IFS,.. is that not what you wanted?
 
Old 08-12-2014, 09:17 AM   #10
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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 09:23 AM.
 
Old 08-12-2014, 09: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, 09: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



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 02:25 AM
using grep and awk in threads vkivinen Linux - Newbie 2 10-15-2012 01:46 PM
help with grep/sed/awk nikunjbadjatya Programming 8 02-17-2010 07:29 PM
Grep and AWK keogk Linux - General 10 01-04-2010 08:03 PM
newbie needs help for grep and awk parker Programming 1 08-12-2003 04:24 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:44 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
Open Source Consulting | Domain Registration