Simple question

Sorry for stupid question, but why this script gives that output?

$ awk 'BEGIN { well=56789; print 1234$well }'
1234

I expected 123456789

Hello silyin,

Welcome to the forum. Kindly use the code tags please while posting your code/commands. The code shown by you is NOT showing expected output because any variable in awk need not to be put with $ symbol. So you can use following codes.

i- Code with BEGIN and without variable:

awk 'BEGIN { well=56789; print 1234 well }'

Output will be as follows.

123456789

ii- Code with use of variable.

awk -vwell="56789" 'BEGIN {print 1234 well}'

Output will be as follows.

123456789

Thanks,
R. Singh

1 Like

So basically just leave out the dollar sign. In awk variable references do not have a $-sign:

1 Like

Next one.
Why the value of variable 'well' defined for record 1 is empty for another records?

#! /bin/sh
for names
do
sed -e s/:// $names |
gawk ' 
NR==1 { if($1=="UWI") well=$2 }
NR>26 { printf "%s %s\r\n", well, $0 } ' >> tmp 
done

Please specify samples of your input files and desired output

OK.
The problem is I have about 2000 files (too big files) where is some ID in first raw which I want to transform to first field in all followed raws and to concatenate all files in single. I can attach about 30 raws of input file

---------- Post updated at 07:54 AM ---------- Previous update was at 07:48 AM ----------

So, this is input data (1 file)

UWI :060317000_00021R
  :00021rir.zak
  :060
  :317
  :000_
  :00021
   :
   :
- :
- :
  () :17.60
   () :66.00
   () :
  () :
  () :
  () :
  () :
   () :
   () :
   () :
    () :
    () :
   :
  :31.10.1980
DEPT UGOL AI AM DU dX dY dZ ABS UDL 
xxxx.xx xx.xx xxxx.xx xxxx.xx xxxx.xx xxxxx.xx xxxxx.xx xxxxx.xx xxxxx.xx xxxx.xx
0.00 0.00 -999.25 20.00 34.84 0.00 0.00 0.00 66.00 0.00
25.00 0.00 -999.25 20.00 34.84 0.00 0.00 25.00 41.00 0.00
50.00 0.00 -999.25 20.00 34.84 0.00 0.00 50.00 16.00 0.00
75.00 0.00 -999.25 20.00 34.84 0.00 0.00 75.00 -9.00 0.00
100.00 0.00 -999.25 20.00 34.84 0.00 0.00 100.00 -34.00 0.00
125.00 0.83 -999.25 20.00 34.84 0.15 0.10 125.00 -59.00 0.00
150.00 0.00 -999.25 20.00 34.84 0.30 0.21 150.00 -84.00 0.00

This is desired output:

060317000_00021R 0.00 0.00 -999.25 20.00 34.84 0.00 0.00 0.00 66.00 0.00
060317000_00021R 25.00 0.00 -999.25 20.00 34.84 0.00 0.00 25.00 41.00 0.00
060317000_00021R 50.00 0.00 -999.25 20.00 34.84 0.00 0.00 50.00 16.00 0.00
060317000_00021R 75.00 0.00 -999.25 20.00 34.84 0.00 0.00 75.00 -9.00 0.00
060317000_00021R 100.00 0.00 -999.25 20.00 34.84 0.00 0.00 100.00 -34.00 0.00
060317000_00021R 125.00 0.83 -999.25 20.00 34.84 0.15 0.10 125.00 -59.00 0.00
060317000_00021R 150.00 0.00 -999.25 20.00 34.84 0.30 0.21 150.00 -84.00 0.00

you're passing a list of files to this script?
you really don't need the shell loop, or sed.

awk 'FNR==1&&($1=="UWI"){sub(/:/,"",$2);well=$2}FNR>26{print well, $0}' files* >output

as awk script

#!/usr/bin/gawk
FNR==1 && ($1=="UWI") {
  sub(/:/,"",$2)
  well=$2
}
FNR>26 {
  print well, $0 >> "tmp"
}
1 Like

Hi neutroscott,
Excellent code, but 'well' at first column of output is empty...

Posted by neutronscott:

Hello neutronscott,

I think you missed >> concatination sign in 1st solution.

Hello silyin,

Following may also help.

awk -F" |:" 'FNR==1 && ($1=="UWI"){well=$3} NR>26{print well OFS $0}' files* >> Output_File

Thanks,
R. Singh

I have understood! After first field is \r which erase first field when I look output file by 'more' or 'head'

Thanks a lot for all who has helped me!