简体   繁体   中英

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.

$ 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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM