LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-31-2008, 04:12 PM   #1
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,214

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
awk print correct lines when certain columns are blank


hi, i got a text file and i would like to print all the lines where $5 == "CP". the problem is awk thinks that for certain lines the column i'm interested in is $3:
Code:
schneidz@lq:temp> cat test.lst
01   M    EC     D       CP      01   C      2008-03-20  133052274          60054
01   M                   CP      05   C      2008-03-20  133052274          60054
01   V    TC     C       VP      01   C      2008-03-20  133052274          60054
01   M                   CP      05   C      2008-03-20  133052274          60054

schneidz@lq:temp> awk '$5 == "CP" {print $0}' test.lst
01   M    EC     D       CP      01   C      2008-03-20  133052274          60054
schneidz@lq:temp> awk '$3 == "CP" {print $0}' test.lst
01   M                   CP      05   C      2008-03-20  133052274          60054
01   M                   CP      05   C      2008-03-20  133052274          60054
how do i make it print all the lines that have the value "CP" at the column beginning at byte-26 (eventhough some of the preceeding columns may be blank) ?

thanks,
 
Old 03-31-2008, 05:15 PM   #2
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Code:
awk '{if (NF==10 && $5=="CP") print $0; else if (NR!=10 && $2=="CP" || $3=="CP" || $4=="CP") print $0}' test.lst
is a horrible hack.
 
1 members found this post helpful.
Old 03-31-2008, 05:18 PM   #3
prad77
Member
 
Registered: Mar 2008
Posts: 101

Rep: Reputation: 15
if the colunms are tab seperate try to use the awk field seprator as <tab> ctrl H.

Gentoo

Last edited by prad77; 04-17-2008 at 04:31 AM.
 
Old 03-31-2008, 05:23 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
If the string CP never appears in other fields, you can simply use a regular expression:
Code:
gawk '/CP/' test.lst
Quote:
how do i make it print all the lines that have the value "CP" at the column beginning at byte-26 (eventhough some of the preceeding columns may be blank)?
This can be done by
Code:
gawk 'index($0,"CP") == 26' test.lst

Last edited by colucix; 03-31-2008 at 05:31 PM. Reason: Added answer to specific question
 
1 members found this post helpful.
Old 04-01-2008, 08:30 AM   #5
fender_gr
LQ Newbie
 
Registered: Apr 2008
Posts: 10

Rep: Reputation: 0
hi all

first of all i'd like to apologise for my really bad english

well, i have a similar problem with awk

Recently i decided to replace all my Solaris systems at work, with linux (fedora).. actually, that was not my decision, but anyway :P

The problem is the way that awk command prints one of my values. See bellow to understand.

Code:
#!/bin/csh
setenv MYHOME /usr/local/mrtg/daemons/saa/rtd/athmil
setenv NOW `date '+%Y%m%d%H%M%S'`
.....
.....
.....
/usr/bin/cat $MYHOME/all.tmp | /usr/bin/awk '{print "ATH", "," "MIL", "," '$NOW', "," $1, "," $2, "," $3, "," $4}' > $MYHOME/final.log
the file all.tmp contains the following values : 1 32 2 0

The output of this (final.log) in Solaris is ATH ,MIL ,20080401152000 ,1 ,32 ,2 ,0 which is the output i need.
But, the output in Linux is ATH ,MIL ,2.00804e+13 ,1 ,32 ,2 ,0 and i have problem with the bold format.

Any ideas?

Many Thanks

Panos
 
Old 04-01-2008, 08:44 AM   #6
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
I tried something similar and it printed out without the scientific notation.
Code:
echo $NOW | awk '{ print "ATH", "," "MIL" "," '$NOW'}'
What version of gawk do you have?
 
Old 04-01-2008, 08:51 AM   #7
fender_gr
LQ Newbie
 
Registered: Apr 2008
Posts: 10

Rep: Reputation: 0
Quote:
Originally Posted by jschiwal View Post
I tried something similar and it printed out without the scientific notation.
Code:
echo $NOW | awk '{ print "ATH", "," "MIL" "," '$NOW'}'
What version of gawk do you have?
according to man page, i have gawk version 3.1.3
 
Old 04-01-2008, 09:09 AM   #8
fender_gr
LQ Newbie
 
Registered: Apr 2008
Posts: 10

Rep: Reputation: 0
solved... i put double quotes on '$NOW' ... "'NOW'" is the solutions...
 
Old 04-01-2008, 09:22 AM   #9
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by fender_gr View Post
solved... i put double quotes on '$NOW' ... "'NOW'" is the solutions...
In this case it prints $NOW as a string, not as a numeric value. Another solution can be using printf to specify an exact format, as in:
Code:
echo $NOW | gawk '{ printf "ATH ,MIL ,%d\n",'$NOW' }'
 
Old 04-02-2008, 05:40 AM   #10
fender_gr
LQ Newbie
 
Registered: Apr 2008
Posts: 10

Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
In this case it prints $NOW as a string, not as a numeric value. Another solution can be using printf to specify an exact format, as in:
Code:
echo $NOW | gawk '{ printf "ATH ,MIL ,%d\n",'$NOW' }'
i know that with this way it's a string, but i don't have problem with that I'm putting the output into mysql and i'm proccessing the data from the db many thx to everybody
 
Old 04-03-2008, 10:39 PM   #11
JWPurple
Member
 
Registered: Feb 2008
Posts: 67

Rep: Reputation: 17
Quote:
Originally Posted by schneidz View Post
hi, i got a text file and i would like to print all the lines where $5 == "CP". the problem is awk thinks that for certain lines the column i'm interested in is $3:
Whenever I've had this situation I treat the records like fixed-length and use
Code:
if(substr($0, <char pos>, 2) == "CP") { print $0; }
 
1 members found this post helpful.
Old 04-04-2008, 05:06 PM   #12
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,214

Original Poster
Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
thanks for all your suggestins:

colucix and JWPurple post were most useful
 
  


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
Sed/Awk: print lines between n'th and (n+1)'th match of "foo" xaverius Programming 17 08-20-2007 12:39 PM
awk to remove first 3 lines and print remaining $1, $2 fields phyx Linux - General 1 01-10-2007 06:21 PM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 10:19 AM
awk print lines that doesn't have a pattern huynguye Programming 5 05-04-2006 12:08 PM
columns to lines Luskacik Linux - General 2 08-31-2004 12:38 PM


All times are GMT -5. The time now is 04:13 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration