Regex in sed to find specific pattern and assign to variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regex in sed to find specific pattern and assign to variable
# 1  
Old 12-30-2013
Regex in sed to find specific pattern and assign to variable

# 2  
Old 12-30-2013
Hello,

Could you please post your complete input and expected output.

NOTE: Also please use code tags while posting commands please.


Thanks,
R. Singh
# 3  
Old 12-30-2013
Hi

Thanks for replying

The input looks like below
Code:
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  SEV=NONE                                                                 
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
                                                                           
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  TFWD=N;OV=N;~                                                            
  COUNT=3;ATOM=KLZCPU.CPUID;TFWD=Y;SEV=Critical;TDST=0;                    
  COUNT=3;ATOM=KLZCPU.CPUID;TFWD=Y;SEV=Warning;TDST=0;                     
  COUNT=3;ATOM=LNXPROC.CMD;TFWD=Y;SEV=Critical;TDST=0;                     
  ATOM=LNXPROC.CMDU;TFWD=Y;SEV=Critical;TDST=0;                            
  ATOM=LNXPROC.CMDU;TFWD=Y;SEV=Warning;TDST=0;                             
  COUNT=3;TFWD=Y;SEV=Warning;TDST=0;                                       
  ATOM=LNXPROC.CMDU;TFWD=Y;SEV=Warning;TDST=0;                             
  ATOM=LNXPROC.CMDU;TFWD=Y;SEV=Critical;TDST=0;                            
  ATOM=LNXPROC.CMDU;TFWD=Y;SEV=Critical;TDST=0;                            
  ATOM=KLZNET.FNAME;TFWD=Y;SEV=Critical;TDST=0;                            
  ATOM=LNXPROC.CMDU;TFWD=Y;SEV=Critical;TDST=0;                            
  COUNT=3;TFWD=Y;SEV=Critical;TDST=0;                                      
  COUNT=4;ATOM=LNXPROC.PID;TFWD=Y;SEV=Critical;TDST=0;                     
  COUNT=3;ATOM=KLZNET.FIPADDR;TFWD=Y;SEV=Critical;TDST=0;                  
  COUNT=3;TFWD=Y;SEV=Warning;TDST=0;                                       
  ATOM=LNXPROC.CMDU;TFWD=Y;SEV=Critical;TDST=0;                            
  ATOM=LNXPROC.CMDU;TFWD=Y;SEV=Critical;TDST=0;                            
  COUNT=3;TFWD=Y;SEV=Critical;TDST=0;                                      
  SEV=Warning                                                              
  SEV=Warning                                                              
  SEV=Warning                                                              
  SEV=Warning                                                              
  SEV=Critical                                                             
  SEV=Critical                                                             
  SEV=Critical                                                             
  SEV=Warning                                                              
  SEV=Warning                                                              
  SEV=Critical                                                             
  SEV=Warning                                                              
  SEV=Warning                                                              
  SEV=Warning                                                              
  SEV=Warning                                                              
  SEV=Warning                                                              
  SEV=Warning                                                              
  SEV=Warning                                                              
  SEV=Warning                                                              
  SEV=Warning                                                              
  SEV=Warning                                                              
  SEV=Warning                                                              
  SEV=Critical                                                             
  SEV=Warning                                                              
  SEV=Warning                                                              
  SEV=Warning                                                              
  SEV=Warning                                                              
  SEV=Warning                                                              
  SEV=Warning                                                              
  SEV=Warning                                                              
  SEV=Warning                                                              
  SEV=Warning;ATOM=KUDDBASEGROUP00.db_name                                 
  SEV=Warning;ATOM=KUD_DB2_Database00.db_name                              
  SEV=Warning;ATOM=KUDBUFFERPOOL00.db_name                                 
  SEV=Warning;ATOM=KUD_DB2_Buffer_Pool.db_name                             
  SEV=Critical                                                             
  SEV=Critical                                                             
  SEV=Warning                                                              
  SEV=Warning                                                              
  SEV=Critical                                                             
  SEV=Critical                                                             
  SEV=Warning                                                              
  SEV=Warning                                                              
  SEV=Critical                                                             
  SEV=Critical                                                             
  SEV=Warning                                                              
  SEV=Warning                                                              
  SEV=Critical                                                             
  SEV=Critical                                                             
  SEV=Warning                                                              
  SEV=Warning                                                              
  SEV=Critical                                                             
  SEV=Critical                                                             
  SEV=Critical;ATOM=KUDDBASE00.DBNM                                        
  SEV=Warning;ATOM=KUDDBASE00.DBNM                                         
  SEV=Critical;ATOM=KUD3437500.UA1                                         
  SEV=Warning;ATOM=KUD3437500.UA1                                          
  SEV=Critical;ATOM=KUD3437600.UUA1                                        
  SEV=Critical;ATOM=KUDDBASE01.DBNM                                        
  SEV=Warning;ATOM=KUD3437600.UUA1                                         
  SEV=Warning;ATOM=KUDDBASE01.DBNM                                         
  SEV=Critical;ATOM=KUD3437600.UUA1                                        
  SEV=Critical;ATOM=KUDDBASE01.DBNM                                        
  SEV=Warning;ATOM=KUD3437600.UUA1                                         
  SEV=Warning;ATOM=KUDDBASE01.DBNM                                         
  SEV=Critical;ATOM=KUD3437500.UA1                                         
  SEV=Critical;ATOM=KUDDBASE00.DBNM                                        
  SEV=Critical;ATOM=KUDDBASE01.DBNM                                        
  SEV=Warning;ATOM=KUDDBASE01.DBNM                                         
  SEV=Critical;ATOM=KUD3437600.UUA1                                        
  SEV=Warning;ATOM=KUD3437600.UUA1                                         
  SEV=Critical;ATOM=KUD3437600.UUA1                                        
  SEV=Critical;ATOM=KUDDBASE01.DBNM                                        
  SEV=Warning;ATOM=KUD3437600.UUA1                                         
  SEV=Warning;ATOM=KUDDBASE01.DBNM                                         
  SEV=Critical;ATOM=KUD3437600.UUA1

