Extracting the strings between the patterns and storing it in different files using Shell Script
I am new to unix shell scripting. My goal is to extract a set of strings present between the lines "开发者_StackOverflow--------" and store each set of strings to different files.
Eg: My file main.txt looks like
----------------------------
One
two
three
----------------------------
abc
four
five
-----------------------------
Expected Output: String appearing in between the lines "----" has to be stored in different files like,
first.txt contains,
one
two
three
Second.txt contains,
abc
four
five
Request your valuable help in this.
Thanks in advance, Srikanth
i=0; file=$i.txt; cat tmp/t.txt |while read line;
do if [ "$line" = "----------------------------" ];
then let "i=i+1"; file=$i.txt; continue;
fi;
echo "$line" >> $file;
done
BTW, I use the shell only for interactive work. All coding gets done in Python.
You could use awk, too. But then you could use Python, Ruby, .... too.
精彩评论