Help with awk regular expression for RS record separator


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with awk regular expression for RS record separator
# 1  
Old 08-07-2017
Help with awk regular expression for RS record separator

Hi,

I'm using gawk to read a text file and count the sentences.
I want to use a record separator of a period, exclamation mark and a question mark.

The problem is that the file contains words like "Mr. Smith" so the periods in the appellation are tripping my record separator.

This is my code snippet:

Code:
BEGIN {
       RS="[.?!]"
}

This actually works fine until the file contains words like Mr. Smith.

So I tried like this:

Code:
RS="[^Mr.][.?!]"

Or like this:

Code:
RS="!Mr.[.?!]"

Or like this:

Code:
RS = "!(Mr.)[.?!]"

But I coudn't get any of them to work Smilie

Any ideas how I can do this?

Last edited by Don Cragun; 08-07-2017 at 11:10 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 08-07-2017
Hi, try
Code:
RS="[^\"Mr.\"][.?!]"

This User Gave Thanks to ctac_ For This Post:
# 3  
Old 08-07-2017
No it didn't work Smilie

It broke the file that was working.
I have a file without any "Mr." words.

By adding your suggestion, even the file without any "Mr." words stops working.

For example, it reads "one." as "on", "two." as tw, "three" as "thre".

This is the same as what was happening with my attempt below too.

---------- Post updated at 06:22 PM ---------- Previous update was at 06:18 PM ----------

Here's my full code:

Code:
#!/bin/bash

BEGIN {
       RS="[.?!;:]"           #       There is a problem with Mr. and Mrs. 
       maxWords=0
      }

{

if (maxWords<NF) 
     { 
        maxWords=NF
        longestSentence = $0
     }

for (i=1;i<=NF;i++) 
        a[$i]++

}

END{ 
      i=1;
      for(k in a) 
      {
        print i, k, a[k];
        i++;
      }
      print
      print("There were", NR, "sentences and the longest sentence had", maxWords, "words and there were", length(a), "unique words")
      print ("The longest sentence was:", longestSentence)
}

---------- Post updated at 06:25 PM ---------- Previous update was at 06:22 PM ----------

And the test file I'm using, which works fine as the code is above, but when I start changing the RS expression, even this file which has no "Mr." stops working.

----start of file----
Code:
one.
two two. 
three three three!
four four four four five five five five five.
six six six six six six?

------end of file---------

Last edited by Don Cragun; 08-07-2017 at 11:12 PM.. Reason: Add CODE tags.
# 4  
Old 08-07-2017
Code:
awk '
BEGIN {
   eol="[.?!;:]$" # There is a problem with Mr. and Mrs.
   maxWords=0
}

{
   if (NF>maxWords) {
      maxWords=NF
      longestSentence = $0
   }

   for (i=1;i<=NF;i++) {
      sub(eol "$", "", $i)
      a[$i]++
   }
}

END{
for(k in a) print ++ii, k, a[k];
print ""
print("There were", NR, "sentences and the longest sentence had", maxWords, "words and there were", length(a), "unique words")
print ("The longest sentence was:", longestSentence)
}
' infile

This User Gave Thanks to rdrtx1 For This Post:
# 5  
Old 08-07-2017
hmmm...interesting...isn't the record separator a newline now?
What if one sentence spans multiple newlines? Won't it be counted as two or more sentences?

Also, I don't understand exactly what the sub command is doing?

thank you
# 6  
Old 08-07-2017
If what you want to do is separate records at points where the last character on a line is a <period>, <question-mark>, or <exclamation-point>, you probably want to use:
Code:
RS="[.?!]$"

as rdrtx1 suggested.

Using RS="[.?!;:]" splits records on <period>, <question-mark>, <exclamation-point>, <semicolon>, and <colon> anywhere on a line.

Using RS="[^\"Mr.\"][.?!]" splits records on any two character sequence where the first character is not a <backslash>, <double-quote>, <uppercase-M>, <lowercase-r>, <period>, <backslash>, or <double-quote> and the second character is a <period>, <question-mark>, or <exclamation-point>. This ERE makes no sense to me for this use.

If, in addition to splitting when a set of characters is found at the end of a line, you also wanted to find that set of characters followed by two <space> characters (which is the common way of separating sentences in old fashioned text files), you could use:
Code:
RS="[.?!](  |$)"