I am going in Loops and assigning the values of Sev ATOM and Count to 3 variables in each line and

varSEV=... (should get CRITCAL for line having SEV=CRITICAL)
varCount=... (should get 2 for line having COUNT=2)
varATOM=... (should get KUDDBASEGROUP00.db_name for line having ATOM=KUDDBASEGROUP00.db_name)

I was trying using sed and regex but failed miserably and two days gone in this (I thought will be simple thing)

Please help

Last edited by Franklin52; 12-30-2013 at 11:57 AM.. Reason: Please use code tags
# 4  
Old 12-30-2013
Here is an approach in bash:
Code:
#!/bin/bash

while read line
do
        for word in  ${line//\;/ }
        do
                [[ "$word" =~ "SEV" ]] && varSEV="${word##*=}"
                [[ "$word" =~ "COUNT" ]] && varCount="${word##*=}"
                [[ "$word" =~ "ATOM" ]] && varATOM="${word##*=}"
                [ ! -z "$varSEV" ] && echo "Severity: $varSEV"
                [ ! -z "$varCount" ] && echo "Count: $varCount"
                [ ! -z "$varATOM" ] && echo "Atom: $varATOM"
                varSEV=""
                varCount=""
                varATOM=""
        done
done < file

# 5  
Old 12-30-2013
Not clear. We have lines, that have SEV in them, lines with SEV and ATOM, others with those two and COUNT, and many not having any of those. On top, the Guessing wildly, I infer that in a shell loop you want to assign those three values to three variables, but only from lines that have all three in them? Try this:
Code:
sed -nr 's/COUNT=([^;]*).*ATOM=([^;]*).*SEV=([^;]*).*/\1 \2 \3/p' file | while read A B C; do echo $C $B $A; done
Critical KLZCPU.CPUID 3
Warning KLZCPU.CPUID 3
Critical LNXPROC.CMD 3
Critical LNXPROC.PID 4
Critical KLZNET.FIPADDR 3

This depends on the three values to occur in the same sequence in all lines. If that's not the case, you'd need to use e.g. awk.
# 6  
Old 12-31-2013
Hi

Thanks for the inputs. I really appreciate it.

Yes some lines will have all three some will have just one. Some might not have any.
I am going through the loop and just want to pick the values of each item in a variable

ATOM=KOQDBD.UDBNAME;TFWD=Y;SEV=Critical;TDST=0;
ATOM=UNIXPS.CMD;TFWD=Y;SEV=Critical;TDST=0;
COUNT=2;ATOM=UNIXPS.CMD;TFWD=Y;OV=N;SEV=Critical;TDST=0;~
COUNT=3;ATOM=UNIXPS.CMD;TFWD=Y;SEV=Minor;TDST=0;
COUNT=3;TFWD=Y;OV=N;SEV=Minor;TDST=0;~
COUNT=2;ATOM=MSETRQUE.TRQUENAME;TFWD=Y;OV=N;SEV=Minor;TDST=0;~
ATOM=KLZDISK.MOUNTPT;TFWD=N;OV=N;~



I want to assign the values (if present)
ATOM to a variable
Count to a variable
SEV to a variable

for eg

second last line i should get

varCount=2
varAtom=MSETRQUE.TRQUENAME
varSEV=Minor

for last line I should get

varCount=
varAtom=KLZDISK.MOUNTPT
varSEV=

I tried using sed

for eg for COUNT in second last option I tried

Code:
varCount=`echo $sitInfo|sed 's/.*COUNT=\(.*;\)+.*/\1/'`

where sitInfo="COUNT=2;ATOM=MSETRQUE.TRQUENAME;TFWD=Y;OV=N;SEV=Minor;TDST=0;~"

Code:
varATOM=`echo $sitInfo|sed 's/.*ATOM=\(.*;\)+.*/\1/'`
varSEV=`echo $sitInfo|sed 's/.*SEV=\(.*;\)+.*/\1/'`

Before running the above lines I will make sure that either one of them is present. Just trying to extract the values

Code:
loop....

do

varisCountpresent=`echo $sitInfo |grep -i COUNT | grep -v grep |wc -l`
    if [ "$varisCountpresent" -eq 0 ]
    then
        varCount=0
    else
        varCount=`echo $sitInfo|sed 's/.*COUNT=\(.*;\)+.*/\1/'`
    fi

varisATOMpresent=`echo $sitInfo |grep -i ATOM | grep -v grep |wc -l`
    if [ "$varisATOMpresent" -eq 0 ]
    then
        varATOM=NONE
    else
        varATOM=`echo $sitInfo|sed 's/.*ATOM=\(.*;\)+.*/\1/'`
    fi
varisSEVpresent=`echo $sitInfo |grep -i ATOM | grep -v grep |wc -l`
    if [ "$varisSEVpresent" -eq 0 ]
    then
        varSEV=NONE
    else
        varSEV=`echo $sitInfo|sed 's/.*SEV=\(.*;\)+.*/\1/'`
    fi
done


but I am not getting 2

Similarly for varAtom and varSEV none are working as expected. Kindly help Smilie

---------- Post updated 12-31-13 at 03:51 AM ---------- Previous update was 12-30-13 at 09:46 PM ----------

Hey All

I got this working. Thanks to all the hints above Smilie

Code:
 
isCount=`echo $sitInfo |grep -i COUNT | grep -v grep |wc -l`
 if [ "$isCount" -eq 0 ]
 then
  varCount=0
 else
  varCount=`echo $sitInfo|sed -nr 's/.*COUNT=([^;]*).*/\1/p'`
 fi
 isSEV=`echo $sitInfo |grep -i SEV | grep -v grep |wc -l`
        if [ "$isSEV" -eq 0 ]
        then
                varSEV=NONE
        else
                varSEV=`echo $sitInfo|sed -nr 's/.*SEV=([^;]*).*/\1/p'`
        fi
 isATOM=`echo $sitInfo |grep -i ATOM | grep -v grep |wc -l`
        if [ "$isATOM" -eq 0 ]
        then
                varATOM=NONE
        else
                varATOM=`echo $sitInfo|sed -nr 's/.*ATOM=([^;]*).*/\1/p'`
        fi

Can anyone explain why the escape charecters for regex was not required before bracket I mean

why not *ATOM=\([^;]*\)Smilie or if someone can explain this part
Code:
 
sed -nr 's/.*ATOM=([^;]*).*/\1/p'`

Sorry I want to learn so that I can reuse the concept again in future Smilie

Last edited by radioactive9; 12-30-2013 at 11:06 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

2. Shell Programming and Scripting

How to find a file with a specific pattern for current sysdate & upon find email the details?

I need assistance with following requirement, I am new to Unix. I want to do the following task but stuck with file creation date(sysdate) Following is the requirement I need to create a script that will read the abc/xyz/klm folder and look for *.err files for that day’s date and then send an... (4 Replies)
Discussion started by: PreetArul
4 Replies

3. Shell Programming and Scripting

Why regex pattern doesn't work in find?

$ find /opt/data_* -maxdepth 3 -type d -name "main*" 2> /dev/null /opt/data_025/maindblogs /opt/data_026/maindblogs /opt/data_027/maindblogs /opt/data_028/maindblogs $ find /opt/data_* -maxdepth 3 -type d -name "rvlogs*" 2> /dev/null /opt/data_002/prod/rvlogs2_archive... (4 Replies)
Discussion started by: urello
4 Replies

4. Shell Programming and Scripting

SED: Pattern repitition regex matching

Fairly straightforward, but I'm having an awful time getting what I thought was a simple regex to work. I'll give the command I was playing with, and I'm aware why this one doesn't work (the 1,3 is off the A-Z, not the whole expression), I just don't know what the fix is: Actual Output(s): $... (5 Replies)
Discussion started by: Vryali
5 Replies

5. Shell Programming and Scripting

SED Replacing all but one regex match on a line or specific matches

Hi, I'm attempting to rename some files that have spaces in them. Without linking sed commands together is it possible to replace the first three "." to " ". File.name.is.long.ext -> File name is long.ext I can get the desired effect with echo "File.name.is.long.ext" | sed 's/\./ /g;s/... (5 Replies)
Discussion started by: vectox
5 Replies

6. UNIX for Dummies Questions & Answers

blank space in regex pattern using sed

why does sed 's/.* //' show the last word in a line and sed 's/ .*//' show the first word in a line? How is that blank space before or after the ".*" being interpreted in the regex? i would think the first example would delete the first word and the next example would delete the second... (1 Reply)
Discussion started by: glev2005
1 Replies

7. 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

8. Shell Programming and Scripting

sed to awk (regex pattern) how?

Hello, I am trying to covert a for statement into a single awk script and I've got everything but one part. I also need to execute an external script when "not found", how can I do that ? for TXT in `find debugme -name "*.txt"` ;do FPATH=`echo $TXT | sed 's/\(.*\)\/\(.*\)/\1/'` how... (7 Replies)
Discussion started by: TehOne
7 Replies

9. Shell Programming and Scripting

How to assign value to a variable with row(using -n) returned by sed

Hi Friends, REQUIREMENT: Want to delete files from the current directory match with the same in the file test.txt set -x i=1 echo "i=$i" COUNT=`sed -n '$=' test.txt` echo "Count=$COUNT" while do "## Here is error##" FILETOREMOVE=`sed -n \'$i,1p\' test.txt` echo $FILETOREMOVE... (5 Replies)
Discussion started by: sourabhsharma
5 Replies
Login or Register to Ask a Question