Replace lines matching a pattern with lines from another file in order

Msegade

I want to replace the lines matching a pattern from one file from the lines in order from another file, for example, given:

file1.txt:

aaaaaa
bbbbbb
!! 1234
!! 4567
ccccc
ddddd
!! 1111

we like to replace the lines starting with !! with the lines of this file:

file2.txt:

first line
second line
third line

so the result should be:

aaaaaa
bbbbbb
first line
second line
ccccc
ddddd
third line
Costas

Easy can be done with awk

awk '
    /^!!/{                    #for line stared with `!!`
        getline <"file2.txt"  #read 1 line from outer file into $0 
    }
    1                         #alias for `print $0`
    ' file1.txt

Other version

awk '
    NR == FNR{         #for lines in first file
        S[NR] = $0     #put line in array `S` with row number as index 
        next           #starts script from the beginning
    }
    /^!!/{             #for line stared with `!!`
        $0=S[++count]  #replace line by corresponded array element
    }
    1                  #alias for `print $0`
    ' file2.txt file1.txt

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Move lines matching a pattern from one file to another

From Dev

Perl - Replace pattern only in lines matching another pattern

From Dev

Perl - Replace pattern only in lines matching another pattern

From Dev

Select and remove lines matching a pattern from a file

From Dev

For each line in file A replace all matching lines in file B with a pattern

From Dev

How to delete lines matching a pattern from a file using Tcl/Expect

From Dev

replace lines in a text file from another text file

From Dev

How to replace/add lines from one file to another file

From Dev

Find and replace lines in text file with output from another file

From Dev

replace lines in the original file from another file by line number

From Dev

Matching lines in file from list

From Dev

Removing lines which match with specific pattern from another file

From Dev

Removing lines which match with specific pattern from another file

From Dev

Print specific number of lines furthest from the current pattern match and just before matching another pattern

From Dev

How to find lines in a file matching lines in another file?

From Dev

awk replace column from another file but for a range of lines only

From Dev

Replace certain text in lines with each line from another file

From Dev

Using sed to replace first n lines in a file with the first n lines from another file (say n=5)

From Dev

Removing lines matching a pattern

From Dev

Print all lines of file and matching lines from other file

From Dev

Print all lines of file and matching lines from other file

From Java

Replace a pattern in all lines but only before another pattern

From Dev

replace lines in one file with lines in another by line number

From Dev

replace lines in one file with lines in another by line number

From Dev

regex - replace ordered list of lines matching a string with another list

From Java

Remove lines from a file corresponding to blank lines of another file

From Dev

bash delete lines in file containing lines from another file

From Dev

substitute several lines in a file with lines from another file

From Dev

Remove lines from a file depending on lines found in another file

Related Related

  1. 1

    Move lines matching a pattern from one file to another

  2. 2

    Perl - Replace pattern only in lines matching another pattern

  3. 3

    Perl - Replace pattern only in lines matching another pattern

  4. 4

    Select and remove lines matching a pattern from a file

  5. 5

    For each line in file A replace all matching lines in file B with a pattern

  6. 6

    How to delete lines matching a pattern from a file using Tcl/Expect

  7. 7

    replace lines in a text file from another text file

  8. 8

    How to replace/add lines from one file to another file

  9. 9

    Find and replace lines in text file with output from another file

  10. 10

    replace lines in the original file from another file by line number

  11. 11

    Matching lines in file from list

  12. 12

    Removing lines which match with specific pattern from another file

  13. 13

    Removing lines which match with specific pattern from another file

  14. 14

    Print specific number of lines furthest from the current pattern match and just before matching another pattern

  15. 15

    How to find lines in a file matching lines in another file?

  16. 16

    awk replace column from another file but for a range of lines only

  17. 17

    Replace certain text in lines with each line from another file

  18. 18

    Using sed to replace first n lines in a file with the first n lines from another file (say n=5)

  19. 19

    Removing lines matching a pattern

  20. 20

    Print all lines of file and matching lines from other file

  21. 21

    Print all lines of file and matching lines from other file

  22. 22

    Replace a pattern in all lines but only before another pattern

  23. 23

    replace lines in one file with lines in another by line number

  24. 24

    replace lines in one file with lines in another by line number

  25. 25

    regex - replace ordered list of lines matching a string with another list

  26. 26

    Remove lines from a file corresponding to blank lines of another file

  27. 27

    bash delete lines in file containing lines from another file

  28. 28

    substitute several lines in a file with lines from another file

  29. 29

    Remove lines from a file depending on lines found in another file

HotTag

Archive