Search one string and then search another string in the next line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search one string and then search another string in the next line
# 1  
Old 02-27-2013
Search one string and then search another string in the next line

I am unable to use grep comman to Print only EmpPosition and if the EmpID next line. So output should be both EmpPosition and EmpID and also EmpPosition and EmpID data should match.

Code:
Sample Data

   EmpPosition "New"
       EmpID "New"
       
       -
       -
    EmpPosition "New"
     EmpName "XXXX"
     -
     -
    EmpPosition "old"
       EmpID "old"
           
           -
           -
    EmpPosition "old"
     EmpName "XXXX"
    -
    -
    EmpPosition "temp"
       EmpID "old"
           
           -
           -
    EmpPosition "old"
     EmpName "XXXX"
     
  Output    
     EmpPosition "New"
       EmpID "New"   
     EmpPosition "old"
       EmpID "old"

# 2  
Old 02-27-2013
Code:
awk '/EmpPosition|EmpID/' infile

# 3  
Old 02-27-2013
Some thing like this:

Code:
awk '/EmpPosition/ { c=1;a=$2;getline } c==1 && /EmpID/ { print a,$2;next} {c=0}' file

# 4  
Old 02-27-2013
Neither of the suggestions above ensure the equality of EmpPosition and EmpID. Further, Jotne's solution does not ensure that the lines are consecutive.

Is whitespace allowed in the values to be tested for equality?

Regards,
Alister

---------- Post updated at 10:05 AM ---------- Previous update was at 09:54 AM ----------

Untested, but should work even if whitespace occurs in the quoted strings:
Code:
sed '/^[^"]*EmpPosition/!d; N; /\n[^"]*EmpID/!d; /^[^"]*\("[^"]*"\)[^"]*\1[^"]*$/!d' file

Regards,
Alister

Last edited by alister; 02-27-2013 at 11:17 AM..
# 5  
Old 02-27-2013
awk version:
Code:
awk '$1=="EmpPosition"{p=$0; v=$2; getline; if($1=="EmpID" && v==$2) print p ORS $0}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Search for a string,delete the line and replace with new string in a file

Hi Everyone, I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file. I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
10 Replies

2. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

3. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

4. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

5. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

6. Shell Programming and Scripting

Search a string and to add another string after that in new line

Hi Guys I am facing a problem:wall: In searching a string in a file and to add another string(ie. passed through command line argument) just after this(searched) string in new line. Thanks (2 Replies)
Discussion started by: kushwaha
2 Replies

7. Shell Programming and Scripting

search string in a file and retrieve 10 lines including string line

Hi Guys, I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same. can anyone of you please let me know how to go about it ? (8 Replies)
Discussion started by: sukrish
8 Replies

8. Shell Programming and Scripting

help for search string by line

Hi there, I'm having some problem here with searching line. Lets say I got 1 text file which output will be: A: Hi B: Bye C: GG D: LOL A: second B: zzz C: here D: help I will prompt user to input string that want to find and only can match with 1st line. So if user input "Hi", then,... (11 Replies)
Discussion started by: hihihehe
11 Replies

9. Shell Programming and Scripting

Help - Search for string, then do string operation on line

Hi, I wish to find all lines that contain a specific search word, and then do few string operations on that line. The idea is to "fix" the file which has been moved from windows to unix. Using unix - Sun Solaris Test input ("t2.sas") statement1 statement2 libname yahoo ... (6 Replies)
Discussion started by: deepaksinbox
6 Replies

10. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies
Login or Register to Ask a Question