Fill fields with awk from an array?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Fill fields with awk from an array?
# 1  
Old 09-29-2011
Fill fields with awk from an array?

Hi experts,

I have been trying for a while to accomplish the following task using awk, and I just don't seem find find a way. I am not particular about using awk, it just seemed like the logical choice at first.

I have a file that contains 5 fields that are delimited by a space character. These fields are options and I need to change either one or more of them.

The new options are read in from standard input, i.e. the user provides a new set of options.

so, I read in the options and then I have a file and for each line in the file I aks the user if he wants to change particular options (they refer to locations).

But I don't seem to manage to read from the array. Smilie

as an example:

text file:
one two three four five
here there somewhere anywhere nowhere

sometimes options may contain only one field, sometimes they contain 2 or more and I want to replace items starting at the 3rd field.

so that afterwards, if options are
options=(this that)

the text file would look like

one two this that five
here there this that nowhere

how can this be achieved???

or if options=(this)

the text file would look like:
one two this four five
here there this anywhere nowhere

This decision is made dynamically, depending on how many options the user provides, but always starts at $3.

Anyone any ideas??
# 2  
Old 09-29-2011
Code:
$ awk -v OPTIONS="this that" 'BEGIN { split("X X " OPTIONS, A, " "); }
{ for(N=3; N<=NF; N++) if(A[N]) $N=A[N] } 1' < data
one two this that five
here there this that nowhere
$

If you need it to be interactive:

Code:
printf "Options: "
read OPTIONS
awk -v OPTIONS="$OPTIONS" ...

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Fill in missing hours and interpolate values using awk.

I have a time series data like this 40754,35.6931,51.3092,201610160700,21.0 40754,35.6931,51.3092,201610160800,23.0 40754,35.6931,51.3092,201610160900,24.0 40754,35.6931,51.3092,201610161000,24.0 40754,35.6931,51.3092,201610161300,25.0 40754,35.6931,51.3092,201610161400,23.0... (6 Replies)
Discussion started by: emirzaei
6 Replies

2. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

3. Shell Programming and Scripting

awk - compare 1st 15 fields of record with 20 fields

I'm trying to compare 2 files for differences in a selct number of fields. When differnces are found it will write the whole record of the second file including appending '|C' out to a delta file. Each record will have 20 fields, but only want to do comparison of 1st 15 fields. The 1st field of... (7 Replies)
Discussion started by: sljnk
7 Replies

4. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

5. Shell Programming and Scripting

awk two fields in one array

I want to compare these files while putting $1 and $2 into an array and getting rid of the punctuation. What am i doing wrong? File1.txt Apple # 223 Peach # 84; Banana # 1605. Banana # 1605; Orange # 6; Peach # 84 Peach # 84; Apple # 229; Banana # 1605. Peach # 84 Apple, # 229;... (3 Replies)
Discussion started by: sdf
3 Replies

6. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 Replies

7. Shell Programming and Scripting

awk reading many fields to array

I want to read $3,$4,$5,$6,$7 of fileA in array and when fileb $1 = fileA $4 the i want to print array and few fields from fileB. This should work but has some syntax error. nawk -F, 'FNR==NR{a=;next} a{print a}' fileB fileA Appreciate if someone can correct this. (2 Replies)
Discussion started by: pinnacle
2 Replies

8. Shell Programming and Scripting

parse a file and fill an array with it

Hi Guys, I am trying to do a file parse which is something like config file: machines= sha1 sha2 sha3 sha4 The bash script should be supporting upto 64 such machines what I want is to place the machines in an array and then use the array to ssh to each machine. The script I worte ... (11 Replies)
Discussion started by: jbmukund
11 Replies

9. Shell Programming and Scripting

Fill the Key fields : Please help us

Hi .... we are having the below file .Column 1, Column 2 ,column 3 are key fields... In the below ...for 2 nd , 3 rd row the repeated key column is missing .... i want the new file to be populated with all missing key columns. ... (11 Replies)
Discussion started by: charandevu
11 Replies

10. Shell Programming and Scripting

align several fields and fill spaces with zero

hi all, i have a big problem, and i donīt know what to do. i have a flat file with several fields, which are separated by ";" like this: 5656838-7B;97030000-7;*;V16106133 ;1;1; 4612062-0B;97030000-7;*;C14038149 ;1;2; 8044938-0B;97030000-7;*;V16034219 ;1;2; where B is a blank space. ... (2 Replies)
Discussion started by: DebianJ
2 Replies
Login or Register to Ask a Question