我有一个数据列表,例如 CSV,但有些行缺少值。我想根据使用 linux shell 脚本之前和之后的行生成缺失行的值。
以这张表为例。
线 | 人 | 年龄 |
---|---|---|
1 | 亚当 | 45 |
2 | 鲍勃 | 50 |
3 | 辛迪 | 47 |
4 | * | # |
5 | 埃德 | 49 |
我想做的是用“Cindy:Ed”(B 列中每个方向上最近的有效数据与“:”分隔符的串联)和“#”填充第 4 行中的“*” 48(47 和 49 的平均值,C 列每个方向上最接近的有效数据点)。
输出:
线 | 人 | 年龄 |
---|---|---|
1 | 亚当 | 45 |
2 | 鲍勃 | 50 |
3 | 辛迪 | 47 |
4 | 辛迪:艾德 | 48 |
5 | 埃德 | 49 |
我的数据被格式化为任意行数的空格分隔的文本文件。所有行都是三列。
虽然我了解 For 循环和 grep 等,但我不知道如何在普通的 linux shell 脚本中处理这个问题。
我的猜测是进行初始遍历以查找带有星号和散列的行。然后进行第二遍,分别将前后行的星号替换为 (awk '{print $2}'):(awk '{print $2}') 。
如果丢失的数据位于第一行或最后一行,我很乐意将其保留原样。如果连续行上缺少数据,我可以将所有丢失的行设置为相同的“Cindy:Ed”和相同的平均值。如果我可以设置“Cindy:Ed:1”和 Cindy:Ed:2”等,那就更酷了。
最坏情况下原始输入的准确示例:(它是一个跟踪路由,其中添加了“#”来表示丢失的延迟)
1 192.168.200.2 1
2 192.168.200.1 1
3 10.10.10.1 1
4 11.22.33.44 2
5 11.22.33.55 5
6 * #
7 11.22.44.66 9
8 * #
9 * #
10 8.8.8.0 25
11 * #
12 * #
13 * #
我想要什么:
1 192.168.200.2 1
2 192.168.200.1 1
3 10.10.10.1 1
4 11.22.33.44 2
5 11.22.33.55 5
6 11.22.33.55:11.22.44.66 7
7 11.22.44.66 9
8 11.22.44.66:8.8.8.0 17
9 11.22.44.66:8.8.8.0 17
10 8.8.8.0 25
11 * #
12 * #
13 * #
答案1
和awk
:
#if a previous line with proper IP has been read
oldip != "" {
#i is counter for consecutive invalid lines
i=0
#if IP is set, just print and go to next record
if ($2!="*") {
print ; oldip=$2 ; oldlat=$3 ; next
}
#otherwise get following line and increase counter
else {
#getline exit status => fails for the last line
while (getline > 0) {i++
#check if new line has IP, if so
#set IPold:IPnew and average latency value
if ($2!="*") {
ipfill=oldip":"$2 ; latfill=(oldlat+$3)/2
#print filler lines for all consecutive records without value
for (j=1 ; j<=i ; j++) {
print NR-i+j-1,ipfill,latfill
#alternative printing with oldIP:newIP:counter
# print NR-i+j-1,ipfill":"j,latfill
}
#save current IP+lat and print "good" line
oldp=$2; oldlat=$3
print ; next
}
}
}
#in case getline failed => all previous lines had no value
#just fill them with N/A data as in input
for (j=0 ; j<=i ; j++) {
print NR-i+j,"*","#"
}
}
#If leading lines have no IP value, print them until IP is found
oldip == "" { if ($2=="*") {print ; next} ; oldip=$2 ; oldlat=$3 ; print }
输入:
1 * #
2 * #
3 10.10.10.1 1
4 11.22.33.44 2
5 11.22.33.55 5
6 * #
7 11.22.44.66 10
8 * #
9 * #
10 8.8.8.0 25
11 * #
12 * #
13 * #
输出:
1 * #
2 * #
3 10.10.10.1 1
4 11.22.33.44 2
5 11.22.33.55 5
6 11.22.33.55:11.22.44.66 7.5
7 11.22.44.66 10
8 11.22.33.55:8.8.8.0 17.5
9 11.22.33.55:8.8.8.0 17.5
10 8.8.8.0 25
11 * #
12 * #
13 * #
带有计算行计数器的替代输出:
1 * #
2 * #
3 10.10.10.1 1
4 11.22.33.44 2
5 11.22.33.55 5
6 11.22.33.55:11.22.44.66:1 7.5
7 11.22.44.66 10
8 11.22.33.55:8.8.8.0:1 17.5
9 11.22.33.55:8.8.8.0:2 17.5
10 8.8.8.0 25
11 * #
12 * #
13 * #
答案2
$ cat tst.awk
$2 == "*" {
buf[++bufSz] = $0
next
}
bufSz > 0 {
split(prev,p)
rng = p[2] ":" $2
val = ($3 + p[3]) / 2
for (i=1; i<=bufSz; i++) {
split(buf[i],flds)
print (prev == "" ? buf[i] : flds[1] OFS rng OFS val)
}
bufSz = 0
}
{
print
prev = $0
}
END {
for (i=1; i<=bufSz; i++) {
print buf[i]
}
}
$ awk -f tst.awk file
1 192.168.200.2 1
2 192.168.200.1 1
3 10.10.10.1 1
4 11.22.33.44 2
5 11.22.33.55 5
6 11.22.33.55:11.22.44.66 7
7 11.22.44.66 9
8 11.22.44.66:8.8.8.0 17
9 11.22.44.66:8.8.8.0 17
10 8.8.8.0 25
11 * #
12 * #
13 * #
答案3
GNU sed 使用它的扩展正则表达式模式 (-E)
S='(\S+)'; _re="$S $S"
re="^$_re\\n$_re\$"
_avg='1k\4 \2+2/f'
avg='"$(echo '"'$_avg'"'|dc)"'
sed -E '
s/^(\S+ )[*] #(\n.*\n(.*))/\1\3\2/
ta
s/\n.*//
/[*] #$/b
$!N;//!ba
:loop
${//q;bb}
N;//bloop
:b;h
s/\n.*\n/\n/
s/^\S+ //Mg'"
s#$re#echo '\1:\3' $avg#e
x;G
:a
P;D
" file
Perl 使用范围运算符
perl -lane 'print,next
unless my $e = /\d$/ ... /\d$/;
push @A,[@F]; next
unless $e =~ /E0/ || eof;
if (@A>2&&$A[-1][-1] =~ /\d/) {
my($str,$avg);
for (0,-1) {
$avg += $A[$_][2] / 2.0;
$str .= $A[$_][1] . ":";
}
$str =~ s/.$//;
@{$A[$_]}[1,2] = ($str,$avg)
for 1..$#A-1;
}
print "@$_" for splice @A,0,@A-(eof?0:1);
@A=(); redo if ! eof;
' file
python3 -c 'import sys, itertools as it
prev = ""
p = lambda x: print(*x,sep="",end="")
q = lambda x: x.split()
g = lambda x: x.endswith("* #\n")
with open(sys.argv[1]) as f:
for t in it.groupby(f,g):
G = list(t[1])
if prev == "":
p(G)
if not t[0]: prev = G[-1]
else:
if t[0]: M = G
else:
a,b = map(q,[prev,G[0]])
x = f"{a[1]}:{b[1]}"
y = sum(map(int,[a[2],b[2]]))/2.0
for l in M:
for e in q(l)[0]:
print(e,x,y)
p(G); prev = G[-1]
p(M)
' file
$ cat file
1 * #
2 * #
3 10.10.10.1 1
4 11.22.33.44 2
5 11.22.33.55 5
6 * #
7 11.22.44.66 10
8 * #
9 * #
10 8.8.8.0 25
11 * #
12 * #
13 * #
输出;
1 * #
2 * #
3 10.10.10.1 1
4 11.22.33.44 2
5 11.22.33.55 5
6 11.22.33.55:11.22.44.66 7.5
7 11.22.44.66 10
8 11.22.44.66:8.8.8.0 17.5
9 11.22.44.66:8.8.8.0 17.5
10 8.8.8.0 25
11 * #
12 * #
13 * #
答案4
var1=$(awk '{a[++i]=$0}/#/{for(x=NR-1;x<NR;x++)print a[x]}' file.txt | awk '{print $NF}')
var2=$(awk '/#/{x=NR+1}(NR==x){print $NF}' file.txt)
sed -i "s/#/$var3/g" file.txt
sed -i "s/\*/Cindy:Ed/g" file.txt
output
cat file.txt
line person age
1 Adam 45
2 Bob 50
3 Cindy 47
4 Cindy:Ed 48
5 Ed 49