Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Splitting a file (csplit) !!!!

Status
Not open for further replies.

FORAND

Programmer
May 26, 2003
64
CA


Ok. I have a big file... There are a LOT of messages in it. I don't know how many, but I know that each message ends with "END OF MESSAGE". I need to find the 13923th one.

I think i should be using the CSPLIT command, but I just can't get it to work well... Can you help?

 
You can try something like this:
awk '
/END OF MESSAGE/{++msg}
msg==13922
msg>13922{exit}
' /path/to/bigfile

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
PHV ... you're an hell of awk writer but ...
a little use of:
if (blah blah blah ) {
do something
print something else
}
will make your awk script more understable for people like me
:)))
 
OK sbix, some comments:
/END OF MESSAGE/{++msg}
Count the number of messages read so far
msg==13922
We have read 13922 messages and not seen the end of the 13923th, so print the line (default action in awk when no {stuff} in the right of the pattern
msg>13922{exit}
The 13923th message is read an printed, so quit awk

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
No PHV, I wasn't speaking about comment but about your use of the "contract form" for each awk statement.
I.e.
you write "/END OF MESSAGE/ {++msg}" which is the shortest form of "if ( $0 ~ "/END OF MESSAGE/ ) {++msg}" or a shorter form of "( $0 ~ "/END OF MESSAGE/ ) {++msg}"
not speaking about "msg>13922{exit}" which should be something like "( msg == 13922 ) { print $0 }"
...yes ... you're an hell of awk writer
 
sbix,
use of the "contract form"
Yes, I'm lazzy with my fingers on keyboard :)
 
A variant of PH solution, with the message number passed in a variable :
[tt]
awk -v Msg=13923 '
/END OF MESSAGE/ {--Msg;next}
Msg==1
!Msg {exit}
' msg.txt
[/tt]
the not contracted form
[tt]
awk -v Msg=13923 '
$0 ~ /END OF MESSAGE/ {
Msg = Msg - 1;
next;
}
Msg == 1 {
print $0;
}
Msg == 0 {
exit;
}

' msg.txt
[/tt]


Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top