Extract lines between 2 tokens in a text file using bash

后端 未结 7 678
死守一世寂寞
死守一世寂寞 2020-12-05 07:26

i have a text file which looks like this:

random useless text 
 
para1 
para2 
para3 
 
random us         


        
相关标签:
7条回答
  • 2020-12-05 08:11

    Try the following:

    sed -n '/<!-- this is token 1 -->/,/<!-- this is token 2 -->/p' your_input_file
            | egrep -v '<!-- this is token . -->'
    
    0 讨论(0)
  • 2020-12-05 08:11

    no need to call mighty sed / awk / perl. You could do it "bash-only":

    #!/bin/bash
    STARTFLAG="false"
    while read LINE; do
        if [ "$STARTFLAG" == "true" ]; then
                if [ "$LINE" == '<!-- this is token 2 -->' ];then
                        exit
                else
                        echo "$LINE"
                fi
        elif [ "$LINE" == '<!-- this is token 1 -->' ]; then
                STARTFLAG="true"
                continue
        fi
    done < t.txt
    

    Kind regards

    realex

    0 讨论(0)
  • 2020-12-05 08:15
    sed -n "/TOKEN1/,/TOKEN2/p" <YOUR INPUT FILE> | sed -e '/TOKEN1/d' -e '/TOKEN2/d'
    
    0 讨论(0)
  • 2020-12-05 08:19

    Maybe sed and awk have more elegant solutions, but I have a "poor man's" approach with grep, cut, head, and tail.

    #!/bin/bash
    
    dataFile="/path/to/some/data.txt"
    startToken="token 1"
    stopToken="token 2"
    
    startTokenLine=$( grep -n "${startToken}" "${dataFile}" | cut -f 1 -d':' )
    stopTokenLine=$( grep -n "${stopToken}" "${dataFile}" | cut -f 1 -d':' )
    
    let stopTokenLine=stopTokenLine-1
    let tailLines=stopTokenLine-startTokenLine
    
    head -n ${stopTokenLine} ${dataFile} | tail -n ${tailLines}
    
    0 讨论(0)
  • 2020-12-05 08:20

    You can extract it, including the tokens with sed. Then use head and tail to strip the tokens off.

    ... | sed -n "/this is token 1/,/this is token 2/p" | head -n-1 | tail -n+2

    0 讨论(0)
  • 2020-12-05 08:21

    No need for head and tail or grep or to read the file multiple times:

    sed -n '/<!-- this is token 1 -->/{:a;n;/<!-- this is token 2 -->/b;p;ba}' inputfile
    

    Explanation:

    • -n - don't do an implicit print
    • /<!-- this is token 1 -->/{ - if the starting marker is found, then
      • :a - label "a"
        • n - read the next line
        • /<!-- this is token 2 -->/q - if it's the ending marker, quit
        • p - otherwise, print the line
      • ba - branch to label "a"
    • } end if
    0 讨论(0)
提交回复
热议问题