Search code examples
shellfileifs

Search and copy a specific part of a file to another file in Shell Scripting


Suppose I have a text file with a lot of data. Using Shell Scripting, while reading the file if I get the keyword "CURRENT" it will copy the data to another file until it is getting the keyword "END-0".

Not able to develop the script snippet for this.


Solution

  • You probably want to look at awk or sed for this task.

    # Using SED
    sed -n '/CURRENT/,/END-0/p' input-file >another-file
    
    # Using AWK
    awk '/CURRENT/,/END-0/' input-file >another-file