Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. 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.

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

Output wanted:

1234567890QWERTYUXY abcdefghi1234567890QWERTYUXY.tar.gz

But the two strings are displayed in two different lines. I want to display the output in 1 line.

share|improve this question
    
What is the input? Can you upload the file, or a sample file along the lines of what you're using to something like paste.ubuntu.com ? –  djeikyb Feb 23 '11 at 10:14
1  
Also double-posted to superuser.com: superuser.com/questions/249307/… –  Olli Feb 23 '11 at 10:53
    
As a note, cross posting is strongly discouraged - it can lead to problems like this one with 1 answer posted on one site and the other three on another –  Yi Jiang Feb 23 '11 at 13:45
    
There's no possible way that code will output more lines than there are lines in the file. Could you please post the actual code that has the problem? and preferably some example input and wanted output. –  geirha Feb 23 '11 at 23:34

2 Answers 2

You can pipe the output through sed, notably:

echo "$string3 | sed ':a;N;$!ba;s/\n/ /g'

All credit to Zsolt Botykai on StackOverflow for his answer

share|improve this answer

A couple changes and it should work:
Generally, when you use a variable like you did in the echo, the original variable remains as it was, so your use of string 2 is totally unnecessary. Also, I am in the habit of using curly braces to {enclose} variables so they can be referenced right inside text, the curly braces work to delimit the variable name, just put the dollar sign in front (example to follow).

1- When you first set string1, do not put spaces on either side of the equal sign.
2- when assigning the results of a command to a variable, you have to force the command to be executed, single back ticks (the character to the left of the #1 key on US keyboards) surrounding the command do this.
3- watch for the number of characters you cut, your code and example do not agree. So that line should look like:

string1=`echo $pos | cut -c 20-38`

The other thing is when you set one value from another do not include the extra equal sign. So the code to set string2 should look like:

string2="$pos"

I believe the following will do as you request, I adjusted the character counts to give the output of your example:

The input "file" for my test contained:
abcdefghi1234567890QWERTYUXY.tar.gz

And my script is:

 #!/bin/sh
while read pos; do
string1=`echo ${pos} | cut -c 10-28`
echo "${string1} ${pos}"
done < file

The output looks like:
1234567890QWERTYUXY abcdefghi1234567890QWERTYUXY.tar.gz

Which is exactly what I believe you are asking for.

share|improve this answer

Your Answer

 
discard

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