zp91
zp91

Reputation: 1

Read lines from a tab delimited text file to make a new file using bash

I have a large tab delimited text (.txt) file with multiple lines. What I would like to do is take each line from this txt file and make it its own individual file.

for example, if my text file were to look like this:

11111111
22222222
33333333

I would like to have three text files, one that reads "11111111", another that reads "22222222", and another that reads "33333333".

Upvotes: 0

Views: 517

Answers (2)

buydadip
buydadip

Reputation: 9407

How's this?

file=$1
i=0
while read line    // reads file line by line
do
echo $line > "file$i" //writes each line to a file until last line is reached
((i++))
done < aline.txt

if the contents of file is the same as the one you provided, you will have the following files:

file0 --> 11111111
file1 --> 22222222
file3 --> 33333333

Upvotes: 1

Tom Fenech
Tom Fenech

Reputation: 74595

The most obvious way to do this would be to use split:

split -l 1 file out

This splits file into separate files, outaa, outab and outac, each containing one line of the input file.

The default length of the suffixes is 2 but you can change it using the -a switch. For example,
split -a 1 -l 1 file out would create files outa, outb and outc instead.

Take a look at man split to see more options that you can use to control the output.

Upvotes: 2

Related Questions