7

I have in a bash script:

for i in `seq 1 10` do read AA BB CC <<< $(cat file1 | grep DATA) echo ${i} echo ${CC} SORT=${CC}${i} echo ${SORT} done 

so "i" is a integer, and CC is a string like "TODAY"

I would like to get then in SORT, "TODAY1", etc

But I get "1ODAY", "2ODAY" and so

Where is the error?

Thanks

2
  • The command for i in `seq 1 10` ; do echo HELLO$i ; done gives HELLO1 HELLO2 ... The problem may be in file1 Commented Mar 4, 2010 at 13:28
  • 1
    show an example of your contents of your input file1, and your desired output. Commented Mar 4, 2010 at 13:30

3 Answers 3

7

You should try

SORT="${CC}${i}" 

Make sure your file does not contain "\r" that would end just in the end of $CC. This could well explain why you get "1ODAY".

Try including |tr '\r' '' after the cat command

Sign up to request clarification or add additional context in comments.

2 Comments

your files may have \r\n lien ending, and read understands only the \n. So the \r ends in the CC variable...you should remove it.
+1 Had a related problem reading a curl header into a variable
1

try

 for i in {1..10} do while read -r line do case "$line" in *DATA* ) set -- $line CC=$3 SORT=${CC}${i} echo ${SORT} esac done <"file1" done 

Otherwise, show an example of file1 and your desired output

Comments

1

ghostdog is right: with the -r option, read avoids succumbing to potential horrors, like CRLFs. Using arrays makes the -r option more pleasant:

 for i in `seq 1 10` do read -ra line <<< $(cat file1 | grep DATA) CC="${line[3]}" echo ${i} echo ${CC} SORT=${CC}${i} echo ${SORT} done 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.