tags:

views:

388

answers:

6

Hello there, I'm trying to extract the year from this output :

sam@sam-laptop:~/shell$ date
Mon Feb  8 21:57:00 CET 2010

sam@sam-laptop:~/shell$ date | cut -d' ' -f7
2010

sam@sam-laptop:~/shell$ date | awk '{print $6}'
2010

Are there any other ways to get the same result ? using maybe grep, sed etc ? Merci !

+1  A: 

Grep is intended to print out an entire line that matches an RE. Getting it to print out only part of a line will be relatively difficult (at best).

With sed, you could use an RE that matched the rest of the line, and replace it with nothing, leaving the part you care about.

Jerry Coffin
Well, getting GNU grep to print out only part of a line isn't difficult. Try the -o (--only-matching) flag.
Philip Durbin
@Philip: Thanks -- I hadn't noticed that flag (though I've probably seen the help text for it a few hundred times...)
Jerry Coffin
+9  A: 
MikeSep
I was just typing this exact same answer up when you posted it. Only thing further I would add is to read the man pages for more formatting options. In fact, I'd say nour now has a 'mandate' to read the man pages.
mmrobins
Thanks :) i'm just trying to learn various uses of bash commands...
Samantha
`$()` is easier to use and less error-prone than backticks (consider quotes and escaping).
Roger Pate
+1  A: 

You may want to check out Perl. It lifts heavily from sed and awk in terms of syntax, and is a complete programming language, with a huge library (CPAN) to help you integrate with a variety of different systems.

I migrated to Perl when I found that my simple awk/sed solutions had to expand beyond the simplest cases.

Brian Agnew
And many other shell scripting languages (which simply means you can use a shebang line) work well for similar reasons.
Roger Pate
That's very true. My preference is perl but I'm prepared to accept that's for historical reasons and today I may choose ruby/python etc.
Brian Agnew
+1  A: 

Some sed variations:

date | sed 's/.* //'

date | sed 's/.*\(....\)$/\1/'

date | sed 's/.*\(.\{4\}\)$/\1/'

date | sed -r 's/.*(.{4})$/\1/'

date | sed -r 's/.*([[:digit:]]{4})$/\1/'
Dennis Williamson
amazing ! thank you Dennis
Samantha
+1  A: 

grep finds patterns in your files. It will not, however, modify your files. sed finds patterns as well as do modification to your files. cut is a tool to "cut" columns in your files for display/(or to file). Use it if your task is very simple as just getting some columns. awk finds patterns in your file, and you can do modifications to it by creating another file. And awk does what sed, grep, cut does, so you can do almost anything with it with just 1 tool.

For Big sized files, use grep to find the pattern and pipe to awk/sed for manipulation of text.

for your example, if you want to get the YEAR of date command , use date +%Y.

various ways to get the YEAR of date command

$ date +%Y
2010

$ date | awk '{print $NF}'
2010

$ var=$(date)
$ set -- $var
$ eval echo \${${#}}
2010

Lastly, you can use regular expressions like some sed examples, but I find it easiest to just split the fields and get the last field. No complicated regex is needed.

ghostdog74
+1  A: 

With GNU grep you can use -o (--only-matching) to show only the part of a matching line that matches a pattern. Below the pattern is a Perl regular expression (-P, --perl-regexp) for four digits in a row:

$ date | grep -oP '\d{4}'
2010
Philip Durbin
Very cool Philip, thanks !
Samantha