How to grep with certain condition?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to grep with certain condition?
# 1  
Old 05-17-2013
How to grep with certain condition?

hi all,
i have an xml what i have to do is to search for the source id(s1) and if it matches with that in xml then extract the file mask from the name of the file i.e if the file name is
idr_%YYYY%%MM%%DD%_%N%.idr then , i want the part after first %
and before last % ie in this case
%YYYY%%MM%%DD%_%N%


Code:
<?xml version="1.0" encoding="UTF-8"?>
<Sources>
  <DatabaseConnection>
    <TNSName>dgid1rr</TNSName>
    <Username>rte</Username>
    <Password>rted1</Password>
  </DatabaseConnection>
  <Logger>
    <LogFilePath>/export/home/dgid1rr/merge/log</LogFilePath>
    <LogFileName>idr_merge_log</LogFileName>
    <LogLevel>Info</LogLevel>
  </Logger>
  <Source id="S1">
    <FileMask>idr_%YYYY%%MM%%DD%_%N%.idr</FileMask>
    <TimeLag>100</TimeLag>
    <Backup>True</Backup>
    <InputFilePath>/export/home/dgid1rr/cdr1/input</InputFilePath>
    <OutputFilePath>/export/home/dgid1rr/merge/cdr1/output</OutputFilePath>
    <BackupFilePath>/export/home/dgid1rr/merge/cdr1/backup</BackupFilePath>
    <ProcessOrder>FileTimestamp</ProcessOrder>
    <Enable>True</Enable>
    <MaxFiles>3</MaxFiles>
    <MaxRecs>25</MaxRecs>
  </Source>
  <Source id="S2">
    <FileMask>idr_%2N%.idr</FileMask>
    <TimeLag>300</TimeLag>
    <Backup>True</Backup>
    <InputFilePath>/export/home/dgid1rr/merge/cdr2/input</InputFilePath>
    <OutputFilePath>/export/home/dgid1rr/merge/cdr2/output</OutputFilePath>
    <BackupFilePath>/export/home/dgid1rr/merge/cdr2/backup</BackupFilePath>
    <ProcessOrder>OSTimestamp</ProcessOrder>
    <Enable>True</Enable>
    <MaxFiles>2</MaxFiles>
  </Source>
</Sources>

Thanks

Last edited by Scott; 05-17-2013 at 04:58 PM.. Reason: Moved thread - Not a Redhat question
# 2  
Old 05-17-2013
Code:
awk -F'[<>]' '/Source id="S1"/{getline;sub(/[a-z_]*%/,"%",$3);sub(/%\..*/,"%",$3);print $3}' xmlfile
%YYYY%%MM%%DD%_%N%

# 3  
Old 05-17-2013
what should i do if my file mask is after three lines of source id ie something like this


Code:
<Source id="S1">
    <Type>Ericsson3GSGSN</Type>
    <Operator>0</Operator>
    <Version>1.0</Version> 
    <FileMask>idr_%YYYY%%MM%%DD%_%N%.idr</FileMask>


Last edited by Scott; 05-19-2013 at 12:56 PM.. Reason: icode -> code tags
# 4  
Old 05-17-2013
Then call getline 4 times:
Code:
awk -F'[<>]' '/Source id="S1"/{getline;getline;getline;getline;sub(/[a-z_]*%/,"%",$3);sub(/%\..*/,"%",$3);print $3}' xmlfile

---------- Post updated at 15:40 ---------- Previous update was at 15:38 ----------

Forget getline, here is a much better approach:
Code:
awk -F'[<>]' '/Source id="S1"/{f=1}f&&/<FileMask>/{sub(/[a-z_]*%/,"%",$3);sub(/%\..*/,"%",$3);print $3;exit}' xmlfile

This User Gave Thanks to Yoda For This Post:
# 5  
Old 05-18-2013
hi, i used the same code when the value of source id = value stored in some array but then it is not giving the result . the code which i used is like this

Code:
#!/bin/bash -xv

val_1=$( sqlplus -s rte/rted2@rel76d2 << EOF
set heading off
select max(istat_id) from cvt_istats;
exit
EOF
)
echo "val_1: $val_1"

nohup ./cvt -f MediationSources.xml &
sleep 60

declare -a arr
arr=($( sqlplus -s rte/rted2@rel76d2 << EOF
set heading off
select source_id from cvt_istats where istat_id > $val_1;
exit
EOF
))
echo "val_2: $arr"

