Replace Value of nth Column of Each Line Using Array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace Value of nth Column of Each Line Using Array
# 1  
Old 03-03-2018
Replace Value of nth Column of Each Line Using Array

Hello All,

I am writing a shell script with following requirement:

1. I have one input file as below

Code:
CHE01,A,MSC,INO
CHE02,B,NST,INC
CHE03,C,STM,INP

2. In shell script I have predefined array as below:

Code:
Array1={A, B, C}
Array2= {U09, C04, A054}

Now I wish to replace second column of input file by corresponding array value of Array2. If second column of input file matches with third element of Array1, it will be replaced by Array2(3) = A054 and so on. How do I achieve this using awk? I am avoiding to use while loop as it will consume lot of time when operated against large data set.

Your help is honestly appreciated

Thanks
Angsuman
# 2  
Old 03-03-2018
What operating system are you using?

What shell are you using?

What have you tried to solve this problem on your own?

Why use two arrays? It would be MUCH simpler to do this with a single array using the old value as the subscript and the new value as the value for that element.
# 3  
Old 03-03-2018
How are those two arrays populated? From those "large data sets", perhaps? It would be much easier for awk to operate on file(s) should these be used to define the arrays.
# 4  
Old 03-04-2018
Code:
awk  'NR==FNR { a[$1]=$4; next } {if ($2 ~ a["Array1"]) $2=a["Array2"]; print}' FS="= *{|, *|}" arrayfile FS=, inputfile


Last edited by abdulbadii; 03-04-2018 at 11:06 PM..
# 5  
Old 03-04-2018
Quote:
Originally Posted by abdulbadii
Code:
awk  'NR==FNR { a[$1]=$4; next } {if ($2 == a[array1]) $2=a[array2] }' FS=,={} arrayfile FS=, inputfile

Hi abdulbadii,
Would you please explain how the above script is supposed to do something related to the topic of this thread?

If I slightly modify your script to add a debugging statement showing how the array a is set:
Code:
awk  '
NR==FNR {
	a[$1]=$4
	printf("a[%s] has been set to \"%s\"\n", $1, $4)
	next
}
{	if ($2 == a[array1])
		$2=a[array2]
}
' FS=,={} arrayfile FS=, inputfile

it produces the output:
Code:
a[Array1={A, B, C}] has been set to ""
a[Array2= {U09, C04, A054}] has been set to ""

when the arrayfile and inputfile files contain the data shown in post #1 in this thread.

Note that since neither of the awk variables array1 and array2 have been set, the if statement will never find a match. And, even if a match were found and the 2nd field was changed to an empty string by the assignment, what difference would it make? There is nothing in this awk script that produces any output.

Note also that using the fixed string =,={} as a field separator for the 1st input file seems strange since that string is never found in either of your input files. That means that each line will be field #1 for that line and field $4 in each line will be an empty string (which is exactly what we see from the added debugging statement).
# 6  
Old 03-19-2018
Thank you all for your reply. As suggested by RudiC, I have changed my script to use another file instead of pre-defined array. Here is the code used:

Code:
awk -F',' 'NR==FNR { a[$1]=$2; next } $2 in a {print $1 "," a[$1] "," a[$2] "," $3 "," $4 }' $CONFIGFILE $INPUTFILE

Now CONFIGFILE contains data as below:

Code:
A,U09
B,C04
C,A054

Above code is working fine in this case but it does not work when config file has data as below:

Code:
a,U09
b,C04
c,A054

How do I tell awk to ignore the case

Thanks
Angsuman
# 7  
Old 03-19-2018
How about using awk's toupper() function?

BTW, your code snippet won't deliver what was requested in post#1 as a[$1] will always be undefined and thus deliver an empty field (= ,,) in the output.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search term in nth field and replace kth column

Hi, I have a text file which looks like this a.txt A,12,Apple,Red B,33,Banana,Yellow C,66,Sky,Blue I need to search for a particular field(s) in particular column(s) and for that matching line need to replace the nth column. Sample scenario 1: Search for 66 in second field and Sky in... (5 Replies)
Discussion started by: wahi80
5 Replies

