Answer the question
In order to leave comments, you need to log in
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
[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
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
and how to work with $temp further?
[[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] ~]$
while read line
do
echo $line
<or_whatever_you_want_to_do_with_each_line_separately>
done < $temp
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question