Finding a string in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding a string in a file
# 1  
Old 12-17-2012
Finding a string in a file

Hello All,

I have a text file, i want to search for a string in it and the string is repeated multiple times in a file i want to get the first occurence of the string in a variable.

the content of file is like:

Quote:
2012-12-04 12:59:38 minor dmsc: Deliver final message (sms_done) to "919852104962"
2012-12-04 13:00:15 minor dmsc: Configuration flow done for "919852104962"
2012-12-04 13:18:14 info dmsc: New dms_event on 919852104962
2012-12-04 13:48:28 minor dmsc: Configuration flow done for "919852104962"
I want to grepthe first occurance of "Configuration flow done" and store the result in a variable,
I am currently doing
Code:
var=`cat "filename" | grep "Configuration flow done"`

,
Here var shows var=2012-12-04 13:48:28 minor dmsc: Configuration flow done for "919852104962"

but what i want is:
var=2012-12-04 13:00:15 minor dmsc: Configuration flow done for "919852104962"

Need help....thanks in advance
# 2  
Old 12-17-2012
can you try something like this.

Code:
cat filename | grep -i 'Configuration flow done' | head -1

This User Gave Thanks to Vikram_Tanwar12 For This Post:
# 3  
Old 12-17-2012
One with sed
Code:
$ sed -n "/Configuration flow done/{p;q;}" filename
2012-12-04 13:00:15 minor dmsc: Configuration flow done for "919852104962"

Some grep's also have an option (i.e. -m NUM) to exit after the specified number of matches.
This User Gave Thanks to Scott For This Post:
# 4  
Old 12-17-2012
Thanks Guys Smilie
# 5  
Old 12-17-2012
Try it without the cat
Code:
var=`grep -i 'Configuration flow done' filename | head -1`



Robin
Liverpool/Blackburn
UK
This User Gave Thanks to rbatte1 For This Post:
# 6  
Old 12-17-2012
with awk
Code:
var=$(awk '/Configuration flow done/ {print;exit}' filename)

the exit makes awk exit after first find.
These 2 Users Gave Thanks to Jotne For This Post:
# 7  
Old 12-18-2012
Thank You All
I also got an option
Code:
cat filename | grep -m1 "Configuration flow done"

It also worked
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Finding specific string in file and storing in another file

Text in input file is like this <title> <band height="21" isSplitAllowed="true" > <staticText> <reportElement x="1" y="1" width="313" height="20" key="staticText-1"/> <box></box> <textElement> <font fontName="Arial" pdfFontName="Helvetica-Bold"... (4 Replies)
Discussion started by: aankita30
4 Replies

2. Shell Programming and Scripting

Finding a string in a list of files, print file names

I'm interested in writing a report script using BASH that searches all of the files in a particular directory for a keyword and printing a list of files containing this string... In fact this reporting script would have searches for multiple keywords, so I'm interested in making multiple... (2 Replies)
Discussion started by: chemscripter904
2 Replies

3. UNIX for Dummies Questions & Answers

Finding files with a certain name string inside of another file.

Hi, I have a very large file that contains a listing of all files on the system. I need to create a listing from that file of all files that start with the following format: s???_*, whereas the '?' represents characters, so the file name begins with an 's' followed by three other characters and... (4 Replies)
Discussion started by: tes218
4 Replies

4. Shell Programming and Scripting

Reformatting single column text file starting new line when finding particular string

Hi, I have a single colum file and I need to reformat the file so that it creates a new line every time it come to an IP address and the following lines are corresponding rows until it comes to the next IP address. I want to turn this 172.xx.xx.xx gwpusprdrp02_pv seinwnprd03... (7 Replies)
Discussion started by: kieranfoley
7 Replies

5. Shell Programming and Scripting

mappin strings of two different file and finding the mapped string and then map other fields.

As i am new to unix so facing some problems in scripting: here is my question: i m having two files. 1st file say a.txt contain 3 column like SPECIALITY|UMP_CODE|SPECIALTY_CODE Addictive Diseases|25ADD|ADD Addictive Diseases/Family Practice|25ADD|ADD/FP Aerospace Medicine|1.041666667|AM... (4 Replies)
Discussion started by: dsh007
4 Replies

6. UNIX for Dummies Questions & Answers

finding string in very long file without newlines

What's the best way to find a string in a very long file without newlines in Unix? The standard utility I'm aware of for finding a string in a single file is grep, but for a long file without newlines, I think the output is just going to be the input. I suppose I could use sed to replace the... (5 Replies)
Discussion started by: aaronpoley
5 Replies

7. Shell Programming and Scripting

Finding a string in a text file and posting part of the line

What would be the most succinct way of doing this (preferably in 1 line, maybe 2): searching the first 10 characters of every line in a text file for a specific string, and if it was found, print out characters 11-20 of the line on which the string was found. In this case, it's known that there... (13 Replies)
Discussion started by: busdude
13 Replies

8. Shell Programming and Scripting

Need help with finding unique string in log file

Shell script help Here is 3 sample lines from a log file <date> INFO <java.com.blah> abcd:ID= user login <date> DEBUG <java.com.blah> <nlah bla> abcd:ID=123 user login <date> INFO <java.com.blah> abcd:ID=3243 user login I want to find unique "ID" from this log... (3 Replies)
Discussion started by: gubbu
3 Replies

9. UNIX for Advanced & Expert Users

finding a string in a file

hi all.. I dont know how to search for a string in a file.. I have tried doing.. I did google but didnt get effective answers..my code is as follows: int search(char* filename,const char* username,const char* passwd) { int flag=0; unsigned long fsize=0; unsigned long current=0;... (2 Replies)
Discussion started by: Ume1986
2 Replies

10. Shell Programming and Scripting

Finding a certain string on each line in a file

Hi, I need a script to get every line from a file where there are less then 17 ; on a line. Thank's (5 Replies)
Discussion started by: VODAFUN
5 Replies
Login or Register to Ask a Question