Syntax Error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Syntax Error
# 1  
Old 06-25-2015
Syntax Error

I have a shell script , to read file from template and insert it with list from another file,

sample of sdn.txt
Code:
62387626512
62483736261
628372726392
623982736362

template.txt
Code:
62XXatxxxx://xxx.xxx.xxx.xxx:xxxx/?Uid=xxxxxx&Pwd=xxxxx&id=8272626632&sdn=62XX&txt=hello world ?? click xxxx://budi/games/?m=62XX&id=88862XX&txtType=0&code=budi_666

Code:
#!/bin/bash
IMPORT="./home/ric/sdn.txt"
TEMPLATE="./home/ric/template.txt"
 
for i in `cat ${IMPORT}`
do
VAR_1=`echo $i|awk -F '@' '{print $1}'`
VAR_2=`echo $i|awk -F '&' '{print $4}'|awk -F '=' '{print $2}'`
VAR_3=`echo $i|awk -F '=' '{print $7}'|awk -F '&' '{print $1}'`
VAR_4=`echo $i|awk -F '&' '{print $6}'|awk -F '=' '{print $2}'|cut -c4,5,6,7`

cat $TEMPLATE | sed -e s/62XX/$VAR_1/g 
-e s/62XX/$VAR_2/g 
-e s/62XX/$VAR_3/g 
-e s/62XX/$VAR_4/g  
| tee ./output/$VAR_1.txt 1>/dev/null
done

I already chmod +x dmp.sh and i've got this error
Code:
./dmp.sh: line 16: syntax error near unexpected token `|'
./dmp.sh: line 16: `| tee ./output/$VAR_1.txt 1>/dev/null'

What did i do wrong with this ? please i need this script to generate the files

Regards,

trakath666

Moderator's Comments:
Mod Comment Please use code tags next time , thank you

Last edited by vbe; 06-25-2015 at 09:52 AM..
# 2  
Old 06-25-2015
Please use code tags as required by forum rules!

On first sight, you should place all the sed expressions onto one line or use continuation back slashes to eliminate the error cited.

As none of the field separators you define for all the awk statements does exist in the sdn flle, none of your variables will be assigned except for VAR1 which will always receive the entire string.

Replace the > with > to achieve redirection.

---------- Post updated at 11:58 ---------- Previous update was at 11:51 ----------

And, why do you tee the result to a file and then redirect stdout to /dev/null?

---------- Post updated at 12:01 ---------- Previous update was at 11:58 ----------

Inferring from your sample structure given, and wildly guessing, I came up with
Code:
 awk 'NR==FNR {T[NR]=$0;CNT=NR;next}  {for (i=1; i<=CNT; i++) sub (/62XX/, T[i])} 1'

which might fit and fulfill your needs.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 06-25-2015
Syntax Error

Hello RudiC,

Thanks for your response, the true purpose is to replace the 62XX from the template.txt with numbers in sdn.txt, e.g:

Code:
62XX@xxxx://xxx.xxx.xxx.xxx:xxxx/Uid=xxxx&Pwd=xxxx&id=7034056003&sdn=62XX&Txt=Hallo World ?? sent to click xxxx://budi.com/games/m=62XX&id=88762XX&TxtType=0&code=hello_66517

to

Code:
62387626512@xxxx://xxx.xxx.xxx.xxx:xxxx/Uid=xxxx&Pwd=xxxx&id=7034056003&sdn=62387626512&Txt=Hallo World ?? sent to click xxxx://budi.com/games/m=62387626512&id=88762387626512&TxtType=0&code=hello_66517

on sdn.txt i have over 40 thousand lines. i modified my bash script like this:

Code:
#!/bin/bash
IMPORT="/home/ric/sdn.txt"
TEMPLATE="/home/ric/template.txt"

for i in `cat $IMPORT`
do
VAR=`echo $i`
cat $TEMPLATE |sed -e 's/62XX/$VAR/g' | tee /home/ric/$VAR.txt >> /dev/null
done

But the file results only generated $VAR in the 62XX field, like this

Code:
$VAR@xxxx://xxx.xxx.xxx.xxx:xxxx/Uid=xxxx&Pwd=xxxx&id=7034056003&sdn=$VAR&Txt=Hallo World ?? sent to click xxxx://budi.com/games/m=$VAR&id=887$VAR&TxtType=0&code=hello_66517

I appreciate your help Smilie you've brighten my day Smilie

Moderator's Comments:
Mod Comment Code tags also for data samples

Last edited by Scrutinizer; 06-25-2015 at 01:33 PM.. Reason: Code tags also for data samples
# 4  
Old 06-25-2015
That error is because $VAR does not get expanded within single quotes. Use double quotes instead.

