Looking for pattern next to variable string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Looking for pattern next to variable string
# 1  
Old 07-19-2010
Question Looking for pattern next to variable string

Hi,

I need to help to find the way to get specific data from a log, an unknow string beside a string that is the same always, for example:

Input file:
Code:
ctmdefine -TASKTYPE COMMAND -TABLE PRUEBAS_BIMLAR -JOBNAME PRUEBAJOB12 \
-GROUP PRUEBAS_BIM2 -APPLICATION PRUEBAS_BIM2 -NODEGRP gtwtran -MEMLIB "/usr/bin" \
-MEMNAME "PRUEBAJOB12" -OWNER agt613 -AUTHOR "MARIO_CORONA" -CMDLINE "sleep 90;df -k" \
-MAXWAIT 3 -WEEKDAYS "4" -MONTH ALL Y -CAL_ANDOR OR -INCOND PRUEBAJOB11-TO-PRUEBAJOB12 ODAT AND \
-INCOND PRUEBA_LAR_BIM ODAT AND -OUTCOND PRUEBAJOB11-TO-PRUEBAJOB12 ODAT DEL -OUTCOND PRUEBAJOB12-TO-TEST_BIM-LAR ODAT ADD -OUTCOND PRUEBA_LAR_BIM ODAT DEL

ctmdefine -TASKTYPE COMMAND -TABLE CR-VENETDIA-T39 -JOBNAME VNETNP0204 \
-GROUP NETCASH-P -APPLICATION NET-VE-DESAGRETAG80 -NODEGRP wascash -MEMLIB "/pr/bdfv/batch/multipais/scrt" \
-MEMNAME "VNETNP0204" -OWNER dbstlne -AUTHOR "MARIO_CORONA" -TIMEFROM 0700 \
-TIMEUNTIL "0650" -CMDLINE "/pc/bdfv/batch/multipais/scrt/bdfvj002.sh 0023 0094 RETAQ80 N mx B LOC /repo/xcomcnnc/" \
-CYCLIC Y -INTERVAL 00001M -INTERVALFROM END -DESCRIPTION "Desagrupaci�n Local despu�de distribuidos" -WEEKDAYS "1,2,3,4,5,6,0" -MONTH ALL Y \
-CAL_ANDOR OR -INCOND VNETNP0203-TO-VNETNP0204-A ODAT AND -INCOND VNETNP0203-TO-VNETNP0204-B ODAT AND -OUTCOND VNETNP0203-TO-VNETNP0204-A ODAT DEL \
-OUTCOND VNETNP0203-TO-VNETNP0204-B ODAT DEL

ctmdefine -TASKTYPE COMMAND -TABLE CR-VENETDIA-T35 -JOBNAME VNETNP0175 \
-GROUP NETCASH-P -APPLICATION NET-VE-DESAGFIDE2 -NODEGRP wascash -MEMLIB "/pr/bdfv/batch/multipais/scrt" \
-MEMNAME "VNETNP0175" -OWNER dbstlne -AUTHOR "MARIO_CORONA" -TIMEFROM 0700 \
-TIMEUNTIL "0650" -CMDLINE "/pc/bdfv/batch/multipais/scrt/bdfvj002.sh 0023 0082 FIDEICO2 N co B LOC /repo/xcomcnnc/" \
-CYCLIC Y -INTERVAL 00001M -INTERVALFROM END -DESCRIPTION "Desagrupacion Local" -WEEKDAYS "1,2,3,4,5,6,0" -MONTH ALL Y \
-CAL_ANDOR OR -INCOND VNETNP0169-TO-VNETNP0175-A ODAT AND -INCOND VNETNP0169-TO-VNETNP0175-B ODAT AND -OUTCOND VNETNP0169-TO-VNETNP0175-A ODAT DEL \
-OUTCOND VNETNP0169-TO-VNETNP0175-B ODAT DEL


I look for pattern -INCOND, it's easy with a grep or sed, but how can i get the next string in the same line?

I need something like that:
Code:
PRUEBA_LAR_BIM
VNETNP0203-TO-VNETNP0204-A, VNETNP0203-TO-VNETNP0204-B
VNETNP0169-TO-VNETNP0175-A, VNETNP0169-TO-VNETNP0175-B

As you can see, string INCOND can be once or more times in the same line.

Any idea? Smilie

Sorry for my english, I hope i'm clear enought Smilie

Last edited by Franklin52; 07-19-2010 at 05:30 PM.. Reason: Please use code tags
# 2  
Old 07-19-2010
Tools Assuming no ~ characters...

Could try something like (the 2nd command; 1st just to show how it works):
Code:
C:\cygwin\tmp>sed 's/-INCOND /-INCOND~/g' <mgc.txt | tr " " "\n" | grep "~"
-INCOND~PRUEBAJOB11-TO-PRUEBAJOB12
-INCOND~PRUEBA_LAR_BIM
-INCOND~VNETNP0203-TO-VNETNP0204-A
-INCOND~VNETNP0203-TO-VNETNP0204-B
-INCOND~VNETNP0169-TO-VNETNP0175-A
-INCOND~VNETNP0169-TO-VNETNP0175-B

Code:
C:\cygwin\tmp>sed 's/-INCOND /-INCOND~/g' <mgc.txt | tr " " "\n" | grep "~" | cut -d"~" -f2
PRUEBAJOB11-TO-PRUEBAJOB12
PRUEBA_LAR_BIM
VNETNP0203-TO-VNETNP0204-A
VNETNP0203-TO-VNETNP0204-B
VNETNP0169-TO-VNETNP0175-A
VNETNP0169-TO-VNETNP0175-B

