Linux下为文件增加列的shell脚本

来源:互联网 发布:vc 多线程编程 编辑:程序博客网 时间:2024/06/06 00:33

场景:linux有份文件,需要增加一列序号来标记,通过shell脚本来实现。

步骤:

1)创建一份test.txt文件,内含一列,如下:

bash-4.1$ cat test.txtabcde

2)创建脚本row_id.sh,如下:

bash-4.1$ cat row_id.sh#!/bin/shlines=$1for ((i=1;i<=$lines;i++))do        echo $i>>test_id.txtdone

3)执行脚本:sh row_id.sh 5

生成5行序号,如下:

bash-4.1$ cat test_id.txt12345



4)合并两份文件,执行:

paste test_id.txt test.txt > test_and_id.txt

5)查看最终结果:

bash-4.1$ cat test_and_id.txt1       a2       b3       c4       d5       e