LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-11-2012, 03:56 AM   #1
Vthimmap
LQ Newbie
 
Registered: Oct 2012
Posts: 21

Rep: Reputation: Disabled
Need solution for awk: syntax error near line 1 ERROR


Using awk in kron shell gives the error
awk: syntax error near line 1
awk: bailing out near line 1

Kindly suggest the correct way of using

sample script

#echo " checks the null value in the first column"
less PositPricesUSA.csv.cp | awk -F ',' '$1 == "" { print }'
echo "`cat PositPricesUSA.csv.cp | awk -F\, '{print $1}' | sort | uniq -d`"
if [`cat PositPricesUSA.csv.cp | awk -F\, '{print $1}' | sort | uniq -d | wc -l` == 0] then
echo "No duplicates found in Posit pricing file"
fi


~
 
Old 10-11-2012, 04:08 AM   #2
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,457

Rep: Reputation: 1212Reputation: 1212Reputation: 1212Reputation: 1212Reputation: 1212Reputation: 1212Reputation: 1212Reputation: 1212Reputation: 1212
please use [code][/code] to keep formatting
do not use less (or cat) <filename> | awk <something>, but awk <something> <filename>
So you can rewrite your script:
Code:
#echo " checks the null value in the first column"
awk -F, '$1 == "" { print }' PositPricesUSA.csv.cp
echo "`cat PositPricesUSA.csv.cp | awk -F\, '{print $1}' | sort | uniq -d`"
# here you can eliminate sort, uniq and wc also
if [`awk -F, '{print $1}' PositPricesUSA.csv.cp | sort | uniq -d | wc -l` == 0] then
    echo "No duplicates found in Posit pricing file"
fi
and finally try to find out which awk script causes the problem, the first or the second one
 
1 members found this post helpful.
Old 10-11-2012, 04:16 AM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,225

Rep: Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021
Also
Code:
if [`awk -F, '{print $1}' PositPricesUSA.csv.cp | sort | uniq -d | wc -l` == 0]

# should  look like
if [ `awk -F, '{print $1}' PositPricesUSA.csv.cp | sort | uniq -d | wc -l` -eq 0 ]
You need spaces around '[' & ']'; in fact [[ ]] is better
http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS and numeric comparison operators are different to string comparison operators.
http://tldp.org/LDP/abs/html/comparison-ops.html
 
1 members found this post helpful.
Old 10-11-2012, 04:49 AM   #4
Vthimmap
LQ Newbie
 
Registered: Oct 2012
Posts: 21

Original Poster
Rep: Reputation: Disabled
nawk worked

Since i am using Solaris, awk will not work .I need to use nawk
 
Old 10-13-2012, 06:43 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
Could you please explain the context of what you are doing in more detail, and post an example of the contents of PositPricesUSA.csv.cp? I'm reasonably certain we can rewrite everything much more cleanly. This looks like the kind of thing you can do entirely in awk. Unless you need it as part of a larger script, in which case there may be better ways to integrate it.

Speaking of which, when you say "kron shell", I assume you mean the korn shell (ksh)?

And as mentioned before, you not only have Useless Use Of Cat, but also a useless use of echo on the second line.

Also, $(..) is highly recommended over `..`. (It's posix-defined, so it should work in all modern bourne-based shells.)

I imagine something like this would be cleaner, but again, I can't actually test it right now:
Code:
#this prints to stdout.  Is that what you want?
awk -F ',' '$1 == "" { print }' PositPricesUSA.csv.cp

#It's more efficient to run the external command once only and store the results in a variable if you need to use the values again.  An array might even be a better choice instead of a scalar variable.

result=$( awk -F ',' '{ print $1 }' PositPricesUSA.csv.cp | sort | uniq -d )
echo "$result"
count=$( echo -n "$result" | wc -l )

# "==" is a string test.  For numbers, use -eq
# or even better, use ((..))

if (( ! count )) ; then
     echo "No duplicates found in Posit pricing file"
fi
Finally, "awk" is really a language, not a program. nawk, mawk, and gawk, etc., are just interpreters for that language. gawk, at least, does offer many extensions that the others won't understand, but they should all be able to run basic awk scripts just fine.

Last edited by David the H.; 10-13-2012 at 06:47 AM. Reason: forgot to disable the extra newline in echo
 
1 members found this post helpful.
Old 10-16-2012, 04:04 AM   #6
Vthimmap
LQ Newbie
 
Registered: Oct 2012
Posts: 21

Original Poster
Rep: Reputation: Disabled
Thanks all
 
  


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
Parse error: syntax error, unexpected T_VARIABLE in line 8 gsness Programming 8 01-31-2012 07:53 PM
Error in Bash: line 77: syntax error: unexpected end of file bribon Programming 8 07-13-2011 12:43 PM
Parse error: syntax error (install_admin_menu.php line 2) mike24078 Programming 10 04-08-2010 08:34 AM
Parse error syntax error unexpected '{ ' on line chakaox Programming 2 04-29-2009 03:27 AM


All times are GMT -5. The time now is 08:27 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration