TuxForLife
TuxForLife

Reputation: 219

Replacing dates with SED or AWK (or whatever works) - Linux regex

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

Answers (3)

Ed Morton
Ed Morton

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

Jotne
Jotne

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

anubhava
anubhava

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

Related Questions