Help needed in search string


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help needed in search string
# 1  
Old 11-22-2007
Help needed in search string

Hi ,

I learning shell scripting..

I need to do the following in my shell script.

Search a given logfile for two\more strings.

If the the two strings are found. write it to a outputfile
if only one of the string is found, write the found string in one output file
and other in other output file.

Is it possible to do it?
pls help

Thx
Amit
# 2  
Old 11-22-2007
Lets see if i have understood you correctly:

you search for a string of the form "<string1>\<string2>" and want to write it to a file. Then you search for strings of the form "<string1>\" or "\<string2>" and write it to another file.

This is done with pattern matching. You can use either sed or awk for that, in this case i would suggest sed because it is smaller than awk and you don't need the capabilities awk would offer.

The string "<string1>/<string2>" can be found with the regexp:

Code:
[^ ][^ ]*\\[^ ][^ ]*

This searches for one or more non-spaces ("[^ ][^ ]*") followed by a backslash followed by one or more non-spaces. The "\" has to be escaped because it has a special meaning, therefore "\\".

The second form of strings can be found by two regexps, very similar to the first one:

Code:
" \\[^ ][^ ]*" and
"[^ ][^ ]*\\ "

A space followed by a backslash followed by one or more non-spaces or one or more non-spaces followed by a backslash followed by a space.

Write the found lines to two different files and you are done.

If you need further help feel free to ask.

bakunin
# 3  
Old 11-22-2007
Hi Bak,

Thanks for the info. I did not follow the above...

I have a file called buildlog.txt and I need to look for a particular strings
(for example)

In buildlog.txt
I am looking for string "image1" and "image2"

If the string "image1" and "image2" is found then pass it into a file1.txt
if only string "image1" & no string "image2" found then pass string "image1" to file1.txt and pass the string "image2" to file2.txt

How can I do it?

Thx

Amit
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use a grep search to search for a specific string within multiple directories?

Lets say I have a massive directory which is filled with other directories all filled with different c++ scripts and I want a listing of all the scripts that contain the string: "this string". Is there a way to use a grep search for that? I tried: grep -lr "this string" * but I do not... (3 Replies)
Discussion started by: Circuits
3 Replies

2. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies

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

4. Shell Programming and Scripting

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. Sample Data EmpPosition "New" EmpID "New" - - EmpPosition "New" ... (4 Replies)
Discussion started by: onesuri
4 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

Help needed :Search and Replace a string pattern with empty in an xml file in unix

Search and Replace a string pattern with empty in an xml file in unix: My xml file would be like this : <Accounts><Name>Harish</Name><mobile>90844444444444445999 </mobile><TRIG>srcujim-1</TRIG></Accounts><Accounts><Name>Satish</Name><mobile>908999</mobile><TRIG>ettertrtt-1</TRIG></Accounts> ... (1 Reply)
Discussion started by: harish_s_ampeo
1 Replies

7. Shell Programming and Scripting

Printing 10 lines above and below the search string: help needed

Hi, The below code will search a particular string(say false in this case) and return me 10 lines above and below the search string in a file. " awk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r;print("***********************************");print;c=a;}b{r=$ 0}' b=10 a=10 s="false" " ... (5 Replies)
Discussion started by: vimalm22
5 Replies

8. Shell Programming and Scripting

Search, replace string in file1 with string from (lookup table) file2?

Hello: I have another question. Please consider the following two sample, tab-delimited files: File_1: Abf1 YKL112w Abf1 YAL054c Abf1 YGL234w Ace2 YKL150w Ace2 YNL328c Cup9 YDR441c Cup9 YDR442w Cup9 YEL040w ... File 2: ... ABF1 YKL112W ACE2 YLR131C (9 Replies)
Discussion started by: gstuart
9 Replies

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