awk change one column in file, where column changes position in different files

Viewed 128

I have text files that result from various processing steps, so depending on the order of the steps the order columns and length each line changes from one file to the next.

so file1 would be:

moo 100.35  blah  9  85 0.0038
moo 93.8    bluu 10  85 0.0042

and file2 would be:

125.2  129.3 moo 0.23
123.5  125.3 moo 0.23

and I would like to change it to:

1_horatio 100.35  blah  9  85 0.0038
2_horatio 93.8    bluu 10  85 0.0042

and

125.2  129.3 1_clarence 0.23
123.5  125.3 2_clarence 0.23

where the the number on the new name for moo is incremented for each row. The name is an input variable.

here's what I've been trying so far:

newnam=$1
awk -v nnam=$newnam 'BEGIN{ count=1 }  {imgn=count"_"nam; print imgn,$2,$3,$4 count++  }' $2 > $3

which I then need to change to:

newnam=$1
awk -v nam=$newnam 'BEGIN{ count=1 }  {imgn=count"_"nam; print $1,$2,imgn,$4 count++  }' $2 > $3

I'd like to be able to put the column number as a variable, and not have to worry about how many columns there are. There can be up to 50 column, with up to a million rows.

Is there a way to do this in awk? Or bash with awk?

1 Answers

I believe what you can do is something like this,

awk '{$col=count"_"name; count++}1' name="clarence" col=3 <file>

Here we make use of the following awk features:

  • redefining a field $n will redefine $0
  • The command 1 means {print $0}
  • The operator $expr returns the field number given by expr

Update: to have the counter start at 1, one can rewrite this as:

awk '{count++; $col=count"_"name}1' name="clarence" col=3 <file>

which can be shortened as:

awk '{$col=++count"_"name}1' name="clarence" col=3 <file>

due to the usage of the pre-increment operator ++var. But again, now count resembles nothing but the number of records, thus

awk '{$col=NR"_"name}1' name="clarence" col=3 <file>
Related