Search word in a line and print earlier pattern match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search word in a line and print earlier pattern match
# 1  
Old 07-24-2009
Search word in a line and print earlier pattern match

Hi All,
I have almost 1000+ files and I want to search specific pattern. Looking forwarded your input.

Search for: word1.word2 (Which procedure contain this word, I need procedure name in output.

Expected output:
procedure test1
procedure test2
procedure test3
procedure test4


File Contain:
procedure test1
a:=word1.word2;
end;


procedure

test2
a:= word1.word2;
end;


procedure test3
a := word1.word2;
end;

procedure test4
a :=word1.word2;
end;

procedure test5
a :=word1.word3;
end;

procedure test6
a :=word1.word3;
end;
# 2  
Old 07-24-2009
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

---------- Post updated at 19:37 ---------- Previous update was at 19:28 ----------

Try something like this:

Code:
 awk '/procedure/{s=$0;f=1}/word1\.word2/ && f{print s;f=0}' file

# 3  
Old 07-24-2009
Code:
grep -B1 "word1\.word2" input_file

# 4  
Old 07-24-2009
Hi Franklin, thanks for reply, however this is not working for procedure test2. test2 is missing, the only issue because test2 available in new line. Can you pls reframe this as I am not that good in unix. Appreciate your quick response.

---------- Post updated at 12:46 PM ---------- Previous update was at 12:45 PM ----------

Quote:
Originally Posted by Franklin52
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

---------- Post updated at 19:37 ---------- Previous update was at 19:28 ----------

Try something like this:

Code:
 awk '/procedure/{s=$0;f=1}/word1\.word2/ && f{print s;f=0}' file


Hi Franklin, thanks for reply, however this is not working for procedure test2. test2 is missing, the only issue because test2 available in new line. Can you pls reframe this as I am not that good in unix. Appreciate your quick response.
# 5  
Old 07-24-2009
Try this one:

Code:
awk '
/procedure/{s=$0;f=1}
/^test[0-9]/{s=s FS $0}
/word1\.word2/ && f{print s;f=0}
' file

Regards
# 6  
Old 07-25-2009
Not working

Hi Franlin, Thanks again for response, however looks like you have placed hardcoded test, this name is not fixed. I can be anything, so I am looking for any word which is coming right after keyword procedure, that needs to be printed.

Quote:
Originally Posted by Franklin52
Try this one:

Code:
awk '
/procedure/{s=$0;f=1}
/^test[0-9]/{s=s FS $0}
/word1\.word2/ && f{print s;f=0}
' file

Regards
# 7  
Old 07-25-2009
Quote:
Originally Posted by susau_79
Hi Franlin, Thanks again for response, however looks like you have placed hardcoded test, this name is not fixed. I can be anything, so I am looking for any word which is coming right after keyword procedure, that needs to be printed.
Something like this?

Code:
awk '/procedure/{
  s=$0; f=1
  if(length < 10){
    line=""
    while(!line){getline line}
    s=s FS line
  }
}
/word1\.word2/ && f{print s;f=0}
' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to print the next word from the same line based on grep string condtion match.

I need to fetch particular string from log file based on grep condition match. Actual requirement is need to print the next word from the same line based on grep string condtion match. File :Java.lanag.xyz......File copied completed : abc.txt Ouput :abc.txt I have used below... (5 Replies)
Discussion started by: siva83
5 Replies

2. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

3. Shell Programming and Scripting

How to match the first word and print only that line in UNIX?

Below is the file DISK-A 109063.2 49 31 40.79 DISK-B 110058.5 49 44 57.07 DISK-c 4402.4 2 1 2.14 from the file, i want to search for 'DISK-A' and print only that line with the first word matching to DISK-A and the output should skip DISK-A. Desired Output: (If i'm... (2 Replies)
Discussion started by: web2moha
2 Replies

4. Shell Programming and Scripting

Print only next pattern in a line after a pattern match

I have 2013-06-11 23:55:14 1Umexd-0004cm-IG <= user@domain.com I need sed/awk operation on this, so that it should print the very next pattern only after the the pattern mach <= ie only print user@domain.com (7 Replies)
Discussion started by: anil510
7 Replies

5. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

6. Shell Programming and Scripting

print word after pattern match in two instances

i have a file like below. how can i printout the digits followed by the pattern -bwout and -bwin. say i run the script by entering line number 145 (the fourth line), then the o/p should be like 5000000 1024000 8 test1 -ipprot erp -ppsout 500 -ppsin 500 -bwout 300000 -bwin 300000 -statsdevice... (7 Replies)
Discussion started by: sb245
7 Replies

7. UNIX for Dummies Questions & Answers

MATCH A PATTERN AND PRINT A LINE ABOVE AND BELOW

Dear All, Hv a very specific requirement. I have a very large text file and in which I have to match a pattern and insert a line above and below. Eg: My file cat test date1 date2 date3 date4 I need to match 'date3' and insert "Reminder1" above date3 and insert 'reminder2'... (4 Replies)
Discussion started by: gokulj
4 Replies

8. Shell Programming and Scripting

Print word 1 in line 1 and word 2 in line 2 if it matches a pattern

i have a file in this pattern MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1... (7 Replies)
Discussion started by: bangaram
7 Replies

9. Shell Programming and Scripting

match a pattern and print the line once

Hi, I have a xml file <cisco:name> <cisco:mdNm>Cisco Device 7500 A Series</cisco:mdNm> <cisco:meNm>10.1.100.19</cisco:meNm> <cisco:ehNm>/shelf=1</cisco:ehNm> <cisco:subname> <cisco:meNm>10.1.100.19</cisco:meNm> <cisco:sptp>Cisco PortA Series</cisco:sptp> ... (11 Replies)
Discussion started by: bhagirathi
11 Replies

10. Shell Programming and Scripting

match a pattern, print it and the next line

I have a file nbu_faq.txt (Question/answer) which looks like this What I am trying to do is write out each question in a file1.txt and than the question/answer in a file2.txt like this file1.txt Q: What is nbu? Q: What is blablabla...? Q: Why ....? file2.txt Q: What is nbu? A:... (4 Replies)
Discussion started by: nymus7
4 Replies
Login or Register to Ask a Question