Search term in nth field and replace kth column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search term in nth field and replace kth column
# 1  
Old 03-01-2017
Search term in nth field and replace kth column

Hi,

I have a text file which looks like this
a.txt
Code:
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 third field, then replace 4th column with Pink.
So now a.txt should look like
Code:
A,12,Apple,Red
B,33,Banana,Yellow
C,66,Sky,Pink

Sample scenario 2:
Search for B in first column and replace 3rd column with Tennis
So now a.txt should look like
Code:
A,12,Apple,Red
B,33,Tennis,Yellow
C,66,Sky,Pink

I'm using grep and sed,writing to tmp files and then overwriting a.txt.
Looking for something simpler, if possible
# 2  
Old 03-01-2017
You can create another file as a reference for search and replace and use it:-
Code:
$ cat ref.txt
2,66,4,Pink
1,B,3,Tennis

Code:
awk -F, '
        NR == FNR {
                S[$1] = $2 FS $3 FS $4
                next
        }
        {
                n = split ( $0, U )

                for ( k in S )
                {
                        m = split ( S[k], T )

                        if ( T[1] == U[k] )
                        {
                                for ( i = 1; i <= NF; i++ )
                                {
                                        if ( i == T[2] )
                                                $i = T[3]
                                }
                        }
                }

        }
        1
' OFS=, ref.txt a.txt

This User Gave Thanks to Yoda For This Post:
# 3  
Old 03-01-2017
something along these lines:
wahi.awk:
Code:
BEGIN {
  FS=OFS=","
  if (!repL) replL="2,66,3,Sky,4,Pink"
  replN=split(replL, replA, FS)
}
{
   found=1
   for(i=1;i<replN-1; i=i+2)
     if ($replA[i]!=replA[i+1] )
       found=0
   if (found)
     $(replN-1)=replA[replN]
}
1

for scenario1:
Code:
awk -v replL="2,66,3,Sky,4,Pink" -f  wahi.awk myFile

for scenario 2:
Code:
awk -v replL="1,3,3,Tennis" -f  wahi.awk myFile

This User Gave Thanks to vgersh99 For This Post:
# 4  
Old 03-01-2017
Either of the two above proposals almost fulfill the needs of the OP. Stealing from both, a - hopefully - complete solution was composed:
Code:
awk -F, '
NR == FNR       {S[NR] = $0
                 next
                }
                {for ( k in S ) {F = 1
                                 m = split (S[k], T) - 1
                                 for (i=1; i<m; i+=2) if ($T[i] != T[i+1])    {F = 0; break}
                                 if (F) $T[m] = T[m+1]
                                }
                }
1
' OFS=,  ref.txt a.txt
A,12,Apple,Red
B,33,Tennis,Yellow
C,66,Sky,Pink

with ref.txt collecting ALL the requirements from post#1:

Code:
2,66,3,Sky,4,Pink
1,B,3,Tennis

This User Gave Thanks to RudiC For This Post:
# 5  
Old 03-02-2017
Hi RudiC,
If you get some time could you explain the code a bit. I'm not that well versed in awk Smilie

Thanks
# 6  
Old 03-03-2017
Code:
awk -F, '                                       # set field separator to ","
NR == FNR       {S[NR] = $0                     # collect all rules from ref.txt lines into array S index by running line number
                                                # ref.txt structure: ref field,ref value,[ref field,ref value,...]target field,target value
                                                # like: 2,66,3,Sky,4,Pink: check F2 for "66", F3 for "Sky", if both met, set F4 to "Pink"

                 next                           # if in first file (NR == FNR) don`t continue processing this line but jump to next one
                }
                {for ( k in S ) {F = 1          # for all modifying rules in array S (= contents of ref.txt); do set FOUND variable to TRUE
                                 m = split (S[k], T) - 1        
                                                # split the field No.s and values into T array; keep (field count - 1) in m
                                 for (i=1; i<m; i+=2) if ($T[i] != T[i+1])    {F = 0; break}
                                                # if the mod. rules are violated, set FOUND var to FALSE, break this loop, goto next rule
                                 if (F) $T[m] = T[m+1]
                                                # modify target field if all rules are met
                                }
                }
1                                               # defaut action: print (modified?) line
' OFS=,  ref.txt a.txt                          # set output field separator to ",", specify rules file, data file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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 CHE01,A,MSC,INO CHE02,B,NST,INC CHE03,C,STM,INP 2. In shell script I have predefined array as below: Array1={A, B, C} Array2= {U09, C04, A054} (6 Replies)
Discussion started by: angshuman
6 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 pattern from nth field from a file

I have posted this again as old post is closed and I am not able to reopen. so please consider this new post Input File : 1,A,Completed,06.02_19.36,Jun 30 20:00 2,BBB,Failed,07.04_05.12,Jul 21 19:06 3,CCCCC,New,07.21_03.03,Jul 26 12:57 4,DDDDD,Pending,, I wast output file as: ... (7 Replies)
Discussion started by: Amit Joshi
7 Replies

5. Shell Programming and Scripting

Replace pattern from nth field from a file

$ cat /cygdrive/d/Final2.txt 1,A ,Completed, 07.03_23.01 ,Jun 30 20:00 2,BBB,Pending,, 3,CCCCC,Pending,, 4,DDDDD,Pending,, 5,E,Pending,, 6,FFFF,Pending,, 7,G,Pending,, In the above file 4th field is date which is in MM.DD_HH.MIN format and I need to convert it to as it is there in 5th... (1 Reply)
Discussion started by: Amit Joshi
1 Replies

6. 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

7. 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

8. 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

9. Shell Programming and Scripting

extracting a column using search term

I am trying to select a column using a search term. My input file looks like this (tab delimited): ABC BJS FDG GHH DGH DFG GHF 95 456 5 266 87 4567 67 3 54 678 4567 45 6 36 232 55 3 5 6 8 34 cat filename | awk '{print $2}'above code will give me the second column. However, what I want... (2 Replies)
Discussion started by: SangLad
2 Replies

10. Shell Programming and Scripting

Search term and output term in desired field

Hi All, I have an input_file below and i would like to use Perl to search for the term "aaa" and output the 3rd term in the same row as "aaa".For Example, i want to search for the term "ddd" and would want the code to ouput the 3rd term in the same row which is "fff". Can somebody help ? ... (28 Replies)
Discussion started by: Raynon
28 Replies
Login or Register to Ask a Question