开发者

basics of using cut awk grep and sed

开发者 https://www.devze.com 2022-12-19 08:20 出处:网络
I\'m trying to extract the year from this output : sa开发者_开发知识库m@sam-laptop:~/shell$ date

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

sa开发者_开发知识库m@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 !


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


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


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.


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.


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消