仅打印字符串中的第一个字段 [英] Printing only the first field in a string
本文介绍了仅打印字符串中的第一个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个日期为 12/12/2013 14:32
我想把它转换成只有 12/12/2013
.字符串可以是 1/1/2013 12:32
或 1/10/2013 23:41
我只需要日期部分.
I have a date as 12/12/2013 14:32
I want to convert it into only 12/12/2013
.
The string can be 1/1/2013 12:32
or 1/10/2013 23:41
I need only the date part.
推荐答案
您可以使用各种 Unix 工具轻松完成此操作:
You can do this easily with a variety of Unix tools:
$ cut -d' ' -f1 <<< "12/12/2013 14:32"
12/12/2013
$ awk '{print $1}' <<< "12/12/2013 14:32"
12/12/2013
$ sed 's/ .*//' <<< "12/12/2013 14:32"
12/12/2013
$ grep -o "^S+" <<< "12/12/2013 14:32"
12/12/2013
$ perl -lane 'print $F[0]' <<< "12/12/2013 14:32"
12/12/2013
这篇关于仅打印字符串中的第一个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文