Syntax error near unexpected token

Hi all,

I have a simple script that doesn't work somehow. I can't seem to be spotting the cause of the malfunction.

count=$((1))
for item in `cat test1.txt`
  printf %s `sed -n $((count))p test2.txt` > test3.txt
  count=$((count+1))
  do
  something
done

I get ;

./why.sh: line 3: syntax error near unexpected token `printf'
./why.sh: line 3: `  printf %s `sed -n $((count))p test2.txt` > test3.txt'

Any idea why this might be happening ?

Thanks.

Hi y33t

What do you want to achieve with this script?
You are ignoring the input from test1.txt.

Better use $(cmd) , not the old fashoned `cmd` ,
this also helps to avoid quotation-errors by mistaking ' with ` .

What is that sed command supposed to do?

Try

count=1
for item in `cat test1.txt`
  printf %s `sed -n "$count"p test2.txt` > test3.txt
  count=$((count+1))
  do
  something
done

The only difference I see between

  printf %s `sed -n "$count"p test2.txt` > test3.txt

and

  sed -n "$count"p test2.txt > test3.txt

is that the printf variant will not add a trailing newline character to the sed output.

--------
EDIT: Missed something very obvious:

The valid for syntax is:

for NAME [in WORDS ... ] ; do COMMANDS; done

not

for NAME [in WORDS ... ] ; COMMANDS do COMMANDS; done

Hope this helps.

There is a do statement missing.

for item in `cat test1.txt`
do

Usually a better approach is something like this:

while read item
do
  ....
done <test1.txt >test3.txt

Now the error is gone but I get a completely irrelevant output ;

$./test.sh
ountpountpountpountpountpountpount

What is the content of ./test.sh ?

Please do not leave people guessing. Be complete. Show a representative sample of input, desired output, attempts at a solution and specify what OS and version are being used.

Content of test.sh ;

#!/bin/bash
count=$((1))
while read item
  printf %s `sed -n $"count"p test1.txt` > test2.txt
  count=$((count+1))
  do     
  echo "why?"
done <test3.txt

I am just experimenting to investigate the behavior there is no purpose. Please find test1.txt and test3.txt in the attachment. I want to echo for the amount of line numbers in test3.txt but it doesn't quit the loop now.

Show

  • a representative sample of input - check
  • desired output - missing
  • attempts at a solution - check
  • specify what OS and version are being used - missing

Thank you for the pointers. Desired output ;

why?
why?
.
n

I am temporarily using slackware64 14.
n is the number of line numbers in test3.txt

Try

printf %s `sed -n "$count p" test1.txt`

This should work. Still it would make sense to adopt some programming, say, standards:
Use $(...) instead of deprecated `...`
Use ${...} to separate variable names from its environment/context
Use "..." for strings like format or variables with spaces. So above would become

printf "%s" $(sed -n "${count} p" test1.txt)

Then:

printf "%s" "$(sed -n "${count} p" test1.txt)"

--

That is the screen output. Wat is the desired output in test2.txt ?

Output can be found in the attachment. I also noticed that adding ;

> test2.txt

to the end of you excpression ;

printf "%s" "$(sed -n "${count} p" test1.txt)" > test2.txt

results in an empty test2.txt.

Desired output of test2.txt is count'th line of test3 file. There will always be one line at test2.txt and it will change at each iteration.

The output file would be empty because the script is continually overwriting the output file because of the > redirection every time it goes through the loop.

--

If we combine the recommendations in this thread, we end up with something like this:

count=1
while read item
do
  printf "%s" "$(sed -n "${count} p" test1.txt)"
  count=$((count+1))
  echo "why?"
done < test3.txt > test2.txt

Which produces:

1154why?
1155why?
1151why?
1157why?
1159why?
1163why?
1156why?
1154why?
.....

In test2.txt . Although I still do not understand what it is that you are trying to achieve...

--
The same could be achieved with this:

while read item
do
  IFS= read -r line <&3
  printf "%swhy?\n" "$line"
done < test3.txt 3< test1.txt > test2.txt

Which would be a lot quicker and also more efficient..

1 Like

There is not output.txt generated anywhere. test2.txt will hold all lines if used as scrutinizer proposed in post#14.
As test1.txt and test3.tst happen to have 215 lines each, your output should end with 1072why? . That's it. No extra "why?"s, no extra dots. The contents of test3.txt is irrelevant as it is read but not used anywhere. Should test3.txt be longer than test1.txt , extra "why?"s will be there, if shorter, the output will stop earlier.

This will never happen. If you printf to test2.txt like in post#13, it will be empty in the former case, and hold test1.txt 's respective line's contents in the latter. All the "why?"s go to stdout in both cases.

1 Like