Why don't you take a few minutes and write down a decent specification: the logics behind what you need, meaningful samples with structures described, which data come from where and go to where, etc.

If I infer correctly from your last post, you have ONE single value per line in the import file, and this should replace ALL occurrences of 62XX in your template? Try
Code:
awk 'NR==1 {TMPL=$0; next} {X=TMPL; gsub (/62XX/, $1, X); print X}' $TEMPLATE  $IMPORT

---------- Post updated at 16:04 ---------- Previous update was at 16:03 ----------

Still I wonder why you tee and redirect stdout to /dev/null...
# 5  
Old 06-26-2015
Hi RudiC,

My logic is to replace all 62XX occurence on the template with numbers from sdn.txt and create *.txt files with number name it replaced (62387626512.txt) and so on.

Also on sdn.txt there are over 40,000 lines of numbers, so the amount of *.txt files i have to create is equal with that numbers.

I modified my script according to your post also i have removed redirect to stdout Smilie.

Code:
#!/bin/bash
IMPORT="/home/ric/sdn.txt"
TEMPLATE="/home/ric/template.txt"

for i in `cat $IMPORT`
do
VAR=`echo $i`
cat $TEMPLATE |sed "s/62XX/$VAR/g" | tee >> /home/ric/$VAR.txt
done

and the result is like this

Code:
&TxtType=0&code=hello_66517an ?? Hallo World ?? sent to click xxxx://budi.com/games/?m=62387626512

Here is my expected result of the script

Code:
62387626512@xxxx://xxx.xxx.xxx.xxx:xxxx/Uid=xxxx&Pwd=xxxx&id=7034056003&sdn=62387626512&Txt=Hallo World ?? sent to click xxxx://budi.com/games/m=62387626512&id=88762387626512&TxtType=0&code=hello_66517

also on the files generated there is
Code:
?

after the number generated, i.e 62387626512?.txt


I have also tried the script you have given lately.

Code:
awk 'NR==1 {TMPL=$0; next} {X=TMPL; gsub (/62XX/, $1, X); print X}' $TEMPLATE  $IMPORT

and the result is like this:

Code:
&TxtType=0&code=hello_66517an ?? Hallo World ?? sent to click xxxx://budi.com/games/m=62387626512

Thanks for the help. Smilie
# 6  
Old 06-26-2015
Rather than invoking echo 40,000 times (when none are needed), invoking cat 40,001 times (when none are needed), invoking tee 40,000 times (when none are needed), and invoking sed 40,000 times; I would prefer to just invoke awk once. That should speed up your script by a few orders of magnitude.

I am making a (not too wild) assumption that your sdn file was created with an editor or a DOS/Windows utility that uses <carriage-return><newline> character pairs as the line terminator instead of the single <newline> character line terminator expected on UNIX systems, Linux systems, and other similar systems (which is causing the ? in the filenames you're creating when displayed by some versions of the ls utility as an indication that there is a control character at that point in the filename). I'm also making the wild assumption that you want that extraneous <carriage-return> character in all of the output files you're creating if it also exists in your template file. With those assumptions, try changing your script to be:
Code:
#!/bin/bash
IMPORT="/home/ric/sdn.txt"
TEMPLATE="/home/ric/template.txt"

awk '
NR == 1 {
	IN = $0
	next
}
{	OUT = IN
	sub(/\r/, "", $1)
	file = "/home/ric/" $1 ".txt"
	gsub(/62XX/, $1, OUT)
	print OUT >  file
	close(file)
}' "$TEMPLATE" "$IMPORT"

If sdn.txt contains:
Code:
62387626512
62483736261
628372726392
623982736362

with or without <carriage-returns> and template.txt contains:
Code:
62XX@xxxx://xxx.xxx.xxx.xxx:xxxx/Uid=xxxx&Pwd=xxxx&id=7034056003&sdn=62XX&Txt=Hallo World ?? sent to click xxxx://budi.com/games/m=62XX&id=88762XX&TxtType=0&code=hello_66517

it produces the output files:
62387626512.txt containing:
Code:
62387626512@xxxx://xxx.xxx.xxx.xxx:xxxx/Uid=xxxx&Pwd=xxxx&id=7034056003&sdn=62387626512&Txt=Hallo World ?? sent to click xxxx://budi.com/games/m=62387626512&id=88762387626512&TxtType=0&code=hello_66517

62483736261.txt containing:
Code:
62483736261@xxxx://xxx.xxx.xxx.xxx:xxxx/Uid=xxxx&Pwd=xxxx&id=7034056003&sdn=62483736261&Txt=Hallo World ?? sent to click xxxx://budi.com/games/m=62483736261&id=88762483736261&TxtType=0&code=hello_66517

628372726392.txt containing:
Code:
628372726392@xxxx://xxx.xxx.xxx.xxx:xxxx/Uid=xxxx&Pwd=xxxx&id=7034056003&sdn=628372726392&Txt=Hallo World ?? sent to click xxxx://budi.com/games/m=628372726392&id=887628372726392&TxtType=0&code=hello_66517

and 623982736362.txt containing:
Code:
623982736362@xxxx://xxx.xxx.xxx.xxx:xxxx/Uid=xxxx&Pwd=xxxx&id=7034056003&sdn=623982736362&Txt=Hallo World ?? sent to click xxxx://budi.com/games/m=623982736362&id=887623982736362&TxtType=0&code=hello_66517

which seems to be what you're trying to do.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 06-26-2015
Syntax Error (SOLVED)

Dear Don,

Your script run flawlessly, i'm gratitude for your help Smilie Smilie

Thanks,

Ric aka trakath666 Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Getting this error syntax error near unexpected token `)'

Hi Everyone, my script was running Ok, but suddenly it started giving this error. ./update_env_bi.sh: line 54: syntax error near unexpected token `)' ./update_env_bi.sh: line 54: `sed -i "s/PORT=*1/PORT=$2/" repository.xml' The line 54 has this code. sed -i "s/PORT=*1/PORT=$2/"... (2 Replies)
Discussion started by: shajay12
2 Replies

