LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-13-2012, 10:06 AM   #1
tomasdepomas
LQ Newbie
 
Registered: Jul 2012
Location: Amsterdam
Posts: 14

Rep: Reputation: 0
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
 
Old 07-14-2012, 04:31 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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?
 
Old 07-14-2012, 05:48 AM   #3
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
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
 
1 members found this post helpful.
Old 07-16-2012, 06:03 AM   #4
tomasdepomas
LQ Newbie
 
Registered: Jul 2012
Location: Amsterdam
Posts: 14

Original Poster
Rep: Reputation: 0
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?

Last edited by tomasdepomas; 07-16-2012 at 06:08 AM.
 
Old 07-16-2012, 06:53 AM   #5
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
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.
 
1 members found this post helpful.
Old 07-16-2012, 07:03 AM   #6
tomasdepomas
LQ Newbie
 
Registered: Jul 2012
Location: Amsterdam
Posts: 14

Original Poster
Rep: Reputation: 0
@ 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
 
Old 07-16-2012, 07:13 AM   #7
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Oh, I edited too much at once: and parenthesis around the comparison.
 
1 members found this post helpful.
Old 07-16-2012, 07:23 AM   #8
tomasdepomas
LQ Newbie
 
Registered: Jul 2012
Location: Amsterdam
Posts: 14

Original Poster
Rep: Reputation: 0
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
}
}

Last edited by tomasdepomas; 07-16-2012 at 07:25 AM.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Warning: [fnn_insert] Column count doesn't match value count at row 1 in bondoq Programming 2 09-27-2011 04:11 PM
Row count Shell Script zaeem Linux - Newbie 4 10-08-2010 07:35 PM
Serial terminal emulation - bad row column count ? Vilius Linux - Software 3 06-15-2010 09:01 AM
DBD::mysql::st execute failed: Column count doesn't match value count at row 1 shifter Programming 2 02-24-2010 07:42 PM
Row manipulation with awk SHIFTA Linux - Newbie 1 11-05-2009 10:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:41 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