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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
08-25-2014, 09:21 AM
|
#1
|
LQ Newbie
Registered: Aug 2014
Posts: 10
Rep: 
|
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
|
|
|
08-25-2014, 09:44 AM
|
#2
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,695
|
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 09:46 AM.
|
|
|
08-25-2014, 09:52 AM
|
#3
|
LQ Newbie
Registered: Aug 2014
Posts: 10
Original Poster
Rep: 
|
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
|
|
|
08-25-2014, 09:56 AM
|
#4
|
LQ Newbie
Registered: Aug 2014
Posts: 10
Original Poster
Rep: 
|
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 }
|
|
|
08-25-2014, 07:25 PM
|
#5
|
Member
Registered: Jun 2003
Location: Washington
Distribution: Raspbian, Ubuntu, Chrome/Crouton
Posts: 374
Rep:
|
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"
|
|
|
08-26-2014, 07:55 AM
|
#6
|
LQ Newbie
Registered: Aug 2014
Posts: 10
Original Poster
Rep: 
|
In this case, I see "$13" contains 7 space charecters.
I suspect that the IF condition is not handling the spaces.
|
|
|
08-26-2014, 09:01 AM
|
#7
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,695
|
did you put variables between " ?
line1=$(awk -F'|' '{ if ( $1 == '"$l_en"' && $4 == '"$l_sc"' && $7 == '"$l_id"' ) { print $0 } }' $DOGS_FILE)
|
|
|
08-26-2014, 09:52 AM
|
#8
|
LQ Newbie
Registered: Aug 2014
Posts: 10
Original Poster
Rep: 
|
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 }
|
|
|
08-26-2014, 01:19 PM
|
#9
|
Member
Registered: Jun 2003
Location: Washington
Distribution: Raspbian, Ubuntu, Chrome/Crouton
Posts: 374
Rep:
|
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 )
|
|
|
08-27-2014, 01:21 AM
|
#10
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,695
|
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 )
|
|
|
08-27-2014, 10:08 AM
|
#11
|
LQ Newbie
Registered: Aug 2014
Posts: 10
Original Poster
Rep: 
|
Pwalden,
whenever the variable $l_tf contains spaces, then only hitting with the syntax error. Otherwise it is working fine.
|
|
|
08-27-2014, 10:55 AM
|
#12
|
Member
Registered: Jun 2003
Location: Washington
Distribution: Raspbian, Ubuntu, Chrome/Crouton
Posts: 374
Rep:
|
Quote:
Originally Posted by sravan14
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.
|
|
|
All times are GMT -5. The time now is 05:18 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|