我有这样的输出:
(2+05:10)
(10:19)
(00:45)
该输出代表一天中的小时和分钟。我想分别用“d”、“h”和“m”替换“:”,以获得这个
2d05h10m
10h19m
00h45m
目前,我尝试过,
sed -e 's/(//g; s/)//g; s/+/:/g'|awk '{split($0,s,":"); print s[1]"d" s[2]"h"s[3]"m"}')
这给出了(我在这里搞砸了!)
6d05h20m
3d15h17m
1d02h27m
00d08hm
00d11hm
02d25hm
02d30hm
16d50hm
5d00h39m
21d48hm
答案1
听上去像
< file tr '+:)' dhm | tr -d '('
会做的。或者更明确地匹配该模式:
sed 's/(\([0-9]\{1,\}\)+\([0-9]\{1,\}\):\([0-9]\{1,\}\))/\1d\2h\3m/g
s/(\([0-9]\{1,\}\):\([0-9]\{1,\}\))/\1h\2m/g' < file
通过某些sed
实现,您可以使用扩展正则表达式来简化:
sed -E 's/\(([0-9]+)\+([0-9]+):([0-9]+)\)/\1d\2h\3m/g
s/\(([0-9]+):([0-9]+)\)/\1h\2m/g' < file
或者使用单个s
命令perl
:
perl -pe 's{\((?:(\d+)\+)?(\d+):(\d+)\)}{
($1 && "$1d") . "$2h$3m"}ge' < file
或者:
perl -pe 's/\((\d*\+?\d+:\d+\))/$1 =~ y|+:)|dhm|r/ge' < file
答案2
一个简单(但效率稍低)的 perl 脚本就足够了
cat output_file | perl simple.pl
简单.pl
@seps=split /:/,"d:h:m";
while(<>) {
chomp ;
$_=~s/[\(\)]//g; #remove braces
@x = split /:/, $_; #get parts
$s="";
for($i=$#x,$j=$#sep;$i>=0; $i--,$j--) { #iterate on parts and corresponding separator
$s=$x[$i].$seps[$j].$s; #create print string
}
print "$s\n"; #print this line
}
答案3
你缺少一个awk
解决方案。这是省略零个元素的一个:
解析.awk
# Start with empty variables
{ d = h = m="" }
# Determine format and set variables accordingly.
# Force numeric interpretation by multiplying by one
NF==5 { d = $2*1; h = $3*1; m = $4*1 }
NF==4 { d = "" ; h = $2*1; m = $3*1 }
# Only set value if the number is greater than zero
{
d = d > 0 ? d "d" : ""
h = h > 0 ? h "h" : ""
m = m > 0 ? m "m" : ""
}
length(d h m) > 1 { print d h m }
length(d h m) == 0 { print "Now" }
像这样运行它:
awk -f parse.awk -v FS='[()+:]' file
输出:
2d5h10m
10h19m
45m