Zenet
Zenet

Reputation: 7411

basics of using cut awk grep and sed

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 !

Upvotes: 3

Views: 5719

Answers (6)

Philip Durbin
Philip Durbin

Reputation: 4082

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

Upvotes: 1

ghostdog74
ghostdog74

Reputation: 342463

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.

Upvotes: 1

Dennis Williamson
Dennis Williamson

Reputation: 360153

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/'

Upvotes: 2

Mike Seplowitz
Mike Seplowitz

Reputation: 10375

If you're really just looking for the current year from date, you might consider just running this directly.

date +%Y

No post-processing needed. :)


Update: Comments on text processing (since the OP is looking for those)

cut/awk/sed are great for pulling apart lines of text. grep is good for finding the lines you want.

More obscure (and less portable) are the bash-specific regexp-like operators, but they can be quick and fun to use.

$ MYDATE=`date`
$ echo $MYDATE
Mon Feb 8 16:28:04 EST 2010
$ echo ${MYDATE##* }
2010

Upvotes: 10

Brian Agnew
Brian Agnew

Reputation: 272307

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.

Upvotes: 1

Jerry Coffin
Jerry Coffin

Reputation: 490218

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.

Upvotes: 1

Related Questions