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?
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?
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
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
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.
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.
$ 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.