LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-19-2010, 07:59 PM   #1
skarychinezeguie
LQ Newbie
 
Registered: Feb 2010
Posts: 3

Rep: Reputation: 0
bash script search file


total newb here. call me a script kiddie if you want but here is what i need to do and what i have.

need to:
search a hidden log file for a specific string, find what comes after that part, and then output the result to a variable or something that can be used by an application or other script to carry out further actions.

example:

self.selectedAuxStateStr = Available

self.selectedAuxStateStr = 2 Training


i need to find "self.selectedAuxStateStr = " and then determine what state it is currently in. also, since the log is in chronological order i will need to start at the bottom and find the LAST instance of this string.

here is what i have so far
Code:
( date; uname -a ) >>logfile
# Time and machine name
echo ---------------------------------------------------------- >>logfile
tail -n $LINES /var/log/system.log | tr -d "\"'" | xargs | fmt -s >>logfile
echo >>logfile
echo >>logfile
echo "self.selectedAuxStateStr = " | tr '' ' '
echo "success"
exit 0
 
Old 12-19-2010, 08:08 PM   #2
mike11
Member
 
Registered: Apr 2009
Posts: 222

Rep: Reputation: 17
grep can output line before and after the match. check the man page.
my guess is that you'll need something like
Code:
var=$(grep -B 1 -A 1 <string> <file>)
replace <string> and <file> with the string ur searching for and the file you work on.
The above example will output lines containing the string plus one line before and after and var will hold the output.
 
Old 12-19-2010, 08:50 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Code:
var=$( grep self.selectedAuxStateStr /var/log/system.log | tac | head -1 | sed 's/^.*= //' )
 
Old 12-19-2010, 09:01 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Code:
var=$(awk -F" *= *" '/self\.selectedAuxStateStr/{x=$2}END{print $2}' /var/log/system.log)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Search and Replace in bash script daemoneye Other *NIX 1 04-08-2009 01:24 AM
Does anyone know of a bash script can search & replace txt in a file. jimwelc Linux - Newbie 6 09-15-2008 12:13 AM
Bash script to search through directories. mcdrr Programming 5 05-11-2007 05:41 PM
search a file for a word - bash script paul_mat Linux - Software 12 04-16-2006 01:59 AM
search function (bash script) LYK Programming 2 05-27-2004 10:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration