LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   awk count characters of a row (https://www.linuxquestions.org/questions/linux-general-1/awk-count-characters-of-a-row-4175416530/)

tomasdepomas 07-13-2012 10:06 AM

awk count characters of a row
 
Hi!

I have a huge data set which does not have the same amount of columns in each row. However, the columns are aligned. I would like to check the columns that start at the 20th character of the line and see if the value at that column is larger then 50. In that case I want to print that whole line. Is there an easy way to do this with AWK? Your help is very much appreciated!

cheers,
Tomas

pixellany 07-14-2012 04:31 AM

First, we'll need to know how the data is formatted--e.g. how is a column defined? (It could have the data separated by commas, or it could be based on the number of characters. Can you simply post a sample of the data?

Second, why are you restricting the solution to AWK?

AnanthaP 07-14-2012 05:48 AM

Case A. The data is a number
strng=substr($0,20,length($0)-19)
n=split(strng,arra," ")
if arra[1] > 50 {
... error routines
}

Case B. The data is a string.
strng=substr($0,20,length($0)-19)
n=split(strng,arra," ")
if length(arra[1]) > 50 {
... error routines
}

OK

Assumption (in a SDF - fixed length ASCII - file , the field separator is usually blank. ie. padded to align.

OK

tomasdepomas 07-16-2012 06:03 AM

Thanks for the reply!

The data file is agn.dat at http://cdsarc.u-strasbg.fr/viz-bin/C...8&target=http&

And this is the code that I used:

#!/bin/awk -f

strng=substr($0,83,length($0)-82)
n=split(strng,arra," ")
if arra[1]<17
{
print $0
}

However, it gives the following error:

awk: /home/tomas/scripts/agn:5: if arra[1]<17
awk: /home/tomas/scripts/agn:5: ^ syntax error

Any idea what I do wrong?

Reuti 07-16-2012 06:53 AM

As you want to execute all commands for each line, they need to be enclosed in curly braces around all statements. It’s one case in you awk script.

NB: without giving the length substr will return the rest of the line.

tomasdepomas 07-16-2012 07:03 AM

@ Reuti: So you mean like this?:

#!/bin/awk -f

{
strng=substr($0,83,length($0)-82)
n=split(strng,arra," ")
if arra[1] < 17 {
print $0
}
}

It still gives the following error:

awk: /home/tomas/scripts/agn:6: if arra[1] < 17 {
awk: /home/tomas/scripts/agn:6: ^ syntax error
awk: /home/tomas/scripts/agn:6: if arra[1] < 17 {
awk: /home/tomas/scripts/agn:6: ^ syntax error

Reuti 07-16-2012 07:13 AM

Oh, I edited too much at once: and parenthesis around the comparison.

tomasdepomas 07-16-2012 07:23 AM

Thanks! It works great now.

So this script will check if the number starting at the 83th character of a line is smaller than 11, and if so it will print that whole line:

#!/bin/awk -f
{
strng=substr($0,83,length($0)-82)
n=split(strng,arra," ")
if ( arra[1] < 11 )
{
print $0
}
}


All times are GMT -5. The time now is 07:03 PM.