LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   print everything after $date (https://www.linuxquestions.org/questions/programming-9/print-everything-after-%24date-790532/)

supulton 02-21-2010 05:46 AM

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?

vikas027 02-21-2010 05:50 AM

Quote:

Originally Posted by supulton (Post 3871318)
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}'

ghostdog74 02-21-2010 08:38 AM

Quote:

Originally Posted by vikas027 (Post 3871321)
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

ghostdog74 02-21-2010 08:41 AM

Quote:

Originally Posted by supulton (Post 3871318)
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


supulton 02-21-2010 08:44 AM

Quote:

Originally Posted by ghostdog74 (Post 3871409)
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

supulton 02-21-2010 08:46 AM

Quote:

Originally Posted by ghostdog74 (Post 3871411)
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!


All times are GMT -5. The time now is 12:58 AM.