1

I have a file with five lines with pipe separated data.Using sed command I am removing the pipe Only at the end of every line. But when I am passing below command output to a variable then data lose the format and it comes in a single line.

How can we pass the below data to a variable keeping the five lines intact

sed 's/.$//' $input|sort
  • What you mean by "passing to a variable"? Could you show us a bit more of what you're doing? If you're writing something like v=$(sed | sort), we'll need to see it! – Toby Speight Apr 6 '16 at 7:50
1

I can't be 100% sure since you haven't shown us what you're doing, but I am guessing you're simply not quoting your variables. For example, given this file:

$ cat file
foo|bar|baz|
foo|bar|baz|
foo|bar|baz|
foo|bar|baz|
foo|bar|baz|
foo|bar|baz|
foo|bar|baz|

If you run your command on it and save the output in a variable, the newlines will be saved. However, whether they are printed or not depends on how you print the variable:

$ var=$(sed 's/.$//' file | sort)
$ echo $var       ## bad
foo|bar|baz foo|bar|baz foo|bar|baz foo|bar|baz foo|bar|baz foo|bar|baz foo|bar|baz
$ echo "$var"     ## good
foo|bar|baz
foo|bar|baz
foo|bar|baz
foo|bar|baz
foo|bar|baz
foo|bar|baz
foo|bar|baz

When you use an unquoted variable, it invokes the split+glob operator which will split the variable on characters in the $IFS variable. By default, that's space, newline and tab:

$ printf '%s' "$IFS" | od -c
0000000      \t  \n
0000003

Quoting the variable protects it from split+glob which is one of the reasons why you should always quote your variables. For more details on why, see Security implications of forgetting to quote a variable in bash/POSIX shells.

| improve this answer | |
  • Thank a lot for the reply @terdon and Toby. as I am new here, could not describe the question. I have tried your suggestion and it worked. However my script is still giving error in diff command now... can you please help me here. error is "no such file or directory" in below command in script diff "$input3" <(sort "$input1") – rohan Apr 6 '16 at 9:49
  • 1
    @rohan please take the tour to understand how this site works. Unlike traditional forums, we don't have discussion threads, only specific questions and answers. If an answer solves your issue, please accept it by clicking on the checkmark on the left. Then, if you have another question, please post it separately. – terdon Apr 6 '16 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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