Trying to create files from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trying to create files from a file
# 1  
Old 11-02-2004
Question Trying to create files from a file

Hello to everyone,
I am new to UNIX and I am having a problem. I have a file full of information, and need to create diferent files. I have to split the file whenever I find a sentence in the original one. This sentence is always the same (JOB ^....). As you can see I need to read a file, and whenever I find a sentence create a new file, ending whenever I find that sentence again. I have read about sed and awk, but don't know how to use it, or if it is the best option. I need to split the file because the next step in the process is not capable of doing it.
An example of the original file would be:
Job^1
a
b
c
Job^2
d
e
f

And need to end with a two files. The first file:
Job^1
a
b
c
A second file:
Job^2
d
e
f

Any help anyone could give me would be very appreciated.
Thanks a lot.
Jordi

Last edited by jordimirobruix; 11-02-2004 at 08:34 AM..
# 2  
Old 11-02-2004
Something like
Code:
awk '{if ($0 ~ /^Job\^/) {c=$0;print $0 >> c} else {print $0 >> c} }' data

This will create files as you want, Job^1, Job^2, etc.

Cheers
ZB
# 3  
Old 11-02-2004
Thanks a lot
# 4  
Old 11-02-2004
One more question. The name of the original file is XXXX.dat, and I want them to be named XXXX1.dat, XXXX2.dat and so on. How can I do that?
Thanks a lot,
Jordi
# 5  
Old 11-02-2004
A little script such as
Code:
#!/bin/sh

awk 'BEGIN {c=0}
     { if ($0 ~ /^Job\^/)
        {f="XXXX"++c".DAT";print $0 >> f}
       else {print $0 >> f}
     }' XXXX.DAT

Cheers
ZB
# 6  
Old 11-02-2004
an alterntive way :



ext=`echo $1 | cut -d"." -f 2`
file=`echo $1 | cut -d"." -f 1`

while read line
do
echo $line | grep "^Job"
if test $? -eq 0
then
count=`echo $line | cut -d"^" -f 2`
filenam=$file$count.$ext
echo $line >> $filenam
else
echo $line >> $filenam
fi
done < $1
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting a text file into smaller files with awk, how to create a different name for each new file

Hello, I have some large text files that look like, putrescine Mrv1583 01041713302D 6 5 0 0 0 0 999 V2000 2.0928 -0.2063 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0 5.6650 0.2063 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0 3.5217 ... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

2. UNIX for Beginners Questions & Answers

Create file based on data from two other files

I have looked through several threads regarding merging files with awk and attempted using join however have been unsuccessful likely as I do not fully understand awk. What I am attempting is to take a csv file which could be between 1 and 15,000 lines with 5 colums and another csv file that will... (4 Replies)
Discussion started by: cdubu2
4 Replies

3. Shell Programming and Scripting

How to compare 2 files and create a result file with unmatched lines from first file.?

HI, I have 2 text files. file1 and file2. file1.txt (There are no duplicates in this file) 1234 3232 4343 3435 6564 6767 1213 file2.txt 1234,wq,wewe,qwqw 1234,as,dfdf,dfdf 4343,asas,sdds,dsds 6767,asas,fdfd,fdffd I need to search each number in file1.txt in file2.txt's 1st... (6 Replies)
Discussion started by: Little
6 Replies

4. UNIX for Dummies Questions & Answers

How to create a .csv file from 2 different .txt files?

Hi, I need to create a .csv file from information that i have in two different tab delimited .txt file. I just want to select some of the columns of each .txt file and paste them into a .cvs file. My files look like: File 1 transcript_id Seq. Description Seq. Length ... (2 Replies)
Discussion started by: alisrpp
2 Replies

5. Shell Programming and Scripting

How to create multiple files from one file?

Hi everyone! I usually get large files with different groups of entries; for example, each line starts with A, B, C, D, or E. Is there a way to separate all these entries and then write them in 5 different files with one awk program? Thank you! (4 Replies)
Discussion started by: Avro1986
4 Replies

6. Shell Programming and Scripting

Create different files from input file.

I have file A.txt File A 1,Hi,234 2,Hello,345 1,Kal,980, 9,KJ,098 2,de,098 .. ... I have more then 600 lines... I want separate the files as per 150 line. First 150 lines ----File A1.xtx Second 150 lines ---File A2.txt Third 150 lines----File A3.txt. ... (4 Replies)
Discussion started by: asavaliya
4 Replies

7. UNIX for Dummies Questions & Answers

create table file from different files with index

Hi, I've several files with two collumns, where first collumn can be used as index. filename1 and filename2 how to create a file I should start with cat all files and extract first collumn to create an index? (4 Replies)
Discussion started by: sargotrons
4 Replies

8. Shell Programming and Scripting

Create files based on second column of a file

Hi All, I have a file which looks like this: 234422 1 .00222 323232 1 3232 32323 1 0.00222 1234 2 1211 2332 2 0.9 233 3 0.883 123 3 45 As you can see, the second column of the file is already sorted which I did using sort command. Now, I want to create files based on the second... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

9. Shell Programming and Scripting

Merging files to create CSV file

Hi, I have different files of the same type, as: Time: 100 snr: 88 perf: 10 other: 222 Each of these files are created periodically. What I need to do is to merge all of them into one but having the following form: (2 Replies)
Discussion started by: Ravendark
2 Replies

10. UNIX for Advanced & Expert Users

Inserting variables from 2 files to create 1 file

Hi, I have 2 files which are as follows: Num: 548983 748932 783928 547383 839284 Grp: 2389 9873 8493 7382 8493 I need to make an output file using both of these files. (1 Reply)
Discussion started by: rochitsharma
1 Replies
Login or Register to Ask a Question