LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-21-2010, 05:46 AM   #1
supulton
LQ Newbie
 
Registered: Feb 2010
Posts: 21

Rep: Reputation: 0
print everything after $date


Say I have a line like such:

Code:
today      2010 Feb 21 test10, 12AM
How would I print everything AFTER "2010 Feb 21 "?

What I thought to do so far is to set a variable to the format used in the string like so:

Code:
date="$(date +%Y\ %b\ %e)"
So I know I have to use either awk, sed, or perhaps bash substrings to work with the variable $date, but as to how, I'm drawing a blank.

Any hints?
 
Old 02-21-2010, 05:50 AM   #2
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by supulton View Post
Say I have a line like such:

Code:
today      2010 Feb 21 test10, 12AM
How would I print everything AFTER "2010 Feb 21 "?
One suggestion is:

Code:
echo "today      2010 Feb 21 test10, 12AM" | awk '{print $5 "  " $6}'
 
1 members found this post helpful.
Old 02-21-2010, 08:38 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by vikas027 View Post
One suggestion is:

Code:
echo "today      2010 Feb 21 test10, 12AM" | awk '{print $5 "  " $6}'
what if there are more words after "2010 Feb 21" ? $5 and $6 would not be enough
 
Old 02-21-2010, 08:41 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by supulton View Post
Say I have a line like such:

Code:
today      2010 Feb 21 test10, 12AM
How would I print everything AFTER "2010 Feb 21 "?

What I thought to do so far is to set a variable to the format used in the string like so:

Code:
date="$(date +%Y\ %b\ %e)"
So I know I have to use either awk, sed, or perhaps bash substrings to work with the variable $date, but as to how, I'm drawing a blank.

Any hints?
Code:
$ line="today      2010 Feb 21 test10, 12AM"
$ echo ${line##*2010 Feb 21 }
test10, 12AM
Code:
$ var=$(date +%Y\ %b\ %e)
$ echo ${line##*$var }
test10, 12AM
 
1 members found this post helpful.
Old 02-21-2010, 08:44 AM   #5
supulton
LQ Newbie
 
Registered: Feb 2010
Posts: 21

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ghostdog74 View Post
what if there are more words after "2010 Feb 21" ? $5 and $6 would not be enough
i realized this too and modified it:

Code:
echo "today      2010 Feb 21 two words, 12AM" | awk '{print substr($0, index($0,$5)) }'
this will print from the fifth word to the end of the line.
Code:
two words, 12AM
 
Old 02-21-2010, 08:46 AM   #6
supulton
LQ Newbie
 
Registered: Feb 2010
Posts: 21

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ghostdog74 View Post
Code:
$ line="today      2010 Feb 21 test10, 12AM"
$ echo ${line##*2010 Feb 21 }
test10, 12AM
Code:
$ var=$(date +%Y\ %b\ %e)
$ echo ${line##*$var }
test10, 12AM
this actually looks like a cleaner method. thanks!
 
  


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
Date comparison with 'string date having slashes and time zone' in Bash only TariqYousaf Programming 2 10-08-2009 07:37 AM
shell script to find modified date and last accessed date of any file. parasdua Linux - Newbie 6 04-22-2008 09:59 AM
Setting system date and time affecting the clock and date on BIOS satimis Ubuntu 7 09-21-2007 08:02 AM
what is the correct syntax order for tar with --after-date DATE, --newer DAT farhan Linux - General 1 03-16-2007 08:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:15 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