Inserting a field without disturbing field separator on other fields


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 05-11-2016
Inserting a field without disturbing field separator on other fields

Hi All,

I have the input as below:

Code:
cat input
032016002  2.891 97.109 16.605 27.172 24.017 32.207  0.233  0.021 39.810  0.077  0.026 19.644 13.882  0.131 11.646  0.102 11.449 76.265 23.735 16.991 83.009  8.840 91.160  0.020 99.980 52.102 47.898 44.004 55.996 39.963 18.625  0.121  1.126 40.189 91.289  2.822  0.317  5.571

I need to add a value at 19th field without disturbing the field separator between the fields.

The field separator in my input is space. and each field contains 6 positions in it except the first field.

Now the value i need to add at 19th field is below:

Code:
cat bb
 2.979

My output should look like below:

Code:
cat output
032016002  2.891 97.109 16.605 27.172 24.017 32.207  0.233  0.021 39.810  0.077  0.026 19.644 13.882  0.131 11.646  0.102 11.449  2.979 76.265 23.735 16.991 83.009  8.840 91.160  0.020 99.980 52.102 47.898 44.004 55.996 39.963 18.625  0.121  1.126 40.189 91.289  2.822  0.317  5.571

I have tried the below codes:

Code:
nawk 'NR==FNR{A[FNR]=$0; next} {$19=A[FNR] OFS $19}1' bb input > op

nawk '(getline p<f)>0{$19=p OFS $19}1' f=bb input > op2

I have got the same output from the above two codes as below:

Code:
cat op
032016002 2.891 97.109 16.605 27.172 24.017 32.207 0.233 0.021 39.810 0.077 0.026 19.644 13.882 0.131 11.646 0.102 11.449  2.979 76.265 23.735 16.991 83.009 8.840 91.160 0.020 99.980 52.102 47.898 44.004 55.996 39.963 18.625 0.121 1.126 40.189 91.289 2.822 0.317 5.571

Here in the above output, if we observe,the bb value got inserted at 19th field in the input file. But the spaces got removed from the fields which contains a combination of blank space and values.
For ex:- see field2. field2 = 2.891 but after inserting bb value the leading blank space at field2 got removed. Likewise blankspaces got removed in other fields as well.

Any idea how to overcome this ?

Thanks in advance,
am24
# 2  
Old 05-11-2016
Is that one of your fixed field width files again? Then you shouldn't say the field separator is space...
Your problem is (man awk: )
Quote:
Assignment to NF or to a field causes $0 to be reconstructed by concatenating the $i's separated by OFS
Try adapting the method to copy the line into an array, then add the array element to be inserted, and print the array using a fixed width format.
This User Gave Thanks to RudiC For This Post:
am24 (05-11-2016)
# 3  
Old 05-12-2016
Hi Rudi,

Thanks for the reply. I will try on this and will let you know.

Regards,
am24

---------- Post updated 05-12-16 at 02:52 AM ---------- Previous update was 05-11-16 at 03:52 AM ----------

Hi Rudi,

I have tried the below code.

Code:
nawk '
              {CNF = (length()-10)/7
               printf "%9s", substr ($0,1,9)
               for (i=0;i<=CNF;i++) T[i+2] = substr ($0, 10+i*7, 7)
               T[19] = 2.979
               for (i=2; i<=CNF+2; i++) printf "%7s", T[i]
               printf RS
              }
       ' input > ruop

with the above code, i can keep the 19th field value as 2.979 and the field width also adjusted properly. But the problem is the original value of 19th field in my input 76.265 is getting removed. Al other field values are coming correctly.

I just need to insert 2.979 in 19th filed but i should not remove any other field values. How can i overcome this ?

Could you please help me on this ?

Thanks in advance,
am24
# 4  
Old 05-12-2016
You can modify your for loop as below.
Code:
for (i=2; i<=CNF+2; i++) {printf "%7s", T[i]; if ( i == 19) {printf "%7s", "2.979"}}

This User Gave Thanks to pravin27 For This Post:
am24 (05-12-2016)
# 5  
Old 05-12-2016
Hi pravin27,

Thanks for the reply. I have modified the loop as you suggested. In the output , 2.979 placed in 20th field and 76.265 placed in 19th field.

