How to print the next line by searching with different patterns in AIX server?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print the next line by searching with different patterns in AIX server?
# 1  
Old 06-06-2012
Java How to print the next line by searching with different patterns in AIX server?

Hi,

I am having an '.xml' file with 'n' number of lines and also having another file with '.txt' format contains values which i want to search.

Now I want to print the next line with the pattern which i am searching in '.xml' file. And the loop has to repeat for different patterns which are present in '.txt' file.

the logic which i used is

Code:
  while read line
  do
    grep -h -e  $line -A 1  file1.xml >> values.txt
  done < file2.txt

the above code is working in Linux server. Where I tried the same code in AIX server, it thrown below error

Code:
.rep: 0652-033 Cannot open
grep: 0652-033 Cannot open -A.
grep: 0652-033 Cannot open 1.
.rep: 0652-033 Cannot open
grep: 0652-033 Cannot open -A.
grep: 0652-033 Cannot open 1.

..................

Please help me out from this.

Last edited by Scrutinizer; 06-06-2012 at 08:06 AM.. Reason: code tags
# 2  
Old 06-06-2012
The -A flag is a gnu extension to the grep command, try something like the following (untested) code.
Code:
perl -e 'while(<>){open XML , "<", "file1.xml") ;chomp; $pattern=qr(\Q$_\E/);while(<XML>){print scalar readline(XML)  if (/$pattern/);}}' file2.txt

# 3  
Old 06-06-2012
AIX has a different version of grep. It has no -A and -B switches.

Here are threads with solutions:
https://www.unix.com/shell-programmin...ove-below.html
https://www.unix.com/shell-programmin...p-pattern.html
# 4  
Old 06-07-2012
Hi,
I tried with the above perl command, but its thowring error.

where my xml file contains,
Code:
<NameValuePair>
            <name>Security/SharedResources/Shared/JNDI/Username</name>
            <value>user</value>
        </NameValuePair>
        <NameValuePair>
            <name>Security/SharedResources/Shared/JNDI/Password</name>
            <value>u23r</value>
        </NameValuePair>
        <NameValuePair>
            <name>Security/SharedResources/Shared/JNDI/URL</name>
            <value>tibjmsnaming://aix123.ttgplc.net:11240,tibjmsnaming://aix123.ttgplc.net:11240</value>
        </NameValuePair>
                <!--- Public EMS Server Settings -->
        <NameValuePair>
            <name>Security/SharedResources/Shared/JNDI-Public/Password</name>
            <value>u23r</value>
        </NameValuePair>
        <NameValuePair>
            <name>Security/SharedResources/Shared/JNDI-Public/Username</name>
            <value>user</value>
        </NameValuePair>
        <NameValuePair>
            <name>Security/SharedResources/Shared/JNDI-Public/URL</name>
            <value>tibjmsnaming://aix123.ttgplc.net:11230,tibjmsnaming://aix123.ttgplc.net:11230</value>
        </NameValuePair>

and the patterns which i want to search are placed in a txt file, contains,
Code:
Security/SharedResources/Shared/JNDI/Password
Security/SharedResources/Shared/JNDI/ContextUrl
Security/SharedResources/Shared/JNDI/Username
Security/SharedResources/Shared/JNDI-Public/Password
Security/SharedResources/Shared/JNDI-Public/ContextUrl
Security/SharedResources/Shared/JNDI-Public/Username

Now i want to print the next line of xml file using the above patterns and save it to another file.

Please help me out from this issue..

---------- Post updated at 12:37 AM ---------- Previous update was at 12:36 AM ----------

the logic has to be work in AIX server..

Last edited by Franklin52; 06-07-2012 at 03:59 AM.. Reason: Please use code tags
# 5  
Old 06-07-2012
Code:
while read line; do nawk -v l="$line" '$0~l{getline;print;exit}' file.txt; done < pattern.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print two matched patterns only from each line?

