LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   awk print correct lines when certain columns are blank (https://www.linuxquestions.org/questions/programming-9/awk-print-correct-lines-when-certain-columns-are-blank-631994/)

schneidz 03-31-2008 03:12 PM

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,

pwc101 03-31-2008 04:15 PM

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.

prad77 03-31-2008 04:18 PM

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

Gentoo

colucix 03-31-2008 04:23 PM

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

fender_gr 04-01-2008 07:30 AM

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

jschiwal 04-01-2008 07:44 AM

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?

fender_gr 04-01-2008 07:51 AM

Quote:

Originally Posted by jschiwal (Post 3107052)
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

fender_gr 04-01-2008 08:09 AM

solved... i put double quotes on '$NOW' ... "'NOW'" is the solutions...

colucix 04-01-2008 08:22 AM

Quote:

Originally Posted by fender_gr (Post 3107082)
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' }'

fender_gr 04-02-2008 04:40 AM

Quote:

Originally Posted by colucix (Post 3107097)
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

JWPurple 04-03-2008 09:39 PM

Quote:

Originally Posted by schneidz (Post 3106283)
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; }

schneidz 04-04-2008 04:06 PM

thanks for all your suggestins:

colucix and JWPurple post were most useful


All times are GMT -5. The time now is 03:23 AM.