Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.
while read pos; do 
     string1=`echo $pos | cut -c 20-38`
     string2="=$pos" 
     string3="$string1 $string2" 
     echo "$string3" 
done < file

This is the code of my script. I want to display the output in one single line, but the output comes on different lines. How can I do this?

ex- pos = abcdefghi1234567890QWERTYUXY.tar.gz string1 = 1234567890QWERTYUXY string2 = abcdefghi1234567890QWERTYUXY.tar.gz

output wanted :

1234567890QWERTYUXY abcdefghi1234567890QWERTYUXY.tar.gz

share|improve this question
    
What operating system and what shell you are using? –  Olli Feb 23 '11 at 10:27

3 Answers 3

At first, there is multiple typos in your script. Apart from that, you can use

echo -n "$string3 "

to print variable string3, without adding linebreak after that.

From man echo:

   -n     do not output the trailing newline
share|improve this answer
    
echo - n is not available,it does nt work –  sam Feb 23 '11 at 10:23
    
If you are using Ubuntu (askubuntu.com/questions/27705/…), then -n should be available, unless you are using some exotic shell. –  Olli Feb 23 '11 at 10:54

you want to use tr -d '\n' at the end of the $string1 assignment to remove the line break that is present in your file:

while read pos; do 
     string1=`echo $pos | cut -c 20-38 | tr -d '\n'`
     string2="=$pos" 
     string3="$string1 $string2" 
     echo "$string3" 
done < file

note that however, this will give the output:

QWERTYUXY.tar.gz =abcdefghi1234567890QWERTYUXY.tar.gz

with pos = abcdefghi1234567890QWERTYUXY.tar.gz and the script you gave.

share|improve this answer
    
it works but there is one problem..... i need to do one more thing which i didnt mention------ echo "$string3" >> some_file it does not work in this case. string 1 is displayed on terminal and string 2 on the some_file. i want both strings to be displayed in 1 file in some_file. –  sam Feb 23 '11 at 10:41

There are lots of options, but the way I would recommend is printf.

It doesn't print a newline by default, so changing your echo to a printf should remove the unwanted newline.

while read pos; do 
    string1=`printf "%s" $pos | cut -c 20-38`
    string2="=$pos" 
    string3="$string1 $string2" 
    printf "%s\n" "$string3" 
done < file

Or perhaps just

while read pos; do 
    string1=`printf "%s" $pos | cut -c 20-38`
    string2="=$pos" 
    printf "%s=%s\n" "$string1" "$string2"
done < file

Output

1234567890QWERTYUXY=abcdefghi1234567890QWERTYUXY.tar.gz

That said, your original script works for me too. :-/

Output

1234567890QWERTYUXY abcdefghi1234567890QWERTYUXY.tar.gz
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.