linux系统中给数据加上列号

 

1、测试数据

[root@centos7 test]# cat a.txt
e d g e
s d g w
a x d g
n d i d

 

2、

[root@centos7 test]# cat a.txt
e d g e
s d g w
a x d g
n d i d
[root@centos7 test]# seq -s " " $(head -n 1 a.txt | awk '{print NF}')
1 2 3 4
[root@centos7 test]# seq -s " " $(head -n 1 a.txt | awk '{print NF}') | cat - a.txt
1 2 3 4
e d g e
s d g w
a x d g
n d i d

 

3、

[root@centos7 test]# cat a.txt
e d g e
s d g w
a x d g
n d i d
[root@centos7 test]# head -n 1 a.txt | awk '{for (i = 1; i <= NF; i++) printf("%s ", i); printf("\n")}'
1 2 3 4
[root@centos7 test]# head -n 1 a.txt | awk '{for (i = 1; i <= NF; i++) printf("%s ", i); printf("\n")}' | cat - a.txt
1 2 3 4
e d g e
s d g w
a x d g
n d i d

 

 

4、

[root@centos7 test]# cat a.txt
e d g e
s d g w
a x d g
n d i d
[root@centos7 test]# head -n 1 a.txt | awk '{for (i = 1; i < NF; i++) printf("%s ", i); print NF}'
1 2 3 4
[root@centos7 test]# head -n 1 a.txt | awk '{for (i = 1; i < NF; i++) printf("%s ", i); print NF}' | cat - a.txt
1 2 3 4
e d g e
s d g w
a x d g
n d i d

 

posted @ 2021-04-15 14:36  小鲨鱼2018  阅读(264)  评论(0编辑  收藏  举报