Linux 如何在包含的时间间隔内对文本文件中的项目进行排序
我正在编写一个bash脚本,根据条目上的预设时间间隔对文本文件中的列表进行排序,在本例中,我有一个名为Linux 如何在包含的时间间隔内对文本文件中的项目进行排序,linux,bash,sorting,grep,Linux,Bash,Sorting,Grep,我正在编写一个bash脚本,根据条目上的预设时间间隔对文本文件中的列表进行排序,在本例中,我有一个名为final.txt的文本文件,其中包含以下行: 14-Oct-2013|04:18| 14-Oct-2013|04:16| 14-Oct-2013|04:20| 14-Oct-2013|05:16| 14-Oct-2013|06:16| 我将在一个文本文件上显示条目,从04:16到05:16,按顺序,按时间顺序。所以它将显示如下: 14-Oct-2013|04:18| 14-Oct-2013|
final.txt
的文本文件,其中包含以下行:
14-Oct-2013|04:18|
14-Oct-2013|04:16|
14-Oct-2013|04:20|
14-Oct-2013|05:16|
14-Oct-2013|06:16|
我将在一个文本文件上显示条目,从04:16到05:16,按顺序,按时间顺序。所以它将显示如下:
14-Oct-2013|04:18|
14-Oct-2013|04:16|
14-Oct-2013|04:20|
14-Oct-2013|05:16|
并且结果不应显示最后一行,因为最后一行超出了设置的时间间隔(04:16到05:16)
我尝试了grep的开始时间间隔,但我不知道如何在开始和结束时间间隔内对条目进行排序。我想再次通过管道将其发送给grep,以便对结束间隔时间进行排序,但是,我知道,这不会显示任何内容
无论如何,这是我的代码:
cat final.txt | grep $DATE_TO_SORT | sort | grep $HOUR_TO_SORT | sort
这里有人有解决办法吗?请使用排序的-k选项按列排序。 我使用
tr
将数据转换为列数据
user@m:/tmp$ cat final.txt | tr "|" "\t" | sort -k 2
14-Oct-2013 04:16
14-Oct-2013 04:18
14-Oct-2013 04:20
14-Oct-2013 05:16
14-Oct-2013 06:16
由于
perl
在任何unix版本中都可用,并且您可以从命令行使用它,因此我将以以下方式使用它:
$ perl -n -e 'print "$_" if (/(.+)\|(.+)\|/ && $1 eq "14-Oct-2013" && $2 >= "04:18" && $2 <= "05:16")' | sort
14-Oct-2013|04:16|
14-Oct-2013|04:18|
14-Oct-2013|04:20|
14-Oct-2013|05:16|
要读取sh vars,您只需正确使用引号和$,以便shell可以扩展变量,这里有两种情况使用单引号或双引号来包含perl参数:
$ FROM="14-Oct-2013|04:16"
$ TO="14-Oct-2013|05:17"
$ perl -n -e 'print $_ if ($_ ge "'$FROM'" && $_ lt "'$TO'")' | sort
or
$ perl -n -e "print \$_ if (\$_ ge '$FROM' && \$_ lt '$TO')" | sort
这将满足您的要求,并适用于任何日期/时间范围
#!/bin/bash
fn=test.txt
sn=${fn}.sorted
# date to search for
dn=14-Oct-2013
# start time
st=04:16
# end time
et=05:16
# get the output in date/time order
sort -V $fn >$sn
# find the first line number that is of interest (date / time)
startline=`grep $dn $sn |grep -n $st|head -n 1|cut -d: -f1`
# adjust to make results inclusive, comment this to make exclusive
startline=$(( startline - 1 ))
# find the last line number of interest
endline=`grep $dn $sn |grep -n $et |tail -n 1|cut -d: -f1`
# get the total number of lines
numlines=`wc -l $sn|cut -d" " -f 1`
# compute the number of lines to display
displines=$(( endline - startline ))
# compute the window size to search within
windowstart=$(( numlines - startline ))
# display the relevent lines from $startline to $endline
tail -n $windowstart $sn |head -n $displines
“后一行”是什么意思?所有这一切都是删除
和排序,这可以通过一个命令sort-t'.'-k2文件
从04:16到05:16
设置时间间隔(04:16到05:16)
实现。无论如何,我如何将bash变量用于perl?perl似乎没有读取bash变量
#!/bin/bash
fn=test.txt
sn=${fn}.sorted
# date to search for
dn=14-Oct-2013
# start time
st=04:16
# end time
et=05:16
# get the output in date/time order
sort -V $fn >$sn
# find the first line number that is of interest (date / time)
startline=`grep $dn $sn |grep -n $st|head -n 1|cut -d: -f1`
# adjust to make results inclusive, comment this to make exclusive
startline=$(( startline - 1 ))
# find the last line number of interest
endline=`grep $dn $sn |grep -n $et |tail -n 1|cut -d: -f1`
# get the total number of lines
numlines=`wc -l $sn|cut -d" " -f 1`
# compute the number of lines to display
displines=$(( endline - startline ))
# compute the window size to search within
windowstart=$(( numlines - startline ))
# display the relevent lines from $startline to $endline
tail -n $windowstart $sn |head -n $displines