LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-25-2003, 12:42 AM   #1
mikshaw
LQ Addict
 
Registered: Dec 2003
Location: Maine, USA
Distribution: Slackware/SuSE/DSL
Posts: 1,320

Rep: Reputation: 45
multiline grep result > multiple variable?


I'm just getting started with scripting, so this might be unnecessarily complicated....
Code:
ThisDay=`date +%m%d`                           # current date in 4-digits (mmdd)
ThisEvent=`grep ${ThisDay} ~/Documents/dates`  # find current date in a file

if [ -n "${ThisEvent}" ]; then 
   echo ${ThisEvent:5}    # display the line of text while omitting the date
fi
~/Documents/dates is in this format:
0101 Happy New Year
0530 Happy Birthday to me
1225 Merry Christmas

This works for me as long as there is only a single entry for each date. If there are more than one they are all printed on a single line, including their dates.
What I'd like to do is print each entry on its own line. I can do this with a simple grep but I don't want those four digits at the begining of each line.

Is there another application to which I can pipe the grep output, or perhaps a simpler way to handle this?

Thanks for any leads.
 
Old 12-25-2003, 01:03 AM   #2
teval
Member
 
Registered: Jul 2003
Location: Toronto, Canada
Distribution: Gentoo
Posts: 720

Rep: Reputation: 30
You could do this in a much simpler way, if you were to change something in that file using a regual expression.

0101 Happy New Year

Replace with

0101:Happy New Year

Note the delimiter between the date and name is now :
Now you can do

cat <FILE>|grep `date +%m%d`|awk -F: '{ print $2 }'

Replace <FILE> with your file.
I'll leave the regular expression to change the delimiter, as a challange it's very simple though.
 
Old 12-25-2003, 01:31 AM   #3
mikshaw
LQ Addict
 
Registered: Dec 2003
Location: Maine, USA
Distribution: Slackware/SuSE/DSL
Posts: 1,320

Original Poster
Rep: Reputation: 45
That looks much simpler....I'll need to man awk to figure out what's happening, though
Thanks!

Quote:
I'll leave the regular expression to change the delimiter, as a challange it's very simple though.
I don't understand this part...it works perfectly as you wrote it....nothing needs changing.

Many thankyous...now I hopefully won't ever forget another birthday.....

Last edited by mikshaw; 12-25-2003 at 01:46 AM.
 
Old 12-25-2003, 08:52 AM   #4
teval
Member
 
Registered: Jul 2003
Location: Toronto, Canada
Distribution: Gentoo
Posts: 720

Rep: Reputation: 30
No problemo

man awk won't help much, info awk will help a little more. awk is basically a programming language, so you're better off finding a book/tutorial online.

What I meant with the regex, is that if you already have your file the way you showed it first, you don't have to go change it by hand. You can write a regular expression to go do it for you. (Solve a 5 minute problem in 2 hours, the first time, but once you get confortable with regexes, and awk, a lot of things will seem like child's play.)


Heh, I have the birthday problem too
 
Old 12-25-2003, 10:47 AM   #5
mikshaw
LQ Addict
 
Registered: Dec 2003
Location: Maine, USA
Distribution: Slackware/SuSE/DSL
Posts: 1,320

Original Poster
Rep: Reputation: 45
Oh...that's very cool.
I only had a few lines in my file at this point, though, for testing, so it was no trouble to do it by hand.
I was thinking of writing a script to add entries to the file so I wouldn't need to open the file, edit, save...but then I remembered this is Linux....
echo '1225:Merry Christmas!' >> ~/Do<Tab>da<Tab><Enter>
Bash rocks.

Edit:

So from what I've gathered so far, awk -F : '{ print $2 }' tells awk to use a colon for a field separator, then output the second field?

Last edited by mikshaw; 12-25-2003 at 01:22 PM.
 
Old 12-25-2003, 11:13 PM   #6
teval
Member
 
Registered: Jul 2003
Location: Toronto, Canada
Distribution: Gentoo
Posts: 720

Rep: Reputation: 30
Exactely, there is a lot more to awk though.
It's THE text editing language, even better at it then perl is.
 
  


Reply


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
Using find and grep - how to exclude a result ? graziano1968 Linux - General 13 12-04-2010 11:02 PM
Need to assign result of grep to var in PERL amytys Programming 1 09-23-2004 07:25 PM
ps -ef | grep iptables gives no result ? markraem Linux - Networking 1 07-07-2004 06:28 AM
how to grep within a script, and test result? bobbyr Programming 4 01-13-2004 01:11 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 12:38 PM


All times are GMT -5. The time now is 02:54 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration