1

我有一个带有此类日期字符串的 txt 文件:2011-05-21 19:30:00 我想搜索所有 txt 文件并将这个“年-月-日”字符串替换为“日 MonthInLetter 年”

搜索和替换。

还有两个参数。我想在日期之后删除小时。并且只影响meta_value>前面有字符串的小时。

meta_value>2011-07-24 22:00:00

变得

meta_value>24 July 2011

你能帮助我吗?

谢谢!

编辑:

文件解析如下:

 <item>
      <title><![CDATA[UN VIOLON SUR LE TOIT AH, SI J’ETAIS RICHE! ]]></title>
      <pubDate/>
      <dc:creator>admin</dc:creator>
      <description/>
      <content:encoded/>
      <wp:postmeta>
        <wp:meta_key>evcal_start_date</wp:meta_key>
        <wp:meta_value>2010-02-04 11:00:00</wp:meta_value>
      </wp:postmeta>
      <wp:postmeta>
        <wp:meta_key>evcal_end_date</wp:meta_key>
        <wp:meta_value>2010-07-31 20:00:00</wp:meta_value>
      </wp:postmeta>
      <wp:postmeta>
        <wp:meta_key>evcal_location</wp:meta_key>
        <wp:meta_value>8 rue du Fbg Montmartre, 75009 Paris</wp:meta_value>
      </wp:postmeta>
      <wp:postmeta>
        <wp:meta_key>evcal_organizer</wp:meta_key>
        <wp:meta_value>Le Palace</wp:meta_value>
      </wp:postmeta>
      <wp:postmeta>
        <wp:meta_key>evcal_allday</wp:meta_key>
        <wp:meta_value>no</wp:meta_value>
      </wp:postmeta>
      <wp:post_date/>
      <wp:post_date_gmt/>
      <wp:comment_status/>
      <wp:ping_status>open</wp:ping_status>
      <wp:post_name>un-violon-sur-le-toit-ah-si-jretais-riche-</wp:post_name>
      <wp:status/>
      <wp:post_parent>0</wp:post_parent>
      <wp:menu_order>0</wp:menu_order>
      <wp:post_type>ajde_events</wp:post_type>
      <wp:post_password/>
      <wp:is_sticky>0</wp:is_sticky>
      <category domain="category" nicename=""/>
    </item>
4

5 回答 5

1

Using date, you can convert from the first date format to the second:

date --date='2011-07-24 22:00:00' '+%d %B %Y'

It doesn't work in Mac OS X

In Mac OS X, you need to use the BSD form of the date command:

$ date -j -f"%Y-%m-%d %H:%M:%S" "2011-07-24 22:00:00" +"%d %B %Y" 
24 July 2011
  • The -j says _Don't set the date"
  • The -f takes two arguments: The date format and the date. "%Y-%m-%d %H:%M:%S" is the format and "2011-07-24 22:00:00" is the date.
  • The "+" is the format you want the output to be.

You can do a man strftime from the Terminal window to see the various date format characters.


Addendum

can i replace "2011-07-24 22:00:00" by a file containing many things but also dates in this format? dansayag

You couldn't use sed, but you could read the file line-by-line and munge the lines if they contain a date. Untested:

while read line
    do
    if ! grep -q "<wp:meta_value>"
    then
        echo "$line" >> $new_file_name
    else
        date=${line#<wp:meta_value>}  #Removes prefix
        date=${date%</wp:meta_value>} #Removes suffix
        date=$(date -j -f"%Y-%m-%d %H:%M:%S" "$date" +"%d %B %Y")
        echo "<wp:meta_value>$date</wp:meta_value>" >> $new_file_name
    fi
done < FileName

See man bash and look at the section about ${parameter#word} and ${parameter%word} for an explanation of the ${line#<wp:meta_value>} syntax.

于 2013-06-05T18:23:58.247 回答
1

您可以使用 perl“单线”来执行此操作:

perl -wi -pe 'BEGIN{@month=('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')};
s/meta_value>(\d\d\d\d)-(\d\d)-(\d\d) \d\d:\d\d:\d\d/"meta_value>$3 $month[0+$2] $1"/eg'

根据需要调整月份名称。

于 2013-06-05T17:14:08.887 回答
0

使用date,您可以从第一种日期格式转换为第二种:

date --date='2011-07-24 22:00:00' '+%d %B %Y'

结果:

24 July 2011
于 2013-06-05T17:16:21.757 回答
0

使用sed

$ cat file
meta_value>2011-07-24 22:00:00
meta_value>2011-12-25 22:00:00

$ sed -re 's/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/\0 01Jan02Feb03Mar04Apr05May06Jun07Jul08Aug09Sep10Oct11Nov12DecEND/g' -e 's/([0-9]{4})-([0-9]{2})-([0-9]{2}) [0-9]{2}:[0-9]{2}:[0-9]{2} .*\2(...).*END/\3 \4 \1/g' file

meta_value>24 Jul 2011
meta_value>25 Dec 2011

这个想法是将一串月份数字添加到月份名称映射中并将其用作查找。

于 2013-06-05T17:18:21.253 回答
0

您应该始终考虑python,rubyperl对这类字符串进行处理。

#!/usr/bin/env python
import time

s = 'meta_value>2011-07-24 22:00:00'
d = s[s.index('>') + 1:]
ds = time.strftime('%d %B %Y', time.strptime(d, '%Y-%m-%d %H:%M:%S'))
print "%s%s" % (s[:s.index('>') + 1], ds)

此外,python 代码干净且易于阅读。

于 2013-06-05T17:22:25.650 回答