Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

echo from lines of a file

Tags:

bash

echo

cat

i have a file "myfile.txt" that have the next content:

hola mundo
hello word

and i want work with every line

for i in `cat myfile.txt`; do echo $i; done

i hope this give me

hola mundo
hello word

firts one line, then the other, but get

hola
mundo
hello
word

as I can demanding results until newline instead of each space?

ty all

like image 863
patz Avatar asked Mar 04 '12 22:03

patz


1 Answers

That's better

cat myfile.txt | while read line; do
    echo "$line"
done

or even better (doesn't launch other processes such as a subshell and cat):

while read line; do
    echo "$line"
done < myfile.txt

If you prefer oneliners, it's obviously

while read line; do echo "$line"; done < myfile.txt
like image 68
Johannes Weiss Avatar answered Sep 19 '22 20:09

Johannes Weiss