How to incrementally read the lines

2019-07-29 15:12发布

问题:

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

回答1:

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



回答2:

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\^



标签: linux bash shell