Answers:
我想知道这是否会变成一场高尔夫比赛:
sed 'p;p;p'
awk '1;1;1;1'
perl -lpE 'say;say;say' # if Paul McCartney and Michael Jackson were hackers...
说明:
sed的p命令是打印当前行。默认行为是在移至下一行之前打印当前行(这就是sed必须-n允许您将其关闭的原因)。一些较老的sed没有分号(我认为),因此您可能必须这样做sed -e p -e p -e p
Awk可condition {action}成对使用。如果省略该动作,则默认为在条件返回true时打印当前行。与许多类似C的语言一样,Awk被1视为真实。(为完整起见,如果省略条件,将对每个记录执行该操作。)
许多perl函数都利用“默认”变量。这种单行代码等效于(在perl 5.16上):
$ perl -MO=Deparse -lpE 'say;say;say'
BEGIN { $/ = "\n"; $\ = "\n"; }
use feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval';
LINE: while (defined($_ = <ARGV>)) {
chomp $_;
say $_;
say $_;
say $_;
}
continue {
die "-p destination: $!\n" unless print $_;
}
1awk 到底在做什么?简写为print $0?
1(true); 默认块等效于{print}。因此,该语句1意味着要打印当前的行缓冲区({print})。({print}与{print $0}qed基本相同)
sed不支持将分号用作命令终止符;真的有这样的事吗?
sed -n '{p;p;p;p;}' file
awk '{print;print;print;print;}' file
你可以这样做,而不需要sed,perl或awk。
$ for i in `cat <file>` ; do seq <#> <#> | xargs -i -- echo $i ; done
或使用while循环:
$ while read i ; do seq <#> <#> | xargs -i -- echo $i ; done < <file>
$ for i in `cat sample.txt` ; do seq 1 3 | xargs -i -- echo $i ; done
a
a
a
b
b
b
c
c
c
while循环
$ while read i; do seq 1 2| xargs -i -- echo $i;done < sample.txt
a
a
b
b
c
c
使用纯壳:
repeats=4
while read line; do yes $line|head --lines=$repeats; done < infile > outfile
--help字符串而不是选项吗?[考虑以下情况line is --help或其他选项的情况]
yes -- --help
yes和head表示这不是“纯壳”
@potong的答案不适用于空行,替换\+为*应该可以解决此问题:
sed 'h;:a;s/[^\n]*/&/4;t;G;ba' file
awk的for如果只有一个命令重复没有需要括号。而perl更简单,如果你使用的foreach循环:for$i(0..3){print}。