我想將(ISO 8601)日期轉換為YYYY-MM-DD看起來像Mon, 03 Dec 2021 00:00:00 -0600(RFC-822 / RFC 5322)的日期。我一直無法找到解決這個特定問題的問題。
我有一個 RSS XML 檔案,如下所示:
bash-5.1$ cat feed.xml
<rss version="2.0">
<channel>
<title>Title string</title>
<link>https://domain/feed.xml</link>
<description>Description string here</description>
<language>en-us</language>
<item>
<title>title string here</title>
<link>link string with https style information</link>
<guid>link string with https style information</guid>
<pubDate>2021-12-03</pubDate>
</item>
<item>
<title>title string here</title>
<link>link string with https style information</link>
<guid>link string with https style information</guid>
<pubDate>2019-08-13</pubDate>
</item>
<item>
<title>title string here</title>
<link>link string with https style information</link>
<guid>link string with https style information</guid>
<pubDate>2018-11-23</pubDate>
</item>
</channel>
</rss>
決議后我期望的輸出是這樣的:
...
<pubDate>Fri, 03 Dec 2021 00:00:00 -0600</pubDate>
...
<pubDate>Tue, 13 Aug 2019 00:00:00 -0500</pubDate>
...
<pubDate>Fri, 23 Nov 2018 00:00:00 -0600</pubDate>
...
此輸出通常可以使用 bashdate命令實作,如下所示:
bash-5.1$ date -d "2018-11-23" "%a, %d %b %Y %T %z"
Fri, 23 Nov 2018 00:00:00 -0600
我正在嘗試使用它來完成此操作,awk因為我了解到命令替換是可能的,并且我相信我很接近,但并不完全:
bash-5.1$ awk -F "[><]" -v date="$(date "%a, %d %b %Y %T %z" -d "$3")" '/pubDate/ {print $3date}' feed.xml
2021-12-03Sun, 05 Dec 2021 00:00:00 -0600
2019-08-13Sun, 05 Dec 2021 00:00:00 -0600
2018-11-23Sun, 05 Dec 2021 00:00:00 -0600
似乎模式匹配和 date 命令都成功執行,但是 awk$3欄位看起來沒有被傳遞到 shelldate命令中,因此顯示的是當前時間而不是轉換后的時間。
如何$3將 awk 模式匹配中的欄位傳遞給date 命令,以便它可以根據欄位值轉換日期?
任何幫助是極大的贊賞!
uj5u.com熱心網友回復:
正如評論的那樣,一般來說建議使用xml決議工具。如果 xml 檔案與提供的示例對齊,bash或者awk
可能在有限的條件下作業。
使用 bash:
#!/bin/bash
while IFS= read -r line; do
if [[ $line =~ (.*<pubDate>)([0-9]{4}-[0-9]{2}-[0-9]{2})(</pubDate>.*) ]]; then
datestr=$(date -d "${BASH_REMATCH[2]}" "%a, %d %b %Y %T %z")
line="${BASH_REMATCH[1]}$datestr${BASH_REMATCH[3]}"
fi
echo "$line"
done < feed.xml
條件$line =~ (.*<pubDate>)([0-9]{4}-[0-9]{2}-[0-9]{2})(</pubDate>.*)將<pubdate>bash 變數的行${BASH_REMATCH[@]與括號中的子字串匹配。然后line使用重新格式化的日期字串重建。
如果gawk哪個支持mktime()和strftime()功能,你也可以說gawk:
awk '
{
if (match($0, /^(.*<pubDate>)([0-9]{4})-([0-9]{2})-([0-9]{2})(<\/pubDate>.*)/, a) ) {
ts = mktime(a[2] " " a[3] " " a[4] " 00 00 00") # timestamp since the epoch
datestr = strftime("%a, %d %b %Y %T %z", ts)
$0 = a[1] datestr a[5]
}
} 1' feed.xml
uj5u.com熱心網友回復:
awk稍微改變你的代碼。
$ awk -v date="$(date "%a, %d %b %Y %T %z")" 'BEGIN {FS=OFS=">"}$1~/pubDate/{split($2,a,"<"); a[1]=date; $2=a[1]"<"a[2]}1' input_file
<rss version=2.0>
<channel>
<title>Title string</title>
<link>https://domain/feed.xml</link>
<description>Description string here</description>
<language>en-us</language>
<item>
<title>title string here</title>
<link>link string with https style information</link>
<guid>link string with https style information</guid>
<pubDate>Mon, 06 Dec 2021 00:00:00 0000</pubDate>
</item>
<item>
<title>title string here</title>
<link>link string with https style information</link>
<guid>link string with https style information</guid>
<pubDate>Mon, 06 Dec 2021 00:00:00 0000</pubDate>
</item>
<item>
<title>title string here</title>
<link>link string with https style information</link>
<guid>link string with https style information</guid>
<pubDate>Mon, 06 Dec 2021 00:00:00 0000</pubDate>
</item>
</channel>
</rss>
sed 如果設定了變數,也可以使用。
$ date="$(date "%a, %d %b %Y %T %z")"
$ sed "/pubDate/ s/....-..-../$date/" input_file
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/374927.html
上一篇:如何轉換CST時區中的日期列值
