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
原文地址:https://www.cnblogs.com/liujiaxin2018/p/14662362.html