Reputation: 39
I have a file of data type (file.dat) with ASCII data in it and consists two columns. Also this file is sorted according to first column. I want to write a script in either shell or awk in such way that new file should be created for similar record from that sorted file. Suppose I have file consists of (Four records) such as given below...
100.00 321342
100.00 434243
100.00 543231
100.50 743893
Hence according to my problem here two files should be created. One file consists of top three records and other file consists of last record according to data in first column.
File 1 contains
100.00 321342
100.00 434243
100.00 543231
File 2 contains
100.50 743893
Upvotes: 1
Views: 44
Reputation: 4704
This script should do the work:
#!/bin/sh
exec 0<file.txt
makeit=yes
while read stp num; do
if [ -f "Timestep_$stp" ]; then
echo "File Timestep_$stp exists, exiting."
makeit=no
break
fi
done
if [ $makeit = yes ]; then
exec 0<file.txt
while read stp num; do
echo "$stp $num" >> Timestep_$stp
done
echo "Processing done."
fi
The first loop checks that no file exists, otherwise the result would be wrong.
Upvotes: 0
Reputation: 23804
your file
100.00 321342
100.00 434243
100.00 543231
100.50 743893
what you need
perl -a -nE 'qx( echo "$F[0] $F[1]" >> "Timestep_$F[0]" )' file
output is simply creates two file and name of one is Timestep_100.00 and name of other is Timestep_100.50 so it is separated by name of the first unique column. that's it.
$ cat Timestep_100.00
100.00 321342
100.00 434243
100.00 543231
and other file
$ cat Timestep_100.50
100.50 743893
Upvotes: 1