3

I have the string xyz which is a line in file1.txt, I want to copy all the lines after xyz in file1.txt to a new file file2.txt. How can I achieve this?

I know about cat command. But how to specify the starting line?

  • 2
    Do you want to include that xyz line or exclude it from being copied ? Also, what happens if you have multiple lines matching xyz ? – don_crissti Mar 09 '19 at 21:44

4 Answers4

4

Using GNU sed

To copy all lines after xyz, try:

sed '0,/xyz/d' file1.txt >file2.txt

1,/xyz/ specifies a range of lines starting with the first and ending with the first occurrence of a line matching xyz. d tells sed to delete those lines.

Note: For BSD/MacOS sed, one can use sed '1,/xyz/d' file1.txt >file2.txt but this only works if the first appearance of xyz is in the second line or later. (Hat tip: kusalananda.)

Another approach, as suggested by don_crissti, should work for all sed:

{ printf %s\\n; cat file1.txt; } | sed '1,/xyz/d' >file2.txt

Example

Consider this test file:

$ cat file1.txt
a
b
xyz
c
d

Run our command:

$ sed '1,/xyz/d' file1.txt >file2.txt
$ cat file2.txt
c
d

Using awk

The same logic can used with awk:

awk 'NR==1,/xyz/{next} 1' file1.txt >file2.txt

NR==1,/xyz/{next} tells awk to skip over all lines from the first (NR==1) to the first line matching the regex xyz. 1 tells awk to print any remaining lines.

John1024
  • 74,655
2

With ed:

ed -s file.txt <<< $'/xyz/+1,$w file2.txt'

This sends one (ranged) command to ed: from the line after (+1) the one containing xyz until the end of the file ($), write those lines to file2.txt.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
1

There is also csplit :

csplit -s file1.txt %xyz%1
ctac_
  • 1,960
0
$ sed -n '/xyz/,$p' file.txt > file2.txt

With -n we prevent sed to print every line. With $ means end of file end p stands for print line. So /xyz/$p means: If a line matches xyz print it until the end of the file.