i=0
len=${#arr[@]}
echo $len

while [ $len -gt $i ]
do
  value= cat MediationSources.xml |awk -F'[<>]' '/Source id="${arr[$i]}"/{f=1}f&&/<FileMask>/{sub(/[a-z_]*%/,"%",$3);sub(/%\..*/,"%",$3);print $3;exit}'
  echo "$value file mask pass "
  i=$(( $i + 1 ))
done
~
~

can you please check wher i am going wrong

Last edited by vgersh99; 05-18-2013 at 09:53 AM.. Reason: fixed code tags
# 6  
Old 05-18-2013
It should be:
Code:
value=$( awk -F'[<>]' '/Source id="${arr[$i]}"/{f=1}f&&/<FileMask>/{sub(/[a-z_]*%/,"%",$3);sub(/%\..*/,"%",$3);print $3;exit}' MediationSources.xml )

 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Combining grep patterns with OR condition?!

Hello! I have a question about how to combine patterns in grep commands with the OR operator. So I have this little assignment here: Provide a regular expression that matches email addresses for San Jose City College faculty. A San Jose City college faculty’s email address takes the form:... (1 Reply)
Discussion started by: kalpcalp
1 Replies

2. UNIX for Dummies Questions & Answers

Grep with condition

I have file input AAAA_XX_Start> rlong . 0W 130526-11:36:13 10.128.13.226 9.0j RBS_NODE_MODEL_N_1_50 stopfile=/tmp/13019 .. ================================================================================================================= MO ... (2 Replies)
Discussion started by: radius
2 Replies

3. UNIX for Dummies Questions & Answers

Grep certain lines with condition

file input aaaa,52C aaaa,50C bbbb,50C bbbb,58C aaaa,52C bbbb,50C aaaa,30C bbbb,58C cccc,60C i want to print uniq lines with its max value of column2 expected output aaaa,52C bbbb,58C cccc,60C tks (4 Replies)
Discussion started by: radius
4 Replies

4. Shell Programming and Scripting

Where condition in grep or awk?

Dear All, I need help.. I am having a csv file. Home_TITLE,People_TITLE,Repo_ALIAS HMN5530,RKY5807,/mine_repo/rike001 HMN5530,SRY6443,/mine_repo/rike001 HMN5530,ARDY001,/mine_repo/rike001 If i have two value in varible RKY5807, HMN5530. how can fetch and store another value... (6 Replies)
Discussion started by: yadavricky
6 Replies

5. Shell Programming and Scripting

grep not equal to condition

I have below files under dir_a and dir_b and i want to sort out number of dir_a and dir_b files seperately and if i do the grep it should retrun 2 files in dir_a and 1 file in dir_b. /dir_a/12345678 /dir_a/87654321 /dir_a/dir_b/12345687 But i am getting cat file|grep dir_a|wc -l 3... (6 Replies)
Discussion started by: prash358
6 Replies

6. Shell Programming and Scripting

grep in the if condition

Hi, In this code can able to match the pattern without case sensitive. Is that possible? if u knw plz help me... code: echo "Enter name to search" read n if ; echo "name found" else echo "Not Found" fi (8 Replies)
Discussion started by: boopal
8 Replies

7. Shell Programming and Scripting

grep inside if condition - need help

hi i need help with below code. if ] then log "Exiting the script as ID= NULL" log "Please run script first." fi i am calling grep inside this but its not running any ideas why ?? input file is like this -- Msg 102, Level 20, State 1: Server... (4 Replies)
Discussion started by: dazdseg
4 Replies

8. Homework & Coursework Questions

Grep line above X condition

1. The problem statement, all variables and given/known data: I have to grep a data file called datebook.txt. The last information in each line is a salary. I have to grep all the lines which precede those lines with 6 figure salaries. I can't SID it, or use Perl. It has to be grep (or egrep or... (3 Replies)
Discussion started by: DrSammyD
3 Replies

9. Shell Programming and Scripting

grep command with AND condition

I want to do a grep with AND condition. I have three files. file1.txt ======== UNIX ...... WINDOWS ........ ORACLE file2.txt ======== UNIX ....... WINDOWS ...and many such files in a directory (6 Replies)
Discussion started by: prasperl
6 Replies
Login or Register to Ask a Question