1
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.

4
  • 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, 2011 at 10:14
  • 1
    Also double-posted to superuser.com: superuser.com/questions/249307/…
    – Olli
    Feb 23, 2011 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, 2011 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, 2011 at 23:34

3 Answers 3

3

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.

3

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

2

Bash has powerful variable substitution that will save you from having to create extra variables and pipe to another command. Maybe try something like this

while read pos; do
  echo "${pos} ${pos%.tar.gz}"
done

General form is ${var%Pattern} -- Remove from $var the shortest part of $Pattern that matches the back end of $var.

http://www.tldp.org/LDP/abs/html/parameter-substitution.html

You must log in to answer this question.