You are not logged in.

#1 2024-09-09 15:18:05

tethys
Member
Registered: 2019-08-13
Posts: 115

[SOLVED] Why is the pipe not working?

I have a file containing words on each line:

word1
word2

I want to merge these words in a string like this:

$ while read i; do mystr+=$i; done < file
$ echo $mystr
word1word2

Now if I do

$ unset mystr
$ cat file | while read i; do mystr+=$i; done
$ echo $mystr

$mystr is empty, although both of the following work as I expect they should:

$ while read i; do echo $i; done < file
word1
word2
$ cat file | while read i; do echo $i; done
word1
word2

Why is the pipe not working?

Last edited by tethys (2024-09-09 19:10:04)

Offline

#2 2024-09-09 16:49:46

mountaintrek
Member
Registered: 2024-02-01
Posts: 36

Re: [SOLVED] Why is the pipe not working?

Offline

#3 2024-09-09 17:23:25

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: [SOLVED] Why is the pipe not working?

Also, don't loop when you don't need to:

mystr=$(tr -d '\n' file)

Though if you don't really need newlines removed (or if replacing them with spaces would suffice) then this could be even simpler.

Note if this is just a simplified example and you have a more complicated loop that you want to feed a file to, but you also want the file listed at the start of the loop, the following syntax is what you'd use:

# redacted bad code

Last edited by Trilby (2024-09-09 22:06:37)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#4 2024-09-09 19:13:39

tethys
Member
Registered: 2019-08-13
Posts: 115

Re: [SOLVED] Why is the pipe not working?

Thank you both for your answers.
I was not aware that in the example with the pipe the loop is executed in a subshell and the variable is lost.
Marked as solved!

Offline

#5 2024-09-09 21:43:54

ayekat
Member
Registered: 2011-01-17
Posts: 1,611

Re: [SOLVED] Why is the pipe not working?

Trilby wrote:
while read var < file; do
   mystr=$mystr$var
done
# mystr is properly set here

This keeps reading the first line of `file` on each iteration, infinitly (at least in bash, zsh, dash, and my understanding of the shell).
I think this is what you meant:

while read var; do
    mystr=$mystr$var
done < file

pkgshackscfgblag

Offline

#6 2024-09-09 22:08:10

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: [SOLVED] Why is the pipe not working?

Oops!  You're right.  But your code block is not what I meant - that's what I thought the OP might be trying to avoid.  I feel sure that there was a way to put the input redirection at the top of the loop without a subshell though ... but at the moment I'm blanking on how.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#7 2024-09-12 09:46:35

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 262

Re: [SOLVED] Why is the pipe not working?

Do you want to merge all lines into a single line? You can do it with

$ tr -d '\n' < file

Or join them in pairs, i.e. append even lines to odd lines?

$ sed '{N;s/\n//;}' < file

Offline

Board footer

Powered by FluxBB