Help with regular expression

Hi All

We have a file with statements like below

 
 SELECT BLAH,BLAH,... FROM TABSCHEMA1.TABSCHEMA1 WITH UR
 SELECT BLAH,BLAH,... FROM TABSCHEMA2.TABSCHEMA2 WHERE BLAH=1 WITH UR
 .
 .
 . and so on
 

We want the word Anything after FROM<SPACE> and stop when the word Encounter Space.

Could some please help with a regular expression/sed with which I can achieve this?

 
 OUTPUT expected is
 TABSCHEMA1.TABSCHEMA1
 TABSCHEMA2.TABSCHEMA2
  

Thanks

 awk '{match($0,/FROM (\w+\.\w+)/,a); print a[1]}' infile
1 Like

Hello,

kindly use the code tags for any commands or codes as per forum rules.
Also following code may help you in same.

awk -F"FROM " '{gsub(/[[:space:]].*/,X,$2); print $2}' File_name

Output will be as follows.

TABSCHEMA1.TABSCHEMA1
TABSCHEMA2.TABSCHEMA2

Thanks,
R. Singh

1 Like

Try

sed 's/.*FROM \([^ ][^ ]*\).*/\1/' file

or

awk '/FROM/ {getline;print;next}' RS=' ' file
1 Like
sed -n 's/.*FROM \([^ ]*\).*/\1/p' file
sed '/.*FROM /!d; s///; s/ .*//' file
1 Like

Thank you for your responses.

I tried its working as expected.

I need to get that inside awk, can I use SED?

Can I use awk inside awk?

Yes, you can invoke sed inside awk , and yes, you can invoke awk inside awk ; but there is very seldom any reason to do so.

You have already been shown several ways to do what you requested in your original post. So, I assume you must want to do something more than you have shown us.

Please explain what you're trying to do. Show us some sample input and what output you're trying to produce (using CODE tags). Show us the code you have tried and explain what it isn't doing that you want it to do.

1 Like

Hi Don

Thanks for your response

We have input file like below

MONDAY###SELECT BLAH,BLAH,... FROM TABSCHEMA1.TABNAME1 WITH UR
 SUNDAY###SELECT BLAH,BLAH,... FROM TABSCHEMA2.TABNAME2 WHERE BLAH=1 WITH UR

Like

 cat <File name> | awk -F###<......>
 if Day = Monday
 then
 We need to get  TABSCHEMA1 and TABNAME1 in separate variables
 Later
 We use those variable again in other statement
 fi

we are doing an awk on each line and inside that awk , we are checking Day Condition and if that Matches we need to get TABSCHEMA1.TABNAME1 and TABSCHEMA2.TABNAME2

Thanks

Please stop giving us one tiny piece of what you're trying to do and tell us ALL of what you're trying to do. Help us help you!

awk -F 'FROM ' '
/MONDAY###/ && NF > 1 {
	if(match($2, /[.][^ ]* /)) {
		scheme = substr($2, 1, RSTART - 1)
		name = substr($2, RSTART + 1, RLENGTH - 1)
		printf("Line %d: scheme=%s, name=%s\n", NR, scheme, name)
	}
}' "input file"

does everything you've shown us so far, producing the output:

Line 1: scheme=TABSCHEMA1, name=TABNAME1

from your sample input file.

1 Like

That's not particularly clear, I'm afraid.

 cat <File name> | awk -F###<......>
 if Day = Monday
 then
 We need to get  TABSCHEMA1 and TABNAME1 in separate variables
 Later
 We use those variable again in other statement
 fi

Which parts of this are actually in the awk script?

Do you need the schema and table names only inside awk, or are you using them later in the shell script? Does your awk script need to produce anything apart from the table/schema names?

1 Like

Another way in Perl language. Just for alternative solution,

#!/usr/bin/perl -w
# Usage: perl regex.pl file.log
use strict;

my $file = shift;
my $fh = undef;

open ($fh, '<', $file) or die $!;

while (<$fh>)
{
 chomp;
 print "$1\n" if ($_ =~ /.*?FROM\s+(\S+)\s+.*/) 
}

__DATA__
SELECT BLAH,BLAH,... FROM TABSCHEMA1.TABSCHEMA1 WITH UR
SELECT BLAH,BLAH,... FROM TABSCHEMA2.TABSCHEMA2 WHERE BLAH=1 WITH UR

__RESULT__
TABSCHEMA1.TABSCHEMA1
TABSCHEMA2.TABSCHEMA2
1 Like

My SINCERE Thanks to in2nix4life and Don for their valuable suggestions that helped me to complete the code and get the desired results.

I am thankful to all other members and i hope my post helps others to fix their code.

Highly Appreciate the Forum Help.
:b: