Unix Grep Conundrum - Not for Noobies


 
Thread Tools Search this Thread
Special Forums UNIX Desktop Questions & Answers Unix Grep Conundrum - Not for Noobies
# 8  
Old 01-20-2011
I'm not totally sure what you want to do in the end, so maybe I'm way off here. If so, sorry.

Given your snippet,
Quote:
...EXIT 16 FI JOBNAME=DA331Z_LD_CCC_BSE_TBLS_M PROCNAME1=VEH_INC_PART_UPDTBASE_M.SQL $ORACLE_SCRIPT_PROC/SQLSCRIPT_BATCH INC_PART2_UPDTBASE_M.SQL
SCRERROR=$? IF [ $SCRERROR -NE 0 ] THEN ECHO "ERROR: " INC_PART2_UPDTBASE_M.SQL" FAILED ...
Assume that is in a file called "bla.txt".
This:
Quote:
grep -ioP '(?<= )[[:alnum:]_]+=[[:alnum:]_]+\.SQL ' bla.txt | awk -F= '{print $1,$2}'
Will output:
PROCNAME1 VEH_INC_PART_UPDTBASE_M.SQL

If you had more scripts in it it would pick up each one (I think), unless the var/proc pair was broken up by a carriage return.

You can probably tune it quite a bit for your situation, whatever that is.
Brief explanation:
-i case insensitive match
-o just print the matched string, not the whole line like usual
-P use Perl regex's (for the lookbehind)
(<?<= ) lookbehind matches the space before your "proc" name but keeps it out of the -o print
[[:alnum:]_]+=[[:alnum:]_]+\.SQL match one or more alphanumeric or _ characters followed by "=" followed by one or more alphanumerics or _ followed by ".sql"

Then pipe it to awk to print the first and second fields delimited by "=".

You could do other things too like add a -r to the grep to search a whole directory structure recursively, or pipe everything to "sort -u" to eliminate duplicate results, even "sort | uniq -c | sort -n" to get numeric values for how often the same proc/var name pairs occur.

---------- Post updated Jan 20th, 2011 at 03:58 PM ---------- Previous update was Jan 19th, 2011 at 10:58 PM ----------

I just realized I should have posted that reply to owenian rather than methyl to be in the proper place in this thread....sorry...it's my first post @ unix.com.

Last edited by grepeverything; 01-20-2011 at 10:58 AM..
# 9  
Old 01-20-2011
grepeeverything thank you for responding. I can't get it to work but I will continue to play with it.

I did learn from your response as you provided a break down of the code and what each bit does.
# 10  
Old 01-20-2011
If you describe what "can't get it to work" means I might be able to help. I could imagine maybe your version of grep is different, e.g. maybe doesn't support the -P option.
# 11  
Old 01-20-2011
correct it does not support -P or -o
ie
Code:
[ku2q@dwsdv1]/proj/dw/devl/script_job/controlm/map_tables/work
$ grep -ioP '(?<= )[[:alnum:]_]+=[[:alnum:]_]+\.SQL ' cccda331z_ld_ccc_bse_tbls_m.sh | awk -F= '{print $1,$2}'
grep: illegal option -- o
grep: illegal option -- P
Usage: grep -hblcnsviw pattern file . . .
[ku2q@dwsdv1]/proj/dw/devl/script_job/controlm/map_tables/work

Thanks for your time grepeverything.

ps I thought I had hacked out a fix but when i ran a bigger test volume it was not the case ; )

Last edited by Scott; 01-20-2011 at 06:55 PM.. Reason: Code tags, please...
# 12  
Old 01-20-2011
hm, well you could just download (& build & install) gnu grep, which does have the -o and -P switches:http: / / directory.fsf.org / project / grep /
(remove the spaces)

Otherwise, this sort of works but is quite ugly:
Quote:
egrep -i ' [[:alnum:]_]+=[[:alnum:]_]+\.SQL ' bla.txt | awk '/[[:alnum:]_]+\.SQL/{for(i=1;i<=NF;++i)if($i~/[[:alnum:]_]+\.SQL/)print $i}' | grep '='
Getting a fuller-featured version of grep would be much cleaner.
# 13  
Old 01-20-2011
can't download as the company has policies

not working.

Code:
egrep -i '[[:alnum:]_]+=[[:alnum]_]+\.SQL ' cccda331z_ld_ccc_bse_tbls_m.sh

- does not pipe anything into awk

I will play around with it.
thanks

Last edited by Scott; 01-20-2011 at 06:57 PM.. Reason: Code tags
# 14  
Old 01-22-2011
You have a space missing at the beginning of the pattern and the ":" missing the named character class [:alnum:]

egrep -i '***SPACE***[[:alnum:]_]+=[[:alnum**COLON**]_]+\.SQL '
The space is somewhat optional but the colon not.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. OS X (Apple)

Help in explaining this echo conundrum.

OSX 10.12.3, default bash terminal. Consider this code and note it is calling 'sh' inside the code... #!/bin/sh echo '1\n2\n2\n3\n5' > /tmp/text hexdump -C /tmp/text /bin/echo '1\n2\n3\n4\n5' > /tmp/text hexdump -C /tmp/text Now view the interactive mode below, note the underlying shell is... (6 Replies)
Discussion started by: wisecracker
6 Replies

2. IP Networking

iptables conundrum

Ok, if youre reading this prepare yourself.(debian based os) so im trying to do this routing with ip tables, i need to forward/SNAT traffic from 192.168.111.1 to 10.10.10.250, the 192.x.x.x ips are being shoved into a honeyd like program called inetsim so its offline, 10.10.10.125 is connected... (3 Replies)
Discussion started by: Shocco
3 Replies

3. Red Hat

Physical Volume Create Conundrum

I want to start by saying I already resolved my issue but I want to understand why I am seeing what I am seeing. I have a server with a RAID controller two 500GB drives and six 600GB drives. The two 500GB drives are mirrored and have the OS installed on them. The six 600GB they wanted set as... (4 Replies)
Discussion started by: scotbuff
4 Replies

4. Shell Programming and Scripting

sudo scripts conundrum

hello; Got a problem running monitoring scripts using sudo ssh.. Mgmt decided to take away root sudoers access.. so most of the scripts ran as: sudo ssh $BOX ... Now I need to run them as: echo $my_pw | sudo -S -l my_user_id $BOX ... I tried this but not working.. Any wisdom/tricks... (3 Replies)
Discussion started by: delphys
3 Replies

5. Shell Programming and Scripting

Conundrum - Flexible way to strip extension

Hi, First post here. I have something that may prove to be difficult. I have the following files: Example1.0.0.tar.gz Example2.tar Example3.zip Example4.0.0.0.0.0.bzip2 I need to remove the file extensions and store as a variable so they look like this: Example1.0.0 Example2... (3 Replies)
Discussion started by: Spadez
3 Replies

6. UNIX for Dummies Questions & Answers

umask conundrum

Hi All, i was reading up on a umask question on this forum and have a question on this. the umask value on my home PC running on cygwin is 022. when i create a dir it defaults to permission 755, when i create a file it defaults to 644. Now it starts at 777 for dirs and 666 for files and... (1 Reply)
Discussion started by: Irishboy24
1 Replies

7. Shell Programming and Scripting

Sed pattern space/looping conundrum

Although my sed skills are gradually developing, thanks in large part to this forum, I'm having a hard time dealing with pattern space and looping, which I suspect is what I'll need a better handle on to figure out my current issue, which is converting a multi line file like this: ... (4 Replies)
Discussion started by: tiggyboo
4 Replies

8. UNIX for Advanced & Expert Users

Samba 2.2.5 smb.conf for noobies

Is there a generic smb.conf file that should work on all systems? Right now I am running Red Hat 7.3 and also have 3XP machines and 1 2000 pro. (1 Reply)
Discussion started by: GJC
1 Replies
Login or Register to Ask a Question