Reputation: 219
I would like convert:
Charlie answered 9 years ago
random text
Kevin answered 4 months ago
To this:
Charlie answered around March 2006
random text
Kevin answered around November 2014
Using the following code:
date "+%B %Y" --date="9 years ago"
is where I got March 2006 from.
Should I use a for loop since I will be using instances where there will be 10+ dates that say "answered ______ ago"
Which program is recommended? Sed, awk, any more?
Thank you, I am doing this for a Professor who wants to do research on the accuracy of Yahoo Answers, and I will really appreciate your help.
Upvotes: 2
Views: 90
Reputation: 203358
$ 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
Upvotes: 3
Reputation: 41456
Here is one more 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
Upvotes: 1
Reputation: 785098
Doing it in BASH:
while read -r line; do
[[ $line != *answered* ]] && echo "$line" && continue
date "+${line/answered */answered} around %B %Y" -d "${line#* answered }"
done < file
Output:
Charlie answered around March 2006
random text
Kevin answered around November 2014
Upvotes: 1