How to incrementally read the lines

2019-07-29 14:26发布

i want to write a script that reads lines from file where the line starts with wt^ and p^. i am able to get the wt^ but i am not able to get p^... and there are 20 wt^ with corresponding p^ now i have to get 10 each wt^ with its corresponding p^ in two different files,

how can i do this?

cat $( ls unit-*-slides.txt | sort -n ) | grep "st^" | split -l 200

i have used above code

wt^blah blah
    p^blah blah blah blah
    p^blah blah blah blah
    p^blah blah blah blah
    p^blah blah blah blah
    p^blah blah blah blah blah blah
    p^blah blah blah blah blah blah blah blah
    p^blah blah blah blah blah blah
    p^blah blah blah blah blah blah
    p^blah blah blah blah blah blah

标签: linux bash shell
2条回答
贪生不怕死
2楼-- · 2019-07-29 15:22

it seems you have some spaces before p^. I copy pested your example and this grep worked fine grep -E "[ ]*(wt\^|p\^)" <file> it takes into account that there might be n number of whitespace before the beginning of the line. You can append xargs with it

查看更多
forever°为你锁心
3楼-- · 2019-07-29 15:23

Use this regex ^(wt\^)|(p\^) to match lines that being with wt^ or p^

Also from your post, it seems lines having p^ have a space or tab in the beginning, that may be a reason why your regex is not matching ^p\^

查看更多
登录 后发表回答