LinuxQuestions.org
Review your favorite Linux distribution.
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 02-28-2012, 02:22 PM   #1
amateurscripter
LQ Newbie
 
Registered: Nov 2011
Posts: 27

Rep: Reputation: Disabled
awk assistance please


hello all, i need help with this awk command. I want to check to see if 14th column is equal to 0 or 1 or 2...9. But doing it this way doesn't work:

$ cat testing.txt|awk -F'|' '{if($14!="[0-9]") print $0}'
awk: {if($14!="[0-9"]) print $0}
awk: ^ syntax error
$

Or this way(w/o the double quotes):
$ cat testing.txt|awk -F'|' '{if($14!="[0-9]) print $0}'
awk: {if($14!=[0-9]) print $0}
awk: ^ syntax error
$

The file is delimited by pipes(|).

Yeah I can do it one by one: '{if($140="0"||$14=="1"||$14=="2"....' but i don't want to do that b/c this is part of a bigger script where I'll be checking for any character [aA-zZ]

does anybody know how i can us [0-9] to verify data? Thx.
 
Old 02-28-2012, 03:30 PM   #2
jevaba
LQ Newbie
 
Registered: Feb 2012
Posts: 5

Rep: Reputation: Disabled
What if you put column 14 in a variable, then test the variable?

Code:
variable14=`cat test.txt|awk -F '|' '{print $14}'`
echo $variable14
 
Old 02-28-2012, 03:39 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
You can do either
Code:
awk -F"|" '$14 ~ /^[0-9]$/' testing.txt
to match a one-digit number, or simply
Code:
awk -F"|" '$14 < 10' testing.txt
Notice that you don't need to pipe the output of cat to awk, since it accepts the file name as argument.
 
1 members found this post helpful.
Old 02-28-2012, 05:36 PM   #4
amateurscripter
LQ Newbie
 
Registered: Nov 2011
Posts: 27

Original Poster
Rep: Reputation: Disabled
Thx Colucix, that's exactly what I needed the regexp comparison($14 ~/^[0-9].... Appreciate it.
 
Old 02-29-2012, 12:47 AM   #5
Valery Reznic
ELF Statifier author
 
Registered: Oct 2007
Posts: 676

Rep: Reputation: 137Reputation: 137
Quote:
Originally Posted by colucix View Post
You can do either
Code:
awk -F"|" '$14 ~ /^[0-9]$/' testing.txt
to match a one-digit number, or simply
Code:
awk -F"|" '$14 < 10' testing.txt
Notice that you don't need to pipe the output of cat to awk, since it accepts the file name as argument.
Second one can be used only if field may contain only the numbers. And the numbers are only non-negative.
 
1 members found this post helpful.
Old 02-29-2012, 09:59 AM   #6
amateurscripter
LQ Newbie
 
Registered: Nov 2011
Posts: 27

Original Poster
Rep: Reputation: Disabled
thx Valery.
 
  


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
parsing a text file - to awk or not to awk ? rollyah Programming 9 08-18-2011 03:20 PM
[SOLVED] call awk from bash script behaves differently to awk from CLI = missing newlines titanium_geek Programming 4 05-26-2011 10:06 PM
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 03:18 AM
Awk scripting assistance needed Zmyrgel Programming 11 06-04-2009 09:58 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 05:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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