修剪文本并添加时间戳?

发布于 2024-12-11 10:02:11 字数 312 浏览 0 评论 0原文

所以基本上我的输出如下:

<span id="PlayerCount">134,015 people currently online</span>

我想要的是一种修剪它以显示的方法:

134,015 - 3:24:20AM - Oct 24

任何人都可以帮忙吗?另请注意,数字可能会发生变化,因此是否可以输出“>”之间的所有内容和当前的“c”?并以某种方式添加时间戳?

在 Linux 中使用终端命令,这就是 bash 对吗?

So basically I have my output as the following:

<span id="PlayerCount">134,015 people currently online</span>

What I want is a way to trim it to show:

134,015 - 3:24:20AM - Oct 24

Can anyone help? Also note the number may change so is it possible output everything between ">" and the "c" in currently? And add a timestamp somehow?

Using commands from terminal in Linux, so that's called bash right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ヤ经典坏疍 2024-12-18 10:02:11

您的意思可能是:

$ echo '<span id="PlayerCount">134,015 people currently online</span>' | sed
    -e 's/^[^>]*>//'
    -e "s/currently.*$/$(date '+%r %b %d %Y')/"

它会生成:

134,015 people 03:36:30 PM Oct 24 2011

echo 仅用于测试数据。第一个 sed 命令会将第一个 > 字符之前的所有内容更改为空(即删除它)。

第二个将更改从 currently 到行尾的所有内容,并以您想要的格式显示当前日期(尽管我已经添加了年份,因为我对细节有点执着)。

这里 date 的相关参数是:

%r     locale's 12-hour clock time (e.g., 11:11:04 PM)
%b     locale's abbreviated month name (e.g., Jan)
%d     day of month (e.g., 01)
%Y     year

格式说明符的完整列表可以从 date 手册页获取(从 shell 执行 man date )。


一个小脚本可以从您在评论中提到的页面中为您提供所需的信息:

#!/usr/bin/bash
wget --output-document=- http://runescape.com/title.ws 2>/dev/null \
    | grep PlayerCount \
    | head -1l \
    | sed 's/^[^>]*>//' \
    | sed "s/currently.*$/$(date '+%r %b %d %Y')/"

运行此脚本可以得到:

pax$ ./online.sh
132,682 people 04:09:17 PM Oct 24 2011

详细信息:

  • wget 位会下拉网页并将其写入标准输出。标准错误(进度条)被丢弃。
  • grep 仅提取其中包含单词 PlayerCount 的行。
  • head 丢弃除第一个之外的所有内容。
  • 第一个 sed 剥离到第一个 > 字符。
  • 第二个 sed 将尾随文本更改为当前日期和时间。

Do you perhaps mean something like:

$ echo '<span id="PlayerCount">134,015 people currently online</span>' | sed
    -e 's/^[^>]*>//'
    -e "s/currently.*$/$(date '+%r %b %d %Y')/"

which generates:

134,015 people 03:36:30 PM Oct 24 2011

The echo is just for the test data. The first sed command will change everything up to the first > character into nothing (ie, delete it).

The second one will change everything from the currently to the end of the line with the current date in your desired format (although I have added the year since I'm a bit of a stickler for detail).

The relevant arguments for date here are:

%r     locale's 12-hour clock time (e.g., 11:11:04 PM)
%b     locale's abbreviated month name (e.g., Jan)
%d     day of month (e.g., 01)
%Y     year

A full list of format specifiers can be obtained from the date man page (execute man date from a shell).


A small script which will give you the desired information from the page you mentioned in the comments is:

#!/usr/bin/bash
wget --output-document=- http://runescape.com/title.ws 2>/dev/null \
    | grep PlayerCount \
    | head -1l \
    | sed 's/^[^>]*>//' \
    | sed "s/currently.*$/$(date '+%r %b %d %Y')/"

Running this gives me:

pax$ ./online.sh
132,682 people 04:09:17 PM Oct 24 2011

In detail:

  • The wget bit pulls down the web page and writes it on standard output. The standard error (progress bar) is thrown away.
  • The grep extracts only lines with the word PlayerCount in them.
  • The head throws away all but the first of those.
  • The first sed strips up to the first > character.
  • The second sed changes the trailing text to the durrent date and time.
墨小墨 2024-12-18 10:02:11

快速破解(tm):

$ people=$(echo '<span id="PlayerCount">134,015 people currently online</span>' | \
           sed -e 's/^.*>\(.*\) people.*$/\1/')
$ echo $people - $(date)
134,015 - Mon Oct 24 09:36:23 CEST 2011

Quickhack(tm):

$ people=$(echo '<span id="PlayerCount">134,015 people currently online</span>' | \
           sed -e 's/^.*>\(.*\) people.*$/\1/')
$ echo $people - $(date)
134,015 - Mon Oct 24 09:36:23 CEST 2011
怎樣才叫好 2024-12-18 10:02:11
produce_OUTPUT | grep -o '[0-9,]\+' | while read count; do 
  printf "%s - %s\n" $count "$(date +'%l:%M:%S %p - %b %e')"
done
produce_OUTPUT | grep -o '[0-9,]\+' | while read count; do 
  printf "%s - %s\n" $count "$(date +'%l:%M:%S %p - %b %e')"
done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文