Inserting a field without disturbing field separator on other fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inserting a field without disturbing field separator on other fields
# 8  
Old 05-13-2016
You can use getline in awk to get the desire output. i have modified your code as below.
Code:
nawk '{print " " $NF}' line2 > nawk6
      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)
              for (i=2; i<=CNF+2; i++){printf "%7s", T[i]; if( i == 18){ getline var < "/export/home/batham51/radio/nawk6"; printf "%7s", var }}
              printf RS
             }
      ' input > ruop

This User Gave Thanks to pravin27 For This Post:
# 9  
Old 05-13-2016
You can't import shell variables into awk the way you do in above post. Please read man awk and/or many threads in here about how to use the -v option to pass variables to awk.

In your case, with one single value in file nawk6, that could be done like

Try
Code:
awk 'NR == 1 {var = $0; next}
.
.
.
' nawk6 input

These 2 Users Gave Thanks to RudiC For This Post:
# 10  
Old 05-13-2016
Hi Rudi and pravin27,

Thanks for the reply.I have tried both of your codes and the both worked fine.

Now i am applying the same code on seven input files where as my nawk6 contains 7 lines in it. i can do that but i am failed to take the output to seben different output files.

my nawk6 looks like this:
Code:
cat nawk6
 2.979
 3.078
 2.980
 1.338
 3.272
 5.768
 2.827

i want to insert each value of nawk6 at the 19th field in each of my seven input(1,2,3,4,5,6,7) files and take the output to 7 different output files.

can you please help me with this?

Thanks in advance,
am24

---------- Post updated at 09:02 AM ---------- Previous update was at 08:34 AM ----------

Hi,

Since my input files are containing only 1 line. I have done it in the below way.

Code:
nawk '{print " " $NF}' 7lines > nawk7
      2       nawk '
      3               {CNF = (length()-10)/7
      4               printf "%9s", substr ($0,1,9)
      5               for (i=0;i<=CNF;i++) T[i+2] = substr ($0, 10+i*7, 7)
      6               for (i=2; i<=CNF+2; i++){printf "%7s", T[i]; if( i == 18){ getline var < "/export/home/batham51/radio/nawk7"; printf "%7s", var }}
      7               printf RS
      8              }
      9       ' input1 input2 input3 input4 input5 input6 input7  > final
     10
     11 sed -n "1p" final > output1;sed -n "2p" final > output2;sed -n "3p" final > output3;sed -n "4p" final > output4;sed -n "5p" final > output5;sed -n "6p" final > output6;sed -n "7p" final > output7

So i got the desired results. If you have any better idea than this please let me know.

Thanks in advance,
am24
# 11  
Old 05-13-2016
If you're very sure there's no empty fields in your file, try

Code:
awk '
FNR == NR       {P[NR] = $0
                 next
                }
FNR == 1        {FN++
                }
                {printf "%s", $1 > ("output" FN)
                 $19 = P[FN] FS $19
                 $0 = $0
                 for (i=2; i<=NF; i++) printf "%7s", $i > ("output" FN)
                 printf RS > ("output" FN)
                }

' nawk7 file1 file1 file1 file1 file1
cf out*
output1:
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 . . . 
output2:
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  3.078 76.265 23.735  . . . 
output3:
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.980 76.265 23.735  . . . 
output4:
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  1.338 76.265 23.735  . . . 
output5:
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  3.272 76.265 23.735  . . .

This User Gave Thanks to RudiC For This Post:
# 12  
Old 05-13-2016
As long as there is one line in 7lines for each input file, the values in the file 7lines fall in the range -99.999 through 999.999, the value on one line in 7lines is to added between the 18th and 19th field in each line in the corresponding input file (as long as there is one or more lines in each input file), the length of the 1st field on each line in an input file is a constant width within that input file, and the length of each field (other than the 1st field) in every input file is 7; the following awk script doesn't need you to run an awk script to create the awk6 file, nor run sed scripts.
Code:
nawk '
NR == FNR {
	f19[NR] = $1
	next
}
FNR == 1 {
	if(fc++)
		close(ofn)
	len = length($1) + 17 * 7
	ofn = "output" fc
}
{	printf("%s%7.3f%s\n", substr($0, 1, len), f19[fc],
		substr($0, len + 1)) > ofn
}' 7lines input[1-7]

This User Gave Thanks to Don Cragun For This Post:
# 13  
Old 05-14-2016
Hi Rudi and Don,

Thanks for your time on this.Both of your codes worked perfectly fine.

Can you both please explain me the code ?

