重复打印文件首行n次

使用awk方式:

[root@x112 linshi]# cat 10_for.sh
#!/bin/bash
#describtion awk print first line ten time

Line=$(cat c.txt|wc -l)
awk '{if(NR==1) {for(i=1;i<='"$Line"';i++) {print $0}}}' c.txt

在awk中直接引用变量,使用"'$Line'" ,注意使用前格式必须是先用单引号括住再用双引号括住,或者使用-v

[root@x112 linshi]# awk -v a=3 'BEGIN{print a}' c.txt 
3
[root@x112 linshi]# awk -va=996 'BEGIN{print a}' c.txt 
996

 

awk统计文件行数

[root@x112 linshi]# awk 'NR==FNR{a++}END{print a}' c.txt c.txt 
4

  

 

使用sed方式:

[root@x112 linshi]# sed '1h;2,$g'  c.txt 
1.1.1.1
1.1.1.1
1.1.1.1
1.1.1.1

 

原文地址:https://www.cnblogs.com/xiaofeng666/p/13870278.html