So i just modified as below:

Code:
for (i=2; i<=CNF+2; i++) {printf "%7s", T[i]; if ( i == 18) {printf "%7s", " 2.979"}}

Now the value 2.979 is placed in 19th field and 76.265 in 20th field. and all other fields placed correctly.

Also i have question that , if 2.979 value is stored in one variable, then can i assign it to T[19] ?

Reason for this is, the 2.979 is not constant value, i am taking the value from some other file. So if i pull out the value from the file and store it in one variable then can i assign the variable value to array element ?

I have tried something on this but did not get the proper result.

Thanks in advance,
am24
# 6  
Old 05-12-2016
You want to insert that value, i.e. it will be $19, $19 will become $20, etc.? Then you need to shove every single value one index back. Like for (i=CNF; i>18; i--) $(i+1) = $i.

IF there's NO empty fields in your file, you could extend the 19th field like $19 = 2.979 " " $19, and then split $0 into T for formatted output.


EDIT: Of course you can use a variable for the value to be inserted: $19 = var " " $19! But - it needs to be an awk variable, NOT a shell variable.
This User Gave Thanks to RudiC For This Post:
am24 (05-12-2016)
# 7  
Old 05-13-2016
Thanks Rudi,

I will try on this and will let you know.

Regards,
am24

---------- Post updated 05-13-16 at 02:12 AM ---------- Previous update was 05-12-16 at 03:30 AM ----------

Hi Rudi,

I have tried different ways to assign variable value to array element but i did not succeed in this. for ex see the below:

Code:
nawk '{print " " $NF}' line2 > nawk6
a="/export/home/batham51/radio/nawk6"
     
     
      nawk -v '
              var="$a"
              {CNF = (length()-10)/7
              printf "%9s", substr ($0,1,9)
              for (i=0;i<=CNF;i++) T[i+2] = substr ($0, 10+i*7, 7)
              for (i=2; i<=CNF+2; i++){printf "%7s", T[i]; if( i == 18){printf "%7s", var }}
              printf RS
             }
      ' input > ruop

Can you please help me with this ?

Code:
cat nawk6
 2.979

Code:
cat input
032016002  2.891 97.109 16.605 27.172 24.017 32.207  0.233  0.021 39.810  0.077  0.026 19.644 13.882  0.131 11.646  0.102 11.449 76.265 23.735 16.991 83.009  8.840 91.160  0.020 99.980 52.102 47.898 44.004 55.996 39.963 18.625  0.121  1.126 40.189 91.289  2.822  0.317  5.571

I need to insert nawk6 value at 19th field in my input

Thanks in advance,
am24
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Field separator m6248m Shell Programming and Scripting 11 06-10-2015 02:33 PM
awk field separator help - rveri Shell Programming and Scripting 9 08-29-2013 12:39 AM
awk field separator locoroco Shell Programming and Scripting 2 01-28-2013 01:11 AM
change field separator only from nth field until NF beca123456 UNIX for Dummies Questions & Answers 1 08-17-2012 10:28 PM
Strings as Field separator pamu Shell Programming and Scripting 3 06-21-2012 03:22 AM
Inserting a new field inbetween two exisitng field machomaddy Shell Programming and Scripting 3 01-03-2012 04:44 AM
Array and field separator Dedalus Shell Programming and Scripting 3 08-31-2011 09:01 AM
Field separator X'1F' rohan10k Shell Programming and Scripting 2 05-24-2011 09:17 AM
awk, comma as field separator and text inside double quotes as a field. kevintse Shell Programming and Scripting 8 11-15-2010 05:31 PM
Field separator in awk aoussenko Shell Programming and Scripting 2 03-29-2010 12:59 PM
Sorting on two fields time field and number field pat4519 Shell Programming and Scripting 2 01-08-2010 02:45 PM
field separator in Perl ahsog Shell Programming and Scripting 38 04-17-2009 08:30 PM
Field separator Ques. yahyaaa Shell Programming and Scripting 6 09-16-2008 01:54 PM
field separator as regexp Abhishek Ghose Shell Programming and Scripting 2 02-15-2006 07:49 PM
how to include field separator if there are blank fields? ReV Shell Programming and Scripting 19 07-13-2005 04:50 AM