tags:

views:

534

answers:

3

Hi,

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

+4  A: 

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

tonio
same problem already
Werner
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.
tonio
tr -d '\r' did the jobthanks
Werner
+1  A: 

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

ghostdog74
thanks. your idea is good
Werner
+1  A: 

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
Charles Stewart