2. Shell Programming and Scripting

awk search and replace nth column by using a variable.

I am passing a variable and replace nth value with the variable. I tried using many options in awk command but unable to ignore the special characters in the output and also unable to pass the actual value. Input : "1","2","3" Output : "1","1000","3" TempVal=`echo 1000` Cat... (2 Replies)
Discussion started by: onesuri
2 Replies

3. Shell Programming and Scripting

How to search and replace string from nth column from a file?

I wanted to search for a string and replace it with other string from nth column of a file which is comma seperated which I am able to do with below # For Comma seperated file without quotes awk 'BEGIN{OFS=FS=","}$"'"$ColumnNo"'"=="'"$PPK"'"{$"'"$ColumnNo"'"="'"$NPK"'"}{print}' ${FileName} ... (5 Replies)
Discussion started by: Amit Joshi
5 Replies

4. Shell Programming and Scripting

Replace a value of Nth field of nth row

Using Awk, how can I achieve the following? I have set of record numbers, for which, I have to replace the nth field with some values, say spaces. Eg: Set of Records : 4,9,10,55,89,etc I have to change the 8th field of all the above set of records to spaces (10 spaces). Its a delimited... (1 Reply)
Discussion started by: deepakwins
1 Replies

5. Shell Programming and Scripting

awk to search for specific line and replace nth column

I need to be able to search for a string in the first column and if that string exists than replace the nth column with "-9.99". AW12000012012 2.38 1.51 3.01 1.66 0.90 0.91 1.22 0.82 0.57 1.67 2.31 3.63 0.00 AW12000012013 1.52 0.90 1.20 1.34 1.21 0.67 ... (14 Replies)
Discussion started by: ncwxpanther
14 Replies

6. Shell Programming and Scripting

Replace the nth column date as MM/DD/YYYY

Hi, I need some unix command to replace the following thing. cat test.dat 1234|test|8/19/2009|8/20/2009|test 1234|test|8/9/2009|8/21/2009|test 1234|test|8/1/2009|8/2/2009|test after processing 1234|test|08/19/2009|08/20/2009|test 1234|test|08/09/2009|08/21/2009|test... (6 Replies)
Discussion started by: anshaa
6 Replies

7. Shell Programming and Scripting

Calculating average for every Nth line in the Nth column

Is there an awk script that can easily perform the following operation? I have a data file that is in the format of 1944-12,5.6 1945-01,9.8 1945-02,6.7 1945-03,9.3 1945-04,5.9 1945-05,0.7 1945-06,0.0 1945-07,0.0 1945-08,0.0 1945-09,0.0 1945-10,0.2 1945-11,10.5 1945-12,22.3... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

8. Shell Programming and Scripting

Using AWK to find top Nth values in Nth column

I have an awk script to find the maximum value of the 2nd column of a 2 column datafile, but I need to find the top 5 maximum values of the 2nd column. Here is the script that works for the maximum value. awk 'BEGIN { subjectmax=$1 ; max=0} $2 >= max {subjectmax=$1 ; max=$2} END {print... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

9. Shell Programming and Scripting

search pattern and replace x-y characters in nth line after every match

Hi, I am looking for any script which can do the following. have to read a pattern from fileA and copy it to fileB. fileA: ... ... Header ... ... ..p1 ... ... fileB: .... .... Header (3 Replies)
Discussion started by: anilvk
3 Replies

10. Shell Programming and Scripting

get 3rd column of nth line

hi; i have a file.txt and its 9th, 10th and 11th line lines are: RbsLocalCell=S2C1 maxPortIP 4 (this is 9th line) RbsLocalCell=S3C1 maxPortIP 4 (this is 10th line) RbsLocalCell=S1C1 ... (11 Replies)
Discussion started by: gc_sw
11 Replies
Login or Register to Ask a Question