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 08-25-2014, 08:21 AM   #1
sravan14
LQ Newbie
 
Registered: Aug 2014
Posts: 10

Rep: Reputation: Disabled
Error in AWK with IF condition


I this case the variable "l_id" contains multiple spaces. Could you advise me on how to make below code to work when "l_id" has multiple spaces.

code: l_en=`echo $l_key | awk -F'|' '{print $1}'`
l_sc=`echo $l_key | awk -F'|' '{print $2}'`
l_id=`echo $l_key | awk -F'|' '{print $3}'`
l_tf=`echo $l_key | awk -F'|' '{print $4}'`

line1=`cat $DOGS_FILE | awk -F'|' '{ if ( $1 == '$l_en' && $4 == '$l_sc' && $7 == '$l_id' ) { print $0 } }'`

Error:
awk: cmd. line:1: { if ( $1 == 06 && $4 == 16038C915 && $7 ==
awk: cmd. line:1: ^ unexpected newline or end of string
 
Old 08-25-2014, 08:44 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,913

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
please use code tags like:

[code]

-->have you tried:

Code:
l_sc=$(echo ..)
l_id=$(...)
l_tf=$(...)
[/code]

and also:

Code:
line1=$(awk -F'|' '{ if ( $1 == '"$l_en"' && $4 == '"$l_sc"' && $7 == '"$l_id"' ) { print $0 } }' $DOGS_FILE)

Last edited by pan64; 08-25-2014 at 08:46 AM.
 
Old 08-25-2014, 08:52 AM   #3
sravan14
LQ Newbie
 
Registered: Aug 2014
Posts: 10

Original Poster
Rep: Reputation: Disabled
Hi Pan64,

I modified as below, but hitted with syntax errors where I'm expecting that the variable "l_tf" (i.e $13 in AWK command ) having the multiple spaces.
l_en=$(echo $l_key | awk -F'|' '{print $1}')
l_sc=$(echo $l_key | awk -F'|' '{print $2}')
l_id=$(echo $l_key | awk -F'|' '{print $3}')
l_tf=$(echo $l_key | awk -F'|' '{print $4}')

Errors:

awk: { if ( $1 == 06 && $4 == 000008AY8 && $7 == 46204749 && $13 == ) print $0 }
awk: ^ syntax error
awk: { if ( $1 == 06 && $4 == 000008AY8 && $7 == 46204749 && $13 == ) print $0 }
awk: ^ syntax error
awk: { if ( $1 == 06 && $4 == 0016699A0 && $7 == 02015097 && $13 == ) print $0 }
awk: ^ syntax error
awk: { if ( $1 == 06 && $4 == 0016699A0 && $7 == 02015097 && $13 == ) print $0 }
awk: ^ syntax error
awk: { if ( $1 == 06 && $4 == 0016699A0 && $7 == 46204749 && $13 == ) print $0 }
awk: ^ syntax error
 
Old 08-25-2014, 08:56 AM   #4
sravan14
LQ Newbie
 
Registered: Aug 2014
Posts: 10

Original Poster
Rep: Reputation: Disabled
The exact syntaxt error points at $13:


awk: { if ( $1 == 06 && $4 == 000008AY8 && $7 == 46204749 && $13 == ) print $0 }
awk: ^ syntax error
awk: { if ( $1 == 06 && $4 == 000008AY8 && $7 == 46204749 && $13 == ) print $0 }
awk: ^ syntax error
awk: { if ( $1 == 06 && $4 == 0016699A0 && $7 == 02015097 && $13 == ) print $0 }
awk: ^ syntax error
awk: { if ( $1 == 06 && $4 == 0016699A0 && $7 == 02015097 && $13 == ) print $0 }
awk: ^ syntax error
awk: { if ( $1 == 06 && $4 == 0016699A0 && $7 == 46204749 && $13 == ) print $0 }
 
Old 08-25-2014, 06:25 PM   #5
pwalden
Member
 
Registered: Jun 2003
Location: Washington
Distribution: Raspbian, Ubuntu, Chrome/Crouton
Posts: 374

Rep: Reputation: 50
Quote:
awk: { if ( $1 == 06 && $4 == 000008AY8 && $7 == 46204749 && $13 == ) print $0 }
$13 == ??? - missing the rhs argument and hence the syntax error.

$4 == 000008AY8 - I would think that the rhs should be quoted, it is not a number. i.e. "000008AY8"
 
Old 08-26-2014, 06:55 AM   #6
sravan14
LQ Newbie
 
Registered: Aug 2014
Posts: 10

Original Poster
Rep: Reputation: Disabled
In this case, I see "$13" contains 7 space charecters.

I suspect that the IF condition is not handling the spaces.
 
Old 08-26-2014, 08:01 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,913

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
did you put variables between " ?
line1=$(awk -F'|' '{ if ( $1 == '"$l_en"' && $4 == '"$l_sc"' && $7 == '"$l_id"' ) { print $0 } }' $DOGS_FILE)
 
Old 08-26-2014, 08:52 AM   #8
sravan14
LQ Newbie
 
Registered: Aug 2014
Posts: 10

Original Poster
Rep: Reputation: Disabled
Unfortunately no differece:


Code:
           line1=$( awk -F'|' '{ if ( $1 == '"$l_en"' && $4 == '"$l_sc"' && $7 == '"$l_id"' && $13 == '"$l_tf"' ) print $0 }' $DOGS_FILE )
           line2=$( awk -F'|' '{ if ( $1 == '"$l_en"' && $4 == '"$l_sc"' && $7 == '"$l_id"' && $13 == '"$l_tf"' ) print $0 }' $ACTU_FILE )
Same "Syntax error" when $13 contains space charecters.

awk: { if ( $1 == 06 && $4 == 000008AY8 && $7 == 46204749 && $13 == ) print $0 }
awk: ^ syntax error
awk: { if ( $1 == 06 && $4 == 000008AY8 && $7 == 46204749 && $13 == ) print $0 }
 
Old 08-26-2014, 12:19 PM   #9
pwalden
Member
 
Registered: Jun 2003
Location: Washington
Distribution: Raspbian, Ubuntu, Chrome/Crouton
Posts: 374

Rep: Reputation: 50
Is $l_lf an empty string? Hence the $13 == syntax error? What if $l_lf has something in it?

You can also pre-define awk variables in the command line, to eliminate the complex quoting

Code:
line1=$( awk -F'|' --assign l_en="$l_en" --assign l_sc="$l_sc" --assign l_id="$l_id" --assign l_tf="$l_tf -- '{ if ( $1 == l_en && $4 == l_sc && $7 == l_id && $13 == l_tf ) print $0 }' $DOGS_FILE )
 
Old 08-27-2014, 12:21 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,913

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
and another trick to check:
line1=$( awk -F'|' '{ if ( $1 == "'"$l_en"'" && $4 == "'"$l_sc"'" && $7 == "'"$l_id"'" && $13 == "'"$l_tf"'" ) print $0 }' $DOGS_FILE )
 
Old 08-27-2014, 09:08 AM   #11
sravan14
LQ Newbie
 
Registered: Aug 2014
Posts: 10

Original Poster
Rep: Reputation: Disabled
Pwalden,

whenever the variable $l_tf contains spaces, then only hitting with the syntax error. Otherwise it is working fine.
 
Old 08-27-2014, 09:55 AM   #12
pwalden
Member
 
Registered: Jun 2003
Location: Washington
Distribution: Raspbian, Ubuntu, Chrome/Crouton
Posts: 374

Rep: Reputation: 50
Quote:
Originally Posted by sravan14 View Post
Pwalden,

whenever the variable $l_tf contains spaces, then only hitting with the syntax error. Otherwise it is working fine.
Then you will always get a missing rhs argument syntax error when that happens. awk sees '$13 ==' when it parses the command. If you use the variable assignments I proposed, then awk will see '$13 == l_tf' and there will be no syntax error.
 
  


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
[SOLVED] awk with if condition indianovice Linux - Newbie 8 07-12-2012 03:40 AM
[SOLVED] Edit pattern under condition using sed or awk anath0r Programming 4 01-04-2012 03:48 PM
Modify AWK command with correct condition for selective print Perseus Programming 2 10-07-2011 10:10 PM
awk: swap fields given a condition sebelk Programming 5 10-08-2010 08:17 PM
[SOLVED] Update a line using AWK with condition NetRock Programming 11 06-21-2010 08:16 PM

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

All times are GMT -5. The time now is 01:09 PM.

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