5 cat
我有这样的文件
$ 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)
Run Code Online (Sandbox Code Playgroud)
我想要这样的东西
$ 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)
Run Code Online (Sandbox Code Playgroud)
或者甚至更好,如果有一些简单的方法在那里添加 wc:
$ 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)
Run Code Online (Sandbox Code Playgroud)
但我明白了
$ 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)
Run Code Online (Sandbox Code Playgroud)
不是真的需要猫,而是一些简单的工具来共享和显示上面的代码片段,而不是 pastebin。我想要一些标准的命令行东西。我正在尝试为 codegolf.se 创建简单的拼图粘贴,以便人们可以轻松地重现事物...
快速 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
Run Code Online (Sandbox Code Playgroud)
它的行为类似于您的示例输出,因此丢失的文件将被默默地忽略。
一个非常粗略的 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);
}
}
Run Code Online (Sandbox Code Playgroud)
执行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)
Run Code Online (Sandbox Code Playgroud)