Syntax Error

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

sample of sdn.txt

62387626512
62483736261
628372726392
623982736362

template.txt

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
#!/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

./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

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

 awk 'NR==FNR {T[NR]=$0;CNT=NR;next}  {for (i=1; i<=CNT; i++) sub (/62XX/, T)} 1' 

which might fit and fulfill your needs.

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:

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

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:

#!/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

$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 :slight_smile: you've brighten my day :slight_smile:

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

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 ...

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 :).

#!/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

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

Here is my expected result of the script

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

?

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

I have also tried the script you have given lately.

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

and the result is like this:

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

Thanks for the help. :slight_smile:

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:

#!/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:

62387626512
62483736261
628372726392
623982736362

with or without <carriage-returns> and template.txt contains:

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:

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:

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:

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:

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.

Dear Don,

Your script run flawlessly, i'm gratitude for your help :slight_smile: :b:

Thanks,

Ric aka trakath666 :slight_smile: