开发者

Extracting the strings between the patterns and storing it in different files using Shell Script

开发者 https://www.devze.com 2023-04-04 14:46 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号