常用命令
常用命令_LINUX三剑客之文本处理
首先是源文本test.tcl
@@ cat test.tcl- switch_core_1 HEADBUFMDV32_140P7T35R + FIXED ( 1372600 30000 ) FS;
- switch_core_2 HEADBUFMDV32_140P7T35R + FIXED ( 1451000 30000 ) FS;
- switch_core_3 HEADBUFMDV32_140P7T35R + FIXED ( 1529400 30000 ) FS;
- switch_core_4 HEADBUFMDV32_140P7T35R + FIXED ( 1607800 30000 ) FS;
- switch_core_5 HEADBUFMDV32_140P7T35R + FIXED ( 1686200 30000 ) FS;
- switch_core_6 HEADBUFMDV32_140P7T35R + FIXED ( 1764600 30000 ) FS;
- switch_core_7 HEADBUFMDV32_140P7T35R + FIXED ( 3489400 30000 ) FS;
- switch_core_8 HEADBUFMDV32_140P7T35R + FIXED ( 3567800 30000 ) FS;
- switch_core_9 HEADBUFMDV32_140P7T35R + FIXED ( 3646200 30000 ) FS;
- switch_core_10 HEADBUFMDV32_140P7T35R + FIXED ( 3724600 30000 ) FS;
- switch_core_11 HEADBUFMDV32_140P7T35R + FIXED ( 3803000 30000 ) FS;
- switch_core_12 HEADBUFMDV32_140P7T35R + FIXED ( 3881400 30000 ) FS;
- switch_core_13 HEADBUFMDV32_140P7T35R + FIXED ( 1372600 58000 ) FS;
其次是最终要处理成的目标文本test.tcl2
@@ cat test.tcl2
addInst -inst switch_core_1 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_2 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_3 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_4 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_5 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_6 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_7 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_8 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_9 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_10 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_11 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_12 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_13 -cell HEADBUFMDV32_140P7T35R
步骤1 grep switch.def 中含switch* HEAD*这两列并将结果 > test.tcl2
命令详解:
grep “HEADBUFMDV32_140P7T35R” test.tcl | awk ‘{printf “%-20s%-20s\n”,$2,$3}’ > test.tcl2
#先 grep 再 awk 取出第二列、第三列($2,$3),并格式化输出(printf “%-20s%-20s\n”)
#Linux awk中的print和printf的比较
awk中print与printf的主要差别显示在以下两点:
- print在显示多个结果的时候以逗号分隔,结果将这几部分的内容自动使用分隔符进行分隔,且不需要添加换行符\n
- printf可以更加灵活的控制某一个字段的输出格式,通过使用诸如%-12s,%3.1f等格式化方法
#格式化输出–%m.n
switch_core_1 H
(-表示左对齐,省略则右对齐;
switch_core_1占13字符宽度,右边补7个空格,刚好20字符宽度;
即为%-20s)
@@ grep "HEADBUFMDV32_140P7T35R" test.tcl | awk '{printf "%-20s%-20s\n",$2,$3}' > test.tcl2
@@ cat test.tcl2
switch_core_1 HEADBUFMDV32_140P7T35R
switch_core_2 HEADBUFMDV32_140P7T35R
switch_core_3 HEADBUFMDV32_140P7T35R
switch_core_4 HEADBUFMDV32_140P7T35R
switch_core_5 HEADBUFMDV32_140P7T35R
switch_core_6 HEADBUFMDV32_140P7T35R
switch_core_7 HEADBUFMDV32_140P7T35R
switch_core_8 HEADBUFMDV32_140P7T35R
switch_core_9 HEADBUFMDV32_140P7T35R
switch_core_10 HEADBUFMDV32_140P7T35R
switch_core_11 HEADBUFMDV32_140P7T35R
switch_core_12 HEADBUFMDV32_140P7T35R
switch_core_13 HEADBUFMDV32_140P7T35R
步骤2 添加列操作
方式一 sed替换法实现
(适用于某列列首或列尾字符相同,如在第一列之前加列“addInst -inst”时,第一列列首有相同字符‘switch’;第二列之前加列“-cell”时,第一列列首有相同字符‘HEADBUF*’)
sed -i 表示直接写入
SED 简明教程
@@ sed -i "s# HEAD#-cell HEAD#g" test.tcl2
switch_core_1 -cell-cell HEADBUFMDV32_140P7T35R
switch_core_2 -cell-cell HEADBUFMDV32_140P7T35R
switch_core_3 -cell-cell HEADBUFMDV32_140P7T35R
switch_core_4 -cell-cell HEADBUFMDV32_140P7T35R
switch_core_5 -cell-cell HEADBUFMDV32_140P7T35R
switch_core_6 -cell-cell HEADBUFMDV32_140P7T35R
switch_core_7 -cell-cell HEADBUFMDV32_140P7T35R
switch_core_8 -cell-cell HEADBUFMDV32_140P7T35R
switch_core_9 -cell-cell HEADBUFMDV32_140P7T35R
switch_core_10 -cell-cell HEADBUFMDV32_140P7T35R
switch_core_11 -cell-cell HEADBUFMDV32_140P7T35R
switch_core_12 -cell-cell HEADBUFMDV32_140P7T35R
switch_core_13 -cell-cell HEADBUFMDV32_140P7T35R@@ sed -i "s#switch#addInst -inst switch#g" test.tcl2
addInst -inst switch_core_2 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_2 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_3 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_4 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_5 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_6 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_7 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_8 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_9 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_10 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_11 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_12 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_13 -cell HEADBUFMDV32_140P7T35R
方式二 awk列操作实现
awk作为列操作脚本语言,添加列显然比sed(行操作脚本语言)命令更合适。
linux 文本的行、列操作
AWK 简明教程
@@ grep "HEADBUFMDV32_140P7T35R" test.tcl | awk '{printf "%-18s%s\n",$2,$3}' > test_awk.tcl
@@ cat test_awk.tcl
switch_core_1 HEADBUFMDV32_140P7T35R
switch_core_2 HEADBUFMDV32_140P7T35R
switch_core_3 HEADBUFMDV32_140P7T35R
switch_core_4 HEADBUFMDV32_140P7T35R
switch_core_5 HEADBUFMDV32_140P7T35R
switch_core_6 HEADBUFMDV32_140P7T35R
switch_core_7 HEADBUFMDV32_140P7T35R
switch_core_8 HEADBUFMDV32_140P7T35R
switch_core_9 HEADBUFMDV32_140P7T35R
switch_core_10 HEADBUFMDV32_140P7T35R
switch_core_11 HEADBUFMDV32_140P7T35R
switch_core_12 HEADBUFMDV32_140P7T35R
switch_core_13 HEADBUFMDV32_140P7T35R
metho
@@ awk '{print "addInst -inst",$1,"-cell",$2}' test_awk.tcl ##在第一列之前加列“addInst -inst”,以及第一列、第二列之间加列“-cell”
addInst -inst switch_core_1 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_2 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_3 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_4 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_5 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_6 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_7 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_8 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_9 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_10 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_11 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_12 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_13 -cell HEADBUFMDV32_140P7T35R@@ awk '{print "addInst -inst",$1,"-cell",$2}' test_awk.tcl > test_awk.tcl2 ##输出结果写入test_awk.tcl2
@@ awk '{printf "%-10s %-10s %-20s %-10s %-20s\n",$1,$2,$3,$4,$5}' test_awk.tcl2 > test_awk.tcl3 ##格式化输出结果并写入到test_awk.tcl3,不能test_awk.tcl2 > test_awk.tcl2
@@ cat test_awk.tcl3
addInst -inst switch_core_1 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_2 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_3 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_4 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_5 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_6 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_7 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_8 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_9 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_10 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_11 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_12 -cell HEADBUFMDV32_140P7T35R
addInst -inst switch_core_13 -cell HEADBUFMDV32_140P7T35R@@ awk '$1=$1 " -cell "' test_awk.tcl ##在第一列后面加列;此方法适用于列数较多的情况下单独加列
switch_core_1 -cell HEADBUFMDV32_140P7T35R
switch_core_2 -cell HEADBUFMDV32_140P7T35R
switch_core_3 -cell HEADBUFMDV32_140P7T35R
switch_core_4 -cell HEADBUFMDV32_140P7T35R
switch_core_5 -cell HEADBUFMDV32_140P7T35R
switch_core_6 -cell HEADBUFMDV32_140P7T35R
switch_core_7 -cell HEADBUFMDV32_140P7T35R
switch_core_8 -cell HEADBUFMDV32_140P7T35R
switch_core_9 -cell HEADBUFMDV32_140P7T35R
switch_core_10 -cell HEADBUFMDV32_140P7T35R
switch_core_11 -cell HEADBUFMDV32_140P7T35R
switch_core_12 -cell HEADBUFMDV32_140P7T35R
switch_core_13 -cell HEADBUFMDV32_140P7T35R