String concatenation in bash

I have 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 

therefore, "i" is an integer, and CC is a string of type "TODAY"

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

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

Where is the mistake?

thanks

+7
bash
source share
3 answers

You must try

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

Make sure your file does not contain "\ r", which will end only at the end of $ CC. This may explain why you get 1ODAY.

Try to enable | tr '\ r' '' after the cat command

+7
source share

to 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 the desired result.

+1
source share

ghostdog is right: with the -r option, reading avoids potential horrors such as CRLF. Using arrays makes the -r option more enjoyable:

 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
+1
source share

All Articles