0

I have below file: main.txt

/F/FID/FID/000001/0/20180215//1/2/
/F/FID/FID/000002/0/20180215//1/2/
/F/FID/FID/000003/0/20180215//1/2/
/F/FID/FID/000004/0/20180215//1/2/
/F/FID/FID/000005/0/20180215//1/2/
/F/FID/FID/000006/0/20180215//1/2/

I like to use sed to print every two rows into a new file:

file1.txt

/F/FID/FID/000001/0/20180215//1/2/
/F/FID/FID/000002/0/20180215//1/2/

file2.txt

/F/FID/FID/000003/0/20180215//1/2/
/F/FID/FID/000004/0/20180215//1/2/

file3.txt

/F/FID/FID/000005/0/20180215//1/2/
/F/FID/FID/000006/0/20180215//1/2/
  • split command is better suited here.. see man split for details.. if you try that and still have some doubts, you can update the question.. – Sundeep Feb 28 '18 at 5:02
1

Not easy with sed, but

awk 'NR%2 {fname = sprintf("file%d.txt", ++n)} {print > fname}' main.txt

giving

head file*.txt
==> file1.txt <==
/F/FID/FID/000001/0/20180215//1/2/
/F/FID/FID/000002/0/20180215//1/2/

==> file2.txt <==
/F/FID/FID/000003/0/20180215//1/2/
/F/FID/FID/000004/0/20180215//1/2/

==> file3.txt <==
/F/FID/FID/000005/0/20180215//1/2/
/F/FID/FID/000006/0/20180215//1/2/

Alternatively,

split --lines=2 --numeric-suffixes=1 --suffix-length=1 --additional-suffix='.txt' main.txt 'file'

(some of these options may be specific to the GNU version of split).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.