LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-26-2012, 10:34 PM   #1
soupmagnet
LQ Newbie
 
Registered: Sep 2012
Posts: 29

Rep: Reputation: Disabled
Pull sections of text from a file and create a new file?


I'm not quite sure how to explain this, but here it goes anyway.

I have rather large text files that are formatted like so:

Code:
FIELD 1
1 This is the first string
2 This is the second string
3 This is the third string
4 And so on
5 And so forth

FIELD 2
1 And so on
2 And so forth
3 Each field having an indeterminate number of strings
4 And there may be an indeterminate number of fields

FIELD 3
1 The only usable patterns (that I see) are
2 The titles of each field are numbered (anywhere between 1 and 150)
3 Each string in a field is numbered (also anywhere between 1 and 150)
4 And there are empty lines between each field
I need to create a new file for every single field in the original file. Each file needs to have the information and preferably the title of each individual field. And I would need to script this because I have several hundred of these "original" files.


Is this even possible?
 
Old 12-27-2012, 02:43 AM   #2
anon237
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Is this what you are looking for:
Code:
awk 'BEGIN { RS="" ; FS="\n"} { outfile = $1 ; for ( i=1;i<=NF;i++) print $i > outfile }' infile
 
1 members found this post helpful.
Old 12-27-2012, 04:35 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I've been waiting for someone to ask a question like this.

Code:
csplit input.txt '/^FIELD/' '{*}'
csplit is one of the lesser-known coreutils. It has several options for formatting the output file names and such too.

The only minor drawback in this case is that the empty lines between the fields are not removed, but still show up at the end of the files. But that could be corrected fairly easily if needed with sed, or perhaps grep -v.


Edit: You can remove the trailing blank lines with this after you're done:

Code:
for fname in xx* ; do
	printf '%s\n' '/^$/d' 'w' | ed -s "$fname"
done

Last edited by David the H.; 12-27-2012 at 04:48 PM. Reason: as stated
 
1 members found this post helpful.
Old 12-27-2012, 04:46 PM   #4
soupmagnet
LQ Newbie
 
Registered: Sep 2012
Posts: 29

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
Is this what you are looking for:
Code:
awk 'BEGIN { RS="" ; FS="\n"} { outfile = $1 ; for ( i=1;i<=NF;i++) print $i > outfile }' infile
Works perfectly, thank you.

Now to figure out what it all means. It looks like I'll have to study up on 'awk' some more.

Quote:
Originally Posted by David the H. View Post
I've been waiting for someone to ask a question like this.

Code:
csplit input.txt '/^FIELD/' '{*}'
csplit is one of the lesser-known coreutils. It has several options for formatting the output file names and such too.

The only minor drawback in this case is that the empty lines between the fields are not removed, but still show up at the end of the files. But that could be corrected fairly easily if needed with sed, or perhaps grep -v.


Edit: You can remove the trailing blank lines with this after you're done:

Code:
for fname in xx* ; do
	printf '%s\n' '/^$/d' 'w' | ed -s "$fname"
done
Very slick, and both are very viable options.

Thanks again to you both for your help.

Last edited by soupmagnet; 12-27-2012 at 04:55 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cannot create regular file ...: text file busy laode Linux - General 4 11-27-2012 01:56 AM
[SOLVED] Search text file for records in another text file and pull extra data over to new Adzrules Linux - Newbie 21 11-03-2012 11:53 AM
How do I create a duplicate file of a text file in PHP? puppymagic Programming 1 11-11-2010 01:56 AM
bash script to create text in a file or replace value of text if already exists knightto Linux - Newbie 5 09-10-2008 11:13 PM
text match pipe to file then delete from original text file create new dir automatic tr1px Linux - Newbie 6 09-10-2008 09:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration