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!

asterik as record seperator

Status
Not open for further replies.

mfvd

Technical User
Mar 12, 2004
7
US
I have a multiple line file with each record seperated by a line of 45 asteriks. I am trying to print just specific fields/lines within each record. Some fields within the record may have an asterik in it. According to the O'reilly book, awk only pays attention to the first char. of the RS value. How can I accomplish this?

I tried, awk file:

BEGIN { FS = "\n"; RS = "\*" }

{print $1, $3, $4, $6 }

Tried using "*" without the escape char., using all 45 astericks "\*+". Nothing seems to work.
thanks.
 
Can you please post examples of input data and expected result ?

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Something like this ?

Code:
awk '
BEGIN { FS="\n" ; RS="\n\\*+\n" }
{ print NR,NF,$0}
' input_file




Jean Pierre.
 
Input file looks like this:

**********************************************
Thu Mar 11 22:50:33 2004
ns1.xx.com <ipaddress>
To: jwile@xx.com
From: ghjfe@r5rd5r.com
MessageID: <KXBHPBRWBYUYBODUZNDH@yahoo.com>
Subject: Need pain pills?
Hits: 7.155 BAYES_90,MY_body_VIAGRA,PRIORITY_NO_NAME
**********************************************
Thu Mar 11 22:50:38 2004
ns1.xx.com <ipaddress>
To: tro@xx.com
From: Kayleeqlqb@id.ru
MessageID: <uqfbiimsg@id.ru>
Subject: Important for advancement
Hits: 9.176 BAYES_99,DATE_IN_PAST_96_XX,HTML_MESSAGE,HTML_TAG_BALANCE_BODY,MIME_HTML_NO_CHARSET,MIME
_HTML_ONLY
**********************************************
 
Thanks, but the posted code using,

{ FS="\n" ; RS="\n\\*+\n" }

treats each line as a seperate record.
I just posted an excerpt from my input file.
 
Maybe

{j++}
j==1 || j==3 || j==4 ||j==6
/^\**$/{j=0}

CaKiwi
 
Or perhaps

{j++}
/^\**$/{j=0}
j==1 || j==3 || j==4 ||j==6

CaKiwi
 
Naive method:
awk '
$NF~/^200[4-9]$/
$1=="To:"
$1=="From:"
$1=="Subject:"
' </path/to/inputfile >output

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Code:
awk '
{
  gsub(/\n/," ") ;
  print "Record=" NR " Fields=" NF " Record:\n",$0
}
' RS='\n*\\*\{45,\}\n*' input_file

With gawk, you need to add the option '--re-interval'.

With you example of input data, the result is :
[tt]
Record=1 Fields=0 Record:

Record=2 Fields=20 Record:
Thu Mar 11 22:50:33 2004 ns1.xx.com <ipaddress> To: jwile@xx.com From: ghjfe@r
5rd5r.com MessageID: <KXBHPBRWBYUYBODUZNDH@yahoo.com> Subject: Need pain pills?
Hits: 7.155 BAYES_90,MY_body_VIAGRA,PRIORITY_NO_NAME
Record=3 Fields=21 Record:
Thu Mar 11 22:50:38 2004 ns1.xx.com <ipaddress> To: tro@xx.com From: Kayleeql
qb@id.ru MessageID: <uqfbiimsg@id.ru> Subject: Important for advancement Hits: 9
.176 BAYES_99,DATE_IN_PAST_96_XX,HTML_MESSAGE,HTML_TAG_BALANCE_BODY,MIME_HTML_NO
_CHARSET,MIME _HTML_ONLY
[/tt]

Jean Pierre.
 
Tested code:

Code:
BEGIN{FS="\n"}

/^\*\*\*\*+/ {
  if (lines)
  { $0=lines
    OFS="\n" ; NF+=0; OFS=" "
    print $1, $3, $4, $6
    lines=""
  }
  next
}
{lines=lines $0 "\n"}

Let me know whether or not this helps.
 
I get:

awk: can't set $0
record number 9

When I run the code posted by futurelet (Programmer).
 
Change: one line can be eliminated from the above code.

Code:
BEGIN{FS="\n"}

/^\*\*\*\*+/ {
  if (lines)
  { $0=lines
    print $1, $3, $4, $6
    lines=""
  }
  next
}
{lines=lines $0 "\n"}

Let me know whether or not this helps.
 
Save it in a file named, say, "asterisks.awk" and run it with
[tt]awk -f asterisks.awk data_file[/tt].
 
mfvd, if you have nawk (or gawk) use it.
awk: can't set $0
This error message is typically issued by an old fashionned awk version.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Same error with the one line eliminated:

awk: can't set $0
record number 9
 
Yes, did not work on my solaris 8 system, but when run on a linux system, ran great. thanks.
 
on my solaris 8
No nawk on this platform ?

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Yes, nawk on my solaris 8 system does works.
thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top