仅排序文件的一列

时间:2011-11-23 04:27:58

标签: file bash

我有一个像这样的文件

/device/000:004:00.7/vmkname = "vmnic8"

/device/000:005:00.0/vmkname = "vmnic0"

/device/000:005:00.1/vmkname = "vmnic4"

/device/000:005:00.2/vmkname = "vmnic10"

/device/000:005:00.3/vmkname = "vmnic11"

.... 15行

我想改变它:

/device/000:004:00.7/vmkname = "vmnic0"

/device/000:005:00.0/vmkname = "vmnic1"

/device/000:005:00.1/vmkname = "vmnic2"

/device/000:005:00.2/vmkname = "vmnic3"

...直到

/device/000:005:00.3/vmkname = "vmnic15"

4 个答案:

答案 0 :(得分:3)

perl -pe 's/\d+(")$/$i++.$1/e;' file

See it

答案 1 :(得分:2)

Awk版本:

awk '{if ($1) {print $1, $2, "\"vmnic"c++"\""}else{print}}' FILE

您可以将输出重定向到新文件。

答案 2 :(得分:1)

这应该做:

count=0
for i in `cat input_file.txt | cut -d " " -f1`
do
    echo $i = \"vmnic$count\" >> output_file.txt
    count=`expr $count + 1`
done

请确保您的input_file.txt没有任何空行。

答案 3 :(得分:0)

见:

#!/bin/bash

c=0
while read a b; do
    [[ "$a" ]] || continue
    echo "$a = \"vmnic$((c++))\""
done < FILE > NEW_FILE