LinuxQuestions.org
Register a domain and help support LQ
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 11-08-2012, 11:28 AM   #1
pathrosa
LQ Newbie
 
Registered: Nov 2012
Posts: 8
Blog Entries: 1

Rep: Reputation: Disabled
Help Me: Please syntax error near unexpected token `|'


Hi,

I have a problem. I have a code in unix, but i need to pass to zLinux. This problem es that i have this code:

echo "for i in \`ls /home/carlossz/validacion/Idc_7_val_*.asc.Z\`\\ndo\\ny=\`zcat \$i|cut -d'|' -f5|sort|uniq -c|awk '{print \$1}'|sort -u|wc -l\`\\necho \"\$i \$y\"\\ndone|grep -v \" 1\"|cut -d\" \" -f1 | sed 's/^/rm -f /g' ">/home/carlossz/validacion/Idc_7_val_ArchCif.sh

This code create a file called Idc_7_val_ArchCif.sh

And after i need run this file:

sh Idc_7_val_ArchCif.sh

But a have this error:

Idc_7_val_ArchCif.sh: line 1: syntax error near unexpected token `|'
Idc_7_val_ArchCif.sh: line 1: `for i in `ls /home/carlossz/validacion/Idc_7_val_*.asc.Z`\ndo\ny=`zcat $i|cut -d'|' -f5|sort|uniq -c|awk '{print $1}'|sort -u|wc -l`\necho "$i $y"\ndone|grep -v " 1"|cut -d" " -f1 | sed 's/^/rm -f /g' '


Please help me, a need run in zlinux but a i have this error.

Thanks,
Pathrosa.
 
Old 11-08-2012, 12:28 PM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
Try to run script as follow:
Code:
./<scriptname.sh>
Or if you are using /bin/bash, then run script as:
Code:
bash <scriptname.sh>
Also break the code into small parts & don't make it too complex. If pb is not solved, then share the original code.
 
Old 11-08-2012, 02:48 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948
Code:
echo "for i in \`ls /home/carlossz/validacion/Idc_7_val_*.asc.Z\`\\ndo\\ny=\`zcat \$i|cut -d'|' -f5|sort|uniq -c|awk '{print \$1}'|sort -u|wc -l\`\\necho \"\$i \$y\"\\ndone|grep -v \" 1\"|cut -d\" \" -f1 | sed 's/^/rm -f /g' ">/home/carlossz/validacion/Idc_7_val_ArchCif.sh
Gah, what is this spaghetti-coded monster?

To start with, generating complex text inside a script is the kind of thing the here document was designed to do.

Code:
cat <<"SCRIPT" >outfile
This is line one of my script
This is line two of my script
This is line three of my script
SCRIPT
The quote marks around the start string ("SCRIPT") keep any $ and ` substitutions in it from expanding first. Remove them if you actually want their contents to appear in the text. Other than that, there's no need for complex escaping in a heredoc. Everything between the two end strings will be printed literally.



Next, I tried to piece out the actual code. I believe this is the script it produces (albeit reformatted for readability)?

Code:
for i in `ls /home/carlossz/validacion/Idc_7_val_*.asc.Z`
do

     y=`zcat $i | cut -d'|' -f5 | sort | uniq -c | awk '{print $1}' | sort -u | wc -l`

     echo "$i $y"

done | grep -v " 1" | cut -d" " -f1 | sed 's/^/rm -f /g'
Again, quite ugly. A couple of comments to start with:

1)
Don't Read Lines With For and Don't parse ls for filenames or metadata. Indeed, the whole substitution is completely unneeded. Just use the globbing pattern directly.

2)
$(..) is highly recommended over `..` anyway. Backticks are deprecated syntax

3)
Long chains of commands like cut|sort|uniq|awk|wc|grep|sed etc. can almost always be reduced to a single simple awk command, or at most awk+sort. If you could show us the output that zcat produces and what you want to get from it, we could help you simplify it greatly.

Last edited by David the H.; 11-08-2012 at 02:52 PM. Reason: fixt some typos
 
Old 11-08-2012, 04:58 PM   #4
pathrosa
LQ Newbie
 
Registered: Nov 2012
Posts: 8
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
Error unexpected end of file

Sorrry, but a run for part but when i run this code:

for i in `ls /home/carlossz/validacion/Idc_7_val_*.asc.Z`\ndo\ny=`zcat $i|cut -d'|' -f5|sort|uniq -c|awk '{print $1}'|sort -u|wc -l`\necho "$i $y"\ndone

this code is in this file Idc_7_val_ArchCif.sh

But i run this code i have this error:

Idc_7_val_ArchCif.sh: line 2: syntax error: unexpected end of file

Please Help Me.

Thanks,
Pathrosa
 
Old 11-08-2012, 05:18 PM   #5
pathrosa
LQ Newbie
 
Registered: Nov 2012
Posts: 8
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
The problem is:

I have this script run in Unix correct in a Stage in DataStage:

echo "for i in \`ls /home/carlossz/validacion/Idc_7_val_*.asc.Z\`\\ndo\\ny=\`zcat \$i|cut -d'|' -f5|sort|uniq -c|awk '{print \$1}'|sort -u|wc -l\`\\necho \"\$i \$y\"\\ndone|grep -v \" 1\"|cut -d\" \" -f1 | sed 's/^/rm -f /g' ">/home/carlossz/validacion/Idc_7_val_ArchCif.sh


But I am making a migration of tecnology and DataStage now I need run in ZLINUX. This script must be run on a Stage in a single line, that's why this programmed that way. If I run it as you are telling me I have no problem, but I need to run it on a single line as this must be programmed.
 
  


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
sh: syntax error near unexpected token `(' venkatesh_b Linux - Newbie 1 05-16-2009 05:44 AM
Syntax error near unexpected token fi Nortekman Linux - Newbie 8 02-27-2009 03:48 PM
syntax error near unexpected token Aigarzs Linux - Newbie 3 01-12-2008 03:39 PM
syntax error near unexpected token `else' josedias Programming 3 09-11-2006 07:09 PM
syntax error near unexpected token ` mattyspatty Programming 8 05-07-2006 05:19 PM


All times are GMT -5. The time now is 08:49 AM.

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