D
D
DVoropaev2017-01-08 13:16:10
linux
DVoropaev, 2017-01-08 13:16:10

How to process a bunch of lines in bash?

There is a text file, we grep out several lines from it. But how can they be processed separately?
let's say we did something similar to and how to work with $temp further?
temp=$(cat file.txt | grep "string")

Answer the question

In order to leave comments, you need to log in

7 answer(s)
P
planc, 2017-01-08
@planc

[13:34][[email protected]: /tmp ] 
 $  cat 1.txt 
строка 1
строка 2
строка 3
строка 4
строка 5
строка 2
[13:35][[email protected]: /tmp ] 
 $  cat 1.txt | grep 2 |while read line; do echo $line 'добавили еще 3';done
строка 2 добавили еще 3
строка 2 добавили еще 3

A
Andrey Shatokhin, 2017-01-08
@Sovigod

Do
temp=$(cat file.txt | grep "string"| xargs -n1)
Then you'll have an array of strings to loop over. For example
for s in $temp; do
echo $s
done

E
Ethril, 2017-01-08
@Ethril

man xargs

A
abcd0x00, 2017-01-11
@abcd0x00

and how to work with $temp further?

Example
[[email protected] ~]$ text=$(head -3 /etc/passwd)
[[email protected] ~]$ 
[[email protected] ~]$ echo "$text"
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[[email protected] ~]$ 
[[email protected] ~]$ echo "$text" | grep oo
root:x:0:0:root:/root:/bin/bash
[[email protected] ~]$

D
Dmitry Trizna, 2017-01-12
@Dimi3

while read line
do
echo $line
<or_whatever_you_want_to_do_with_each_line_separately>
done < $temp

G
GermSerg, 2017-01-13
@GermSerg

echo $temp>temp
awk '{ system ("echo "$0) }' temp

A
Alex F, 2017-01-14
@delvin-fil

sed

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question