我想转换:
Charlie answered 9 years ago
random text
Kevin answered 4 months ago
对此:
Charlie answered around March 2006
random text
Kevin answered around November 2014
使用以下代码:
date "+%B %Y" --date="9 years ago"
是我 2006年3月的地方。
我是否应该使用for循环,因为我将使用10个以上的日期说“回答______之前”
建议使用哪个程序? sed,awk,还有吗?
谢谢,我正在为一位想要研究Yahoo Answers准确性的教授这样做,我将非常感谢你的帮助。
答案 0 :(得分:3)
$ cat tst.awk
/answered/ && match($0,/[0-9]+[[:space:]]+[[:alpha:]]+[[:space:]]+ago/) {
cmd = "date \"+%B %Y\" --date=\"" substr($0,RSTART,RLENGTH) "\""
if ( (cmd | getline date) > 0 ) {
$0 = substr($0,1,RSTART-1) "around " date
}
close(cmd)
}
{ print }
$ awk -f tst.awk file
Charlie answered around March 2006
random text
Kevin answered around November 2014
答案 1 :(得分:1)
在BASH中执行:
while read -r line; do
[[ $line != *answered* ]] && echo "$line" && continue
date "+${line/answered */answered} around %B %Y" -d "${line#* answered }"
done < file
<强>输出:强>
Charlie answered around March 2006
random text
Kevin answered around November 2014
答案 2 :(得分:1)
这是另外一个awk
awk -F"answered " 'NF>1{"date \"+%B %Y\" --date=\""$2"\"" | getline t;$2=FS "around "t}1' file
Charlie answered around March 2006
random text
Kevin answered around November 2014