Linux - SoftwareThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.)
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.