我有这样的文件
$ cat trapetz
x = 0:0.0001:7pi
plot(x, sin(x).*cos(x))
Area = trapz(x, sin(x).*cos(x))
$ cat simpson
f = inline(sin(x).*cos(x));
Area2 = quad(f, 0, 7pi, 1e-16)
我想要这样的东西
$ cat -b -t MISSING? trapetz simpson
traapetz
1 x = 0:0.0001:7pi
2 plot(x, sin(x).*cos(x))
3 Area = trapz(x, sin(x).*cos(x))
simpson
1 f = inline(sin(x).*cos(x));
2 Area2 = quad(f, 0, 7pi, 1e-16)
或者如果有一些简单的方法在那里添加厕所就更好了:
$ find |tee |...|wc... I feel now reinventing the wheel, there must be some ready...
traapetz: xyz chars
1 x = 0:0.0001:7pi
2 plot(x, sin(x).*cos(x))
3 Area = trapz(x, sin(x).*cos(x))
simpson: zyx chars
1 f = inline(sin(x).*cos(x));
2 Area2 = quad(f, 0, 7pi, 1e-16)
但我得到
$ cat -b -t trapetz simpson
1 x = 0:0.0001:7pi
2 plot(x, sin(x).*cos(x))
3 Area = trapz(x, sin(x).*cos(x))
4 f = inline(sin(x).*cos(x));
5 Area2 = quad(f, 0, 7pi, 1e-16)
并不是真正需要 cat,而是一些简单的工具来共享和显示上面的代码片段,而不是 Pastebin。我想要一些标准的命令行东西。我正在尝试为 codegolf.se 创建简单的拼图粘贴,以便人们可以轻松地重现事物......
答案1
快速 shell 脚本:
#!/bin/sh
# usage: scriptname file1 file2 ...
for file in "$@"
do
[ -f "$file" ] || continue
set -- `wc "$file"`
echo "${file}: lines $1 words $2 bytes $3"
cat -b -t "$file"
done
它的行为类似于您的示例输出,因此丢失的文件将被默默地忽略。
答案2
一个非常粗略的 awk 实现:
BEGIN{
OLDFILENAME="";
}
FNR==1{
if (OLDFILENAME != "") {
printf("#### Processed (chars: %s - lines: %s)\n", FWC, FLC);
}
printf("#### Processing: %s\n", FILENAME);
OLDFILENAME=FILENAME;
FWC=0;
FLC=0;
}
{
printf("%04d - %s\n", FNR, $0);
FWC = FWC + length($0);
FLC = FLC + 1;
}
END{
if (OLDFILENAME != "") {
printf("#### Processed (chars: %s - lines: %s)\n", FWC, FLC);
}
}
执行awk -f AWKFILE trapetz simpson
得到:
#### Processing: trapetz
0001 - x = 0:0.0001:7pi
0002 - plot(x, sin(x).*cos(x))
0003 - Area = trapz(x, sin(x).*cos(x))
#### Processed (chars: 70 - lines: 3)
#### Processing: simpson
0001 - f = inline(sin(x).*cos(x));
0002 - Area2 = quad(f, 0, 7pi, 1e-16)
#### Processed (chars: 57 - lines: 2)
答案3
tail -n +1 trapetz simpson
将打印每个文件,并带有给出文件名的前导标题。nl trapetz simpson
打印行号但不打印文件名。你需要使用awk
或一些贝壳胶将两者结合起来。
for x in trapetz simpson; do
echo "$x: $(wc -c <"$x") bytes"
nl "$x"
done
这是一个 awk 解决方案,它在底部打印字节数:
awk '
FNR == 1 && NR != 1 {print "end", fn, chars, "characters"; bytes=0}
END {print "end", fn, chars, "characters"}
FNR == 1 {print "begin", FILENAME; fn=FILENAME}
1 {chars += 1 + length; printf "%7d ", FNR; print}
' trapetz simpson
答案4
该命令列表:
echo -e "trapetz\nsimpson" | xargs -I fn sh -c "wc -c fn | sed 's/\(.*\) \(.*\)/\2: \1 chars/';cat -b -t fn"
产生这个输出
trapetz: 73 chars
1 x = 0:0.0001:7pi
2 plot(x, sin(x).*cos(x))
3 Area = trapz(x, sin(x).*cos(x))
simpson: 59 chars
1 f = inline(sin(x).*cos(x));
2 Area2 = quad(f, 0, 7pi, 1e-16)
软件版本有:
- bash 4.2.53
- GNU sed 4.2.2
- xargs 4.5.11
- 猫8.21