Regards,
am24
# 14  
Old 05-14-2016
Code:
awk '
FNR == NR       {P[NR] = $0                                     # collect to be inserted values from first file into P array  
                 next
                }
FNR == 1        {FN++                                           # increment file number with every new file
                }
                {printf "%s", $1 > ("output" FN)                # print first element with its own format to respective output file
                 $19 = P[FN] FS $19                             # insert respective value from P array in front of field 19
                 $0 = $0                                        # recompute NF
                 for (i=2; i<=NF; i++) printf "%7s", $i > ("output" FN)
                                                                # print all fields with constant format to output file
                 printf RS > ("output" FN)                      # print line terminator
                }
' nawk7 file1 file1 file1 file1 file1

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Field separator

Hello All, I have a file, but I want to separate the file at a particular record with comma"," in the line Input file APPLE6SSAMSUNGS5PRICEPERPIECEDOLLAR600EACH010020340URX581949695US to Output file APPLE6S,SAMSUNGS5,PRICEPERPIECE,DOLLAR600EACH,010020340URX581949695,US This is for... (11 Replies)
Discussion started by: m6248m
11 Replies

2. Shell Programming and Scripting

awk field separator help -

Hi Experts , file : - How to construct the awk filed separator so that $1, $2 $3 , can be assigned to the each "" range. I am trying : awk -F"]" '{print $1}' but it is printing the entire file. Not first field. The desired output needed for first field... (9 Replies)
Discussion started by: rveri
9 Replies

3. UNIX for Dummies Questions & Answers

change field separator only from nth field until NF

Hi ! input: 111|222|333|aaa|bbb|ccc 999|888|777|nnn|kkk 444|666|555|eee|ttt|ooo|ppp With awk, I am trying to change the FS "|" to "; " only from the 4th field until the end (the number of fields vary between records). In order to get: 111|222|333|aaa; bbb; ccc 999|888|777|nnn; kkk... (1 Reply)
Discussion started by: beca123456
1 Replies

4. Shell Programming and Scripting

Inserting a new field inbetween two exisitng field

I have a '|' delimited file. My file looks like below 23|nationalhoilday|feb12||||||||||||||california|northdistrict|| In the same way, each record has 164 fields. I have to insert one more field after the 85th field. Expected output... (3 Replies)
Discussion started by: machomaddy
3 Replies

5. Shell Programming and Scripting

echo field separator

I am trying to echo all fields except for the last field. I want to include the field seperator, but it is removed. echo "a;s;v;g" | awk -F ";" '{$(NF--)=""; print}' a s v I want an output like this: a;s;v; (3 Replies)
Discussion started by: locoroco
3 Replies

6. Shell Programming and Scripting

Field separator X'1F'

Hi, I have a flat file with fields separated by a X'1F' i have to fetch 4th field from second line. please help me how to achieve it. I tried with below command and its not working. cut -f4 -d`echo -e '\x1f'` filename.txt I am using SunOS. Thanks in advance. (2 Replies)
Discussion started by: rohan10k
2 Replies

7. Shell Programming and Scripting

awk, comma as field separator and text inside double quotes as a field.

Hi, all I need to get fields in a line that are separated by commas, some of the fields are enclosed with double quotes, and they are supposed to be treated as a single field even if there are commas inside the quotes. sample input: for this line, 5 fields are supposed to be extracted, they... (8 Replies)
Discussion started by: kevintse
8 Replies

8. Shell Programming and Scripting

Field separator in awk

Hi I need to check if field separator I am using in awk statement is " : ", for example: TIME=12:59 HOUR=`echo "$TIME" | awk '{FS=":"; print $1}'` MINUTES=`echo "$TIME" | awk '{FS=":"; print $2}'` Is there a way to check within the above awk statement ? Thanks for help -A (2 Replies)
Discussion started by: aoussenko
2 Replies

9. Shell Programming and Scripting

Sorting on two fields time field and number field

Hi, I have a file that has data in it that says 00:01:48.233 1212 00:01:56.233 345 00:09:01.221 5678 00:12:23.321 93444 The file has more line than this but i just wanted to put in a snippet to ask how I would get the highest number with time stamp into another file. So from the above... (2 Replies)
Discussion started by: pat4519
2 Replies

10. Shell Programming and Scripting

how to include field separator if there are blank fields?

Hi, I have the following data in the format as shown (note: there are more than 1 blank spaces between each field and the spaces are not uniform, meaning there can be one blank space between field1 and field2 and 3 spaces between field3 and field4, in this example, # are the spaces in between... (19 Replies)
Discussion started by: ReV
19 Replies
Login or Register to Ask a Question