English 中文(简体)
只处理一栏档案
原标题:Sorting only one column of a file
  • 时间: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"
问题回答
perl -pe  s/d+(")$/$i++.$1/e;  file

Awk edition:

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

您可将产出转用于新的档案。

This should do:

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

Just make sure your input_file.txt doesn t have any empty lines.

见:

#!/bin/bash

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




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...