Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split each line in a text file into new text files via command line

Tags:

bash

shell

split

I have a text file with 30 lines. I would like to split it up by line where each line will be in a new text file.

I used this command in the command line but didnt get any useful output except the exact same 30 line file but just renamed as "xaa" :

split -l 1 mytextfile.txt

Am i doing something wrong here?

like image 934
jxn Avatar asked Jan 10 '23 20:01

jxn


2 Answers

You're using the -l argument incorrectly. The value you pass in with -l is the number of lines to put into each piece. So you're taking a 30 line file and splitting into ... a single 30 line file.

You need to do split -l 1 mytextfile.txt

like image 98
Mike Holt Avatar answered Jan 20 '23 06:01

Mike Holt


Try awk

cat  mytextfile.txt  |  awk '{ print $0 > "my_splittet_textfile_"NR".txt"}'
like image 42
drkunibar Avatar answered Jan 20 '23 04:01

drkunibar