2. Shell Programming and Scripting

IF section problem. syntax error: unexpected end of file error

Hello, I have another problem with my script. Please accept my apologies, but I am really nooby in sh scripts. I am writing it for first time. My script: returned=`tail -50 SapLogs.log | grep -i "Error"` echo $returned if ; then echo "There is no errors in the logs" fi And after... (10 Replies)
Discussion started by: jedzio
10 Replies

3. Linux

Ambiguous redirect error and syntax error when using on multiple files

Hi, I need help on following linux bash script. When I linux commands for loop or while loop on individual file it runs great. but now I want the script to run on N number of files so it gives me ambiguous redirect error on line 12 and syntax error on line 22 : (pls help ); #!/bin/bash #... (16 Replies)
Discussion started by: Madhusudan Das
16 Replies

4. Shell Programming and Scripting

Receiving error: ./ang.ksh[35]: 0403-057 Syntax error at line 116 : `done' is not expected.

Hi All I am quite new to Unix. Following is a shell script that i have written and getting the subject mentioned error. #!/bin/ksh #------------------------------------------------------------------------- # File: ang_stdnld.ksh # # Desc: UNIX shell script to extract Store information.... (3 Replies)
Discussion started by: amitsinha
3 Replies

5. Shell Programming and Scripting

ERROR: ./launch_full_backup.sh[18]: Syntax error at line 28 : `else' is not expected.

Help please! :confused: I have the following error with the following file and the emails are not arriving to the email, any idea please? ERROR: ./launch_full_backup.sh: Syntax error at line 28 : `else' is not expected. FECHA=`date +%d%m%y%H%M`... (2 Replies)
Discussion started by: villenan
2 Replies

6. Programming

Newbie Question.. -> error: syntax error before ';' token

Hello, the following is generating a error at the line "tmprintf(&tmBundle, _TMC("{0}"),Prompt);"... a bit lost as I am diving into this debug... Thank you in advance... int H_YesNo(TMCHAR *Prompt, int DefVal) { TMCHAR YesNo = '\0'; tmprintf(&tmBundle, _TMC("{0}"),Prompt); while... (3 Replies)
Discussion started by: reelflytime
3 Replies

7. Shell Programming and Scripting

sed error : Syntax error: redirection unexpected

My script is throwing the error 'Syntax error: redirection unexpected' My line of code.. cat nsstatustest.html | sed s/<tr><td align="left">/<tr><td align="left" bgcolor="#000000"><font color="white">/ > ztmp.Ps23zp2s.2-Fpps3-wmmm0dss3 HTML tags are getting in the way but they're needed to... (3 Replies)
Discussion started by: phpfreak
3 Replies

8. AIX

nim mksysb error :/usr/bin/savevg[33]: 1016,07: syntax error

-------------------------------------------------------------------------------- Hello, help me please. I am trying to create a mksysb bakup using nim. I am geting this error, how to correct it ? : Command : failed stdout: yes stderr: no... (9 Replies)
Discussion started by: astjen
9 Replies

9. UNIX for Dummies Questions & Answers

awk Shell Script error : "Syntax Error : `Split' unexpected

hi there i write one awk script file in shell programing the code is related to dd/mm/yy to month, day year format but i get an error please can anybody help me out in this problem ?????? i give my code here including error awk ` # date-month -- convert mm/dd/yy to month day,... (2 Replies)
Discussion started by: Herry
2 Replies
Login or Register to Ask a Question