My input looks like this. # Lot Of CODE Before AppType_somethinglese=$(cat << EOF AppType_test1='test-tool/blatest-tool-ear' AppType_test2='test/blabla-ear' # Lot Of CODE After I want to print text betwen 1) _ and = and 2)/ and ' from each line and exclude lines with "EOF". Output... (2 Replies)
Discussion started by: kchinnam
2 Replies

2. Shell Programming and Scripting

How to print line if two lines above it matches patterns.?

Hi, I could only find examples to print line before/after a match, but I'd need to print line after two separate lines matching. E.g.: From the below log entry, I would need to print out the 1234. This is from a huge log file, that has a lot of entries with "CLIENT" and "No" entries (+ other... (3 Replies)
Discussion started by: Juha
3 Replies

3. Shell Programming and Scripting

Find matched patterns and print them with other patterns not the whole line

Hi, I am trying to extract some patterns from a line. The input file is space delimited and i could not use column to get value after "IN" or "OUT" patterns as there could be multiple white spaces before the next digits that i need to print in the output file . I need to print 3 patterns in a... (3 Replies)
Discussion started by: redse171
3 Replies

4. Shell Programming and Scripting

Print line between two patterns when a certain pattern matched

Hello Friends, I need to print lines in between two string when a keyword existed in those lines (keywords like exception, error, failed, not started etc). for example, input: .. Begin Edr ab12 ac13 ad14 bc23 exception occured bd24 cd34 dd44 ee55 ff66 End Edr (2 Replies)
Discussion started by: EAGL€
2 Replies

5. Shell Programming and Scripting

Print mutliple patterns in a line using sed

Hi, I am trying to print multiple patterns in a line using sed. But it is printing only the last occurance of a pattern. If the line is the the output should be Lookup Procedure|Stored proc But the output I am getting is Stored proc The code I am using is echo... (9 Replies)
Discussion started by: kedar_laveti
9 Replies

6. Shell Programming and Scripting

Fetch the different data by searching with a same variable from a file in AIX server

Hi, I am trying to fetch the different values in an xml file by searching with the same variable in AIX Server. <name>SharedResources/Shared/JNDI/Username</name> <value>admin</value> <name>SharedResources/Shared/JNDI/Username</name> ... (1 Reply)
Discussion started by: tejastrikez
1 Replies

7. Shell Programming and Scripting

print the next line by searching with different patterns

Hi, I am having an '.xml' file with 'n' number of lines and also having another file with '.txt' format contains values which i want to search. Now I want to print the next line with the pattern which i am searching in '.xml' file. And the loop has to repeat for different patterns which... (5 Replies)
Discussion started by: tejastrikez
5 Replies

8. Shell Programming and Scripting

Match multiple patterns in a file and then print their respective next line

Dear all, I need to search multiple patterns and then I need to print their respective next lines. For an example, in the below table, I will look for 3 different patterns : 1) # ATC_Codes: 2) # Generic_Name: 3) # Drug_Target_1_Gene_Name: #BEGIN_DRUGCARD DB00001 # AHFS_Codes:... (3 Replies)
Discussion started by: AshwaniSharma09
3 Replies

9. Shell Programming and Scripting

Searching patterns in 1 file and deleting all lines with those patterns in 2nd file

Hi Gurus, I have a file say for ex. file1 which has 3500 lines in it which are different account numbers and another file (file2) which has 230000 lines in it. I want to read all the lines in file1 and delete all those lines from file2 which has that same pattern as in file1. I am not quite... (4 Replies)
Discussion started by: toms
4 Replies

10. UNIX for Dummies Questions & Answers

searching for two or more patterns in a line

how can I search for two or more patterns in one line using grep? for example if I want to show only the lines that have patterns "abc" and "123"? and what if I want to show only the lines that have either "abc" or "123" in them? any hint apprecited (4 Replies)
Discussion started by: metalwarrior
4 Replies
Login or Register to Ask a Question