parse through one text file and output many


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers parse through one text file and output many
# 1  
Old 02-17-2008
parse through one text file and output many

Hi, everyone

The input file pattern is like below:

Begin Object1

txt1

end
;


Begin Object2

txt2

end
;

...


I want to parse this one file into Object1.txt, Obeject2.txt... each contains one statement from 'Begin' to ';' , e.g. object1.txt contains:
Begin object1

txt1

end
;

---------------------------
Any thoughts?
also, Questions
1. any AWK or SED can search for a pattern across many lines?
2. how can I output many files?
3. Shall I move this thread to "Shell Programming and Scripting"?

Thank you in advanceSmilie
# 2  
Old 02-18-2008
If You use bash You could try something like this, very simple and probably lots of pitfalls. But since You are processing source code there are syntactic rules that can be expected to be followed. It simply checks for the Begin word and increments the file name index when encountered.
Code:
lakris@ubuntu:~/projekt/scripts$ cat projekt.txt 
Begin Object1
txt1
end
;
Begin Object2
txt2
end
;
Begin Object3
txt3
end
;
Begin Object4
txt4
end
;
lakris@ubuntu:~/projekt/scripts$ cat splitit.sh 
#!/bin/bash
cnt=0
while read line;do
  [[ "$line" =~ "Begin" ]] && cnt=$(($cnt+1))
  echo $line goes into Object$cnt.txt
done < projekt.txt
lakris@ubuntu:~/projekt/scripts$ ./splitit.sh 
Begin Object1 goes into Object1.txt
txt1 goes into Object1.txt
end goes into Object1.txt
; goes into Object1.txt
Begin Object2 goes into Object2.txt
txt2 goes into Object2.txt
end goes into Object2.txt
; goes into Object2.txt
Begin Object3 goes into Object3.txt
txt3 goes into Object3.txt
end goes into Object3.txt
; goes into Object3.txt
Begin Object4 goes into Object4.txt
txt4 goes into Object4.txt
end goes into Object4.txt
; goes into Object4.txt
lakris@ubuntu:~/projekt/scripts$

Change "goes into" to ">>" when You are confident that the output is what You want. It will append to any file with that name so You may want to remove any Object*.txt first.

/Lakris
# 3  
Old 02-18-2008
MySQL

Thank you very much, Lakris.Smilie I will try it out
# 4  
Old 02-18-2008
Oh, what if the input file like:
Begin aaaaa
txt1
end
;
Begin bbbbbb
txt2
end
;
Begin cccc
txt3
end
;
Begin ddd
txt4
end
;
# 5  
Old 02-18-2008
then the first Begin statement (aaaaa) ends up in Object1.txt the second (bbbbbb) in Object2.txt etc. Do You want to have them named Object-aaaaa.txt, Object-bbbbbb.txt etc?
Have a look at the while read line construct. You can split it up to read more than one variable... or You can treat line as an array.
Smilie
# 6  
Old 02-18-2008
Code:
awk '/^Begin/{close(f);f=$2".txt"}f{print>f}' input

# 7  
Old 02-18-2008
Quote:
Originally Posted by Lakris
then the first Begin statement (aaaaa) ends up in Object1.txt the second (bbbbbb) in Object2.txt etc. Do You want to have them named Object-aaaaa.txt, Object-bbbbbb.txt etc?
Have a look at the while read line construct. You can split it up to read more than one variable... or You can treat line as an array.
Smilie
No, I dont want them named Object-aaaaa.txt. It should be named as aaaaa.txt


Thanks
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to parse this file using awk and output in CSV format?

My source file looks like this: Cust-Number = "101" Cust-Name="Joe" Cust-Town="London" Cust-hobby="tennis" Cust-purchase="200" Cust-Number = "102" Cust-Name="Mary" Cust-Town="Newyork" Cust-hobby="reading" Cust-purchase="125" Now I want to parse this file (leaving out hobby) and... (10 Replies)
Discussion started by: Balav
10 Replies

2. Shell Programming and Scripting

Parse file for fields and specific text

I have a file of ~500,000 entries in the following: file.txt chr1 11868 12227 ENSG00000223972.5 . + HAVANA exon . gene_id "ENSG00000223972.5"; transcript_id "ENST00000456328.2"; gene_type "transcribed_unprocessed_pseudogene"; gene_status "KNOWN"; gene_name "DDX11L1"; transcript_type... (17 Replies)
Discussion started by: cmccabe
17 Replies

3. Shell Programming and Scripting

Parse and Join in a text file

I wanted to parse a text file and join in specific format. please suggest me how to get this done.. The output should be in fasta format which consists of lines starting with ID, PT, PA and Sequence. "//" the two slashes are dividing lines between two different sequences. Like... (10 Replies)
Discussion started by: empyrean
10 Replies

4. Shell Programming and Scripting

Trying to Parse Version Information from Text File

I have a file name version.properties with the following data: major.version=14 minor.version=234 I'm trying to write a grep expression to only put "14" to stdout. The following is not working. grep "major.version=(+)" version.properties What am I doing wrong? (6 Replies)
Discussion started by: obfunkhouser
6 Replies

5. Shell Programming and Scripting

cat file and parse output

Hello, I'm new to shell scripting and did a search on the forum to what I want to do but couldn't find anything. I have about 9 routers that outputs to 1 syslog file daily named cisco.year.mo.date.log ex: cisco.2009.05.11.log My goal is to make a parsing script that cats today's syslog... (2 Replies)
Discussion started by: jjrambar
2 Replies

6. Shell Programming and Scripting

parse csv file, sha1 hash and output

I have a file, not really a csv, but containing delineated data just the same. Lets call that file "raw_data.txt". It contains data in the format of company name:fein number like this: first company name:123456789 second company name:987654321 what i need to do is read this file, apply... (11 Replies)
Discussion started by: FreddyG
11 Replies

7. UNIX for Advanced & Expert Users

How to parse through a file and based on condition form another output file

I have one file say CM.txt which contains values like below.Its just a flat file 1000,A,X 1001,B,Y 1002,B,Z ... .. total around 4 million lines of entries will be in that file. Now i need to write another file CM1.txt which should have 1000,1 1001,2 1002,3 .... ... .. Here i... (6 Replies)
Discussion started by: sivasu.india
6 Replies

8. Shell Programming and Scripting

parse text file

I have a file that has a header followed by 8 columns of data. I want to toss out the header, and then write the data to another file with a different header and footer. I also need to grab the first values of the first and second column to put in the header. How do I chop off the header? ... (9 Replies)
Discussion started by: craggm
9 Replies

9. Shell Programming and Scripting

parse text file

i am attempting to parse a simple text file with multiple lines and four fields in each line, formatted as such: 12/10/2006 12:34:06 77 38 this is what i'm having problems with in my bash script: sed '1,6d' $RAWDATA > $NEWFILE #removes first 6 lines from file, which are... (3 Replies)
Discussion started by: klick81
3 Replies

10. UNIX for Dummies Questions & Answers

Parse Text file and send mails

Please help. I have a text file which looks something like this aaa@abc.com, c:FilePath\Eaaa.txt bbb@abc.com, c:FilePath\Ebbb.txt ccc@abc.com, c:FilePath\Eccc.txt ddd@abc.com, c:FilePath\Eddd.txt...so on I want to write a shell script which will pick up the first field 'aaa@abc.com' and... (12 Replies)
Discussion started by: Amruta Pitkar
12 Replies
Login or Register to Ask a Question