textfile into 2 pieces, seperate by newlines.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting textfile into 2 pieces, seperate by newlines.
# 1  
Old 09-22-2008
textfile into 2 pieces, seperate by newlines.

Hi,

I've a textfile like this:

PHP Code:
blablablalblalbla
blablablalblalbla
blablablalblalbla
blablablalblalbla
blablablalblalbla


important text
important text
important text
important text
... 
I need the important text, but I dont know how to split the file into 2 pieces. Between these pieces are always 3 newlines.

Any ideas?
# 2  
Old 09-22-2008
You can use awk, or shell, or other tools - here is shell example
Code:
#!/bin/ksh
cnt=0
while read record
do
   if [[ $cnt -gt 2 ]] ; then
       echo "$record"
       continue
   fi
   if [[ ${#record} -eq 0 ]] ; then
       cnt=$(( cnt + 1 ))
   else
       cnt=0
   fi
done < oldfile > newfile

# 3  
Old 09-22-2008
Code:
perl -0777 -pe 's/.*?\n\n\n//ms' oldfile >newfile

# 4  
Old 09-22-2008
thx a lot
# 5  
Old 09-23-2008
Hi,

there is a new problem. Some files have three newlines in the unimportant parts. like this:

PHP Code:
blablablalblalbla 
blablablalblalbla 



blablablalblalbla 



blablablalblalbla 
blablablalblalbla 



important text 
important text 
important text 
important text 
... 
But the important text is after the last three newlines, how can I extract it?
# 6  
Old 09-23-2008
Code:
csplit file '/important text/'

# 7  
Old 09-23-2008
Theres no difference between "blabla" and "important text". I cant get a pattern for csplit. Any other ideas?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to transpose pieces of data in a column to multiple rows?

Hello Everyone, I am very new to the world of regular expressions. I am trying to use grep/sed for the following: Input file is something like this and there are multiple such files: abc 1 2 3 4 5 ***END*** abc 6 7 8 9 ***END*** abc 10 (2 Replies)
Discussion started by: shellnewuser
2 Replies

2. Shell Programming and Scripting

How to separate sorte different characters from one textfile and copy them in a new textfile?

My first post, so don't kill me :) Say i open some textfile with some example like this. on the table are handy, bread and wine Now i know exactly what is in and i want to separate and sorted it in terminal to an existing file with another 2 existing lines in like this: table plane ... (3 Replies)
Discussion started by: schwatter
3 Replies

3. Shell Programming and Scripting

Assembling the Pieces of a Regular Expression

Hello all. I'm scripting in ksh and trying to put together a regular expression. I think my logic is sound, but I'm doing the head-against-the-wall routine while trying to put the individual pieces together. Can anybody lend some suggestions to the below problem? I'm taking a date in the... (2 Replies)
Discussion started by: Michael_K
2 Replies

4. Shell Programming and Scripting

Grab 2 pieces of data within a file

I am a newbie and what I have is a captured file of content. I want to be able to grab 2 pieces of data, multiple times and print them to the screen. DataFile owner: locke user: fun data size: 60 location: Anaheim owner: david user: work data size: 80 location: Orange my script... (2 Replies)
Discussion started by: greglocke
2 Replies

5. Shell Programming and Scripting

Split a 30GB XML file into 16 pieces

I have a 30 GB XMl file which looks like this: <page> <title>APRIL</title> .........(text contents that I need to extract and store in 1.dat including the <title> tag) </page> <page> <title>August</title> ....(text contents that I need to store in 2.dat including the <title> tag) </page>... (13 Replies)
Discussion started by: shoaibjameel123
13 Replies

6. Shell Programming and Scripting

Line gets splitted into 99 char long pieces

Hello, I have a script like follows. It reads a file, and with every line, it calls an "adapter" program, which just puts the line into MQ. When I run this locally, it works fine. When I run this on our company's server, one line is split into several pieces (99 characters long) and "adapter"... (1 Reply)
Discussion started by: Adamm
1 Replies

7. Shell Programming and Scripting

How do I split file into pieces with PERL?

How do I split file into pieces with PERL? IE file.txt head 1 2 3 4 end head 5 6 7 8 9 end n so on (7 Replies)
Discussion started by: 3junior
7 Replies

8. Shell Programming and Scripting

Random pieces of number

Hello folks, i have number for example 10 and i want to divide into 4 random pieces that may be (6+2+1+1). How can i do this via script i have random number 234951 and i want to divide into 31 pieces. (6 Replies)
Discussion started by: learnbash
6 Replies

9. Shell Programming and Scripting

paste broken pieces of a record into one line

I am working with a file that has some of the records broken into several lines, I need paste the pieces back into one line. All records should start with numeric id, and presently all lines do start with id. The last field of the record is "telephone", so I need to make sure that each line starts... (1 Reply)
Discussion started by: migurus
1 Replies

10. AIX

AIX Bits and Pieces

There are quite many nifty little tricks, which can make life easier for the AIX administrator carrying out typical tasks in his job. I'll start the collection, suggestions will be highly welcome and added here when they are messaged to me. No, i don't claim to have found out myself what stands... (0 Replies)
Discussion started by: bakunin
0 Replies
Login or Register to Ask a Question