Note, my script found an existence of INCOND before your first noted example!

Last edited by joeyg; 07-19-2010 at 05:45 PM.. Reason: To correct a missing parameter
This User Gave Thanks to joeyg For This Post:
# 3  
Old 07-19-2010
With Perl:

Code:
perl -nle'
  /-INCOND/ and print join ",", /-INCOND\s+(\S+)/g
  ' infile

This User Gave Thanks to radoulov For This Post:
# 4  
Old 07-19-2010
MySQL

Quote:
Originally Posted by joeyg
Could try something like (the 2nd command; 1st just to show how it works):
Code:
C:\cygwin\tmp>sed 's/-INCOND /-INCOND~/g' <mgc.txt | tr " " "\n" | grep "~"
-INCOND~PRUEBAJOB11-TO-PRUEBAJOB12
-INCOND~PRUEBA_LAR_BIM
-INCOND~VNETNP0203-TO-VNETNP0204-A
-INCOND~VNETNP0203-TO-VNETNP0204-B
-INCOND~VNETNP0169-TO-VNETNP0175-A
-INCOND~VNETNP0169-TO-VNETNP0175-B

Code:
C:\cygwin\tmp>sed 's/-INCOND /-INCOND~/g' <mgc.txt | tr " " "\n" | grep "~" | cut -d"~" -f2
PRUEBAJOB11-TO-PRUEBAJOB12
PRUEBA_LAR_BIM
VNETNP0203-TO-VNETNP0204-A
VNETNP0203-TO-VNETNP0204-B
VNETNP0169-TO-VNETNP0175-A
VNETNP0169-TO-VNETNP0175-B

Note, my script found an existence of INCOND before your first noted example!
Ahhh that's true, thank you for your help
# 5  
Old 07-19-2010
Code:
$ grep -o "\-INCOND [[:graph:]]*" urfile |awk '{print $2}'
PRUEBAJOB11-TO-PRUEBAJOB12
PRUEBA_LAR_BIM
VNETNP0203-TO-VNETNP0204-A
VNETNP0203-TO-VNETNP0204-B
VNETNP0169-TO-VNETNP0175-A
VNETNP0169-TO-VNETNP0175-B

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check if string variable is a subset of another string variable

Below is my ksh shell script where I need to check if variable fileprops is a subset of $1 argument. echo "FILE PROPERTY: $fileprops" echo "PARAMETER3: $1" if ; then echo "We are Good. $line FILE is found to be INTACT !! " else echo... (2 Replies)
Discussion started by: mohtashims
2 Replies

2. Shell Programming and Scripting

How to delete all lines before a particular pattern when the pattern is defined in a variable?

I have a file Line 1 a Line 22 Line 33 Line 1 b Line 22 Line 1 c Line 4 Line 5 I want to delete all lines before last occurrence of a line which contains something which is defined in a variable. Say a variable var contains 'Line 1', then I need the following in the output. ... (21 Replies)
Discussion started by: Soham
21 Replies

3. Shell Programming and Scripting

PHP - Regex for matching string containing pattern but without pattern itself

The sample file: dept1: user1,user2,user3 dept2: user4,user5,user6 dept3: user7,user8,user9 I want to match by '/^dept2.*/' but don't want to have substring 'dept2:' in output. How to compose such regex? (8 Replies)
Discussion started by: urello
8 Replies

4. Shell Programming and Scripting

Pattern match exclusive return pattern/variable

I have an application(Minecraft Server) that generates a logfile live. Using Crontab and screen I send a 'list' command every minute. Sample Log view: 2013-06-07 19:14:37 <Willrocksyea1> hello* 2013-06-07 19:14:41 <Gromden29> hey 2013-06-07 19:14:42 Gromden29 lost connection:... (1 Reply)
Discussion started by: gatekeeper258
1 Replies

5. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

6. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

7. UNIX for Dummies Questions & Answers

Comparing a String variable with a string literal in a Debian shell script

Hi All, I am trying to to compare a string variable with a string literal inside a loop but keep getting the ./testifstructure.sh: line 6: #!/bin/sh BOOK_LIST="BOOK1 BOOK2" for BOOK in ${BOOK_LIST} do if then echo '1' else echo '2' fi done Please use next... (1 Reply)
Discussion started by: daveu7
1 Replies

8. UNIX for Dummies Questions & Answers

Append a string on the next line after a pattern string is found

Right now, my code is: s/Secondary Ins./Secondary Ins.\ 1/g It's adding a 1 as soon as it finds Secondary Ins. Primary Ins.: MEDICARE B DMERC Secondary Ins. 1: CONTINENTAL LIFE INS What I really want to achieve is having a 1 added on the next line that contain "Secondary Ins." It... (4 Replies)
Discussion started by: newbeee
4 Replies

9. Shell Programming and Scripting

PERL : pattern matching a string stored in a variable

I have two variables, my $filename = "abc_yyyy_mm_dd.txt"; my $filename1 = " abc_2011_11_07.txt"; I need to perform some operations after checking if $filename has $filename1 in it i have used the below code, if($filename =~ /^$filename1/) { ---- -- } (2 Replies)
Discussion started by: irudayaraj
2 Replies

10. Shell Programming and Scripting

How to assign the Pattern Search string as Input Variable

guys, I need to know how to assing pattern matched string as an input command variable. Here it goes' My script is something like this. ./routing.sh <Server> <enable|disable> ## This Script takes an input <Server> variable from this line of the script ## echo $1 | egrep... (1 Reply)
Discussion started by: raghunsi
1 Replies
Login or Register to Ask a Question