Note that most of the above is talking about gawk and does not necessarily apply to other standards-conforming versions of awk. The standards state that it if more than one character is assigned to RS, it is unspecified whether RS is treated as a multi-character ERE that acts as the record separator or only the 1st character assigned to RS acts as the record separator. If RS is set to an empty string, the record separator is a sequence of two or more adjacent <newline> characters.

The default record separator is a <newline>. When RS is set to something other than a <newline>, <newline> (in addition to whatever FS is set to) is a field separator.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 7  
Old 08-08-2017
Hi Don,

I'm trying to capture an english sentence in a record.
This sentence could be very long and span multiple lines in a file.

My perfect record separator would be a period, exclamation point, question mark, semicolon or colon.

However, my code sees the word "Mr." it thinks that's the end of the sentence because of the period that is part of Mr. So I want it detect that "Mr." is NOT part of the record separator.

Semantically:
Not (Mr.) but ok with any of these [.!?;:]

But syntactically I don't know how to do this, I'm trying like this:
Code:
 RS = (^Mr. | [.!?;:])

But its not working?
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, output, and code segments (as required by forum rules).

Last edited by Don Cragun; 08-08-2017 at 02:15 AM.. Reason: Add CODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use string as Record separator in awk

Hello to all, Please some help on this. I have the file in format as below. How can I set the record separator as the string below in red "No. Time Source Destination Protocol Length Info" I've tried code below but it doesn't seem to... (6 Replies)
Discussion started by: cgkmal
6 Replies

2. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

3. Shell Programming and Scripting

awk - single quotes as record separator

How do I use single quotes as record separator in awk? I just couldn't figure that out. I know how to use single quotes as field separator, and double quotes as both field and record separator ... (1 Reply)
Discussion started by: locoroco
1 Replies

4. Shell Programming and Scripting

apply record separator to multiple files within a directory using awk

Hi, I have a bunch of records within a directory where each one has this form: (example file1) 1 2 50 90 80 90 43512 98 0909 79869 -9 7878 33222 8787 9090 89898 7878 8989 7878 6767 89 89 78676 9898 000 7878 5656 5454 5454 and i want for all of these files to be... (3 Replies)
Discussion started by: amarn
3 Replies

5. Shell Programming and Scripting

awk, string as record separator, transposing rows into columns

I'm working on a different stage of a project that someone helped me address elsewhere in these threads. The .docs I'm cycling through look roughly like this: 1 of 26 DOCUMENTS Copyright 2010 The Age Company Limited All Rights Reserved The Age (Melbourne, Australia) November 27, 2010... (9 Replies)
Discussion started by: spindoctor
9 Replies

6. Shell Programming and Scripting

awk - double quotes as record separator

How do I use double quotes as a record seperator in awk? (4 Replies)
Discussion started by: locoroco
4 Replies

7. Shell Programming and Scripting

Regular expression in AWK

Hello world, I was wondering if there is a nicer way to write the following code (in AWK): awk ' FNR==NR&&$1~/^m$/{tok1=1} FNR==NR&&$1~/^m10$/{tok1=1} ' my_file In fact, it looks for m2, m4, m6, m8 and m10 and then return a positive flag. The problem is how to define 10 thanks... (3 Replies)
Discussion started by: jolecanard
3 Replies

8. Shell Programming and Scripting

awk & cut record separator problem

Hi All, I've got some strange behaviour going on when trying to manipulate a file that contains spaces. My input file looks something like this: xxxxxxxxx,yyyy,sss sss sss,bbbbbbb If I use awk: When running from the command line I get: sss sss sss But when running from a... (7 Replies)
Discussion started by: pondlife
7 Replies

9. UNIX for Dummies Questions & Answers

regular expression and awk

I can print a line with an expression using this: awk '/regex/' I can print the line immediately before an expression using this: awk '/regex/{print x};{x=$0}' How do I print the line immediately before and then the line with the expression? (2 Replies)
Discussion started by: nickg
2 Replies

10. Shell Programming and Scripting

awk and regular expression

Ive got a file with words and also numbers. Bla BLA 10 10 11 29 12 89 13 35 And i need to change "10,29,89,25" and also remove anything that contains actually words... (4 Replies)
Discussion started by: maskot
4 Replies
Login or Register to Ask a Question

Featured Tech Videos