Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 12-19-2010, 08:59 PM   #1
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.


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
( 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, 09:08 PM   #2
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
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, 09:50 PM   #3
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

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

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 02:24 AM
Does anyone know of a bash script can search & replace txt in a file. jimwelc Linux - Newbie 6 09-15-2008 01:13 AM
Bash script to search through directories. mcdrr Programming 5 05-11-2007 06:41 PM
search a file for a word - bash script paul_mat Linux - Software 12 04-16-2006 02:59 AM
search function (bash script) LYK Programming 2 05-27-2004 11:51 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:55 PM.

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