Grep Access Multiple lines, find all words between two patterns

后端 未结 5 1672
星月不相逢
星月不相逢 2020-12-20 14:59

Need help in scanning text files and find all the words between two patterns. Like say if we have a .sql file, Need to scan and find all words between from\' and \'where\'.

5条回答
  •  别那么骄傲
    2020-12-20 15:03

    You could use ed for this, it allows positive and negative offsets for the regex range. If the input is:

    seq 10 | tee > infile
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    

    Pipe in the command to ed:

    <<< /3/,/6/p | ed -s infile
    

    i.e. print everything between lines containing 3 and 6.

    Result:

    3
    4
    5
    6
    

    To get one more line at each end:

    <<< /3/-1,/5/+1p | ed -s infile
    

    Result:

    2
    3
    4
    5
    6
    7
    

    Or the other way around:

    <<< /3/+1,/6/-1p | ed -s infile
    

    Result:

    4
    5
    

提交回复
热议问题