Sponsored Content
Top Forums Shell Programming and Scripting Find specific pattern and change some of block values using awk Post 303025094 by alex2005 on Wednesday 24th of October 2018 08:58:03 AM
Old 10-24-2018
Find specific pattern and change some of block values using awk

Hi,

Could you please help me finding a way to replace a specific value in a text block when matching a key pattern ?

I got the keys and the values from a command similar to:

Code:
echo -e "key01 Nvalue01-1 Nvalue01-2 Nvalue01-3\nkey02 Nvalue02-1 Nvalue02-2 Nvalue02-3 \nkey03 Nvalue03-1 Nvalue03-2 Nvalue03-3" 
key01 Nvalue01-1 Nvalue01-2 Nvalue01-3
key02 Nvalue02-1 Nvalue02-2 Nvalue02-3 
key03 Nvalue03-1 Nvalue03-2 Nvalue03-3

The file which contains the text blocks is similar to:

Code:
cat file1 
[key00]
fieldA = value00
fieldB = value00B
fieldC = value00C
[key01]
fieldA = value01 
fieldB = value01B
fieldC = value01C
[key01-new]
fieldA = value01
fieldB = value01B
fieldC = value01C
[key02]
fieldA = value02
fieldB = value02B
fieldC = value02C
filedX = textstring
[key02-new]
fieldA = value02
fieldB = value02B
fieldC = value02C
[key-03]
fieldA = value03
fieldB = value03B
fieldC = value03C
[key03-new]
fieldA = value03
fieldB = value03B
fieldC = value03C
filedH = somevalue
[key05]
fieldA = value04
fieldB = value05B
fieldC = value05C
filedD = othervalue  
[key06]
fieldA = value06
fieldB = value06B
fieldC = value06C

I want to replace fieldA fieldB and fieldC values for the keys:key01-new key02-new and key03-new .


Desired output should be similar to:

Code:
[key00]
fieldA = value00
fieldB = value00B
fieldC = value00C
[key01]
fieldA = value01 
fieldB = value01B
fieldC = value01C
[key01-new]
fieldA = Nvalue01-1
fieldB = Nvalue01-2
fieldC = Nvalue01-3
[key02]
fieldA = value02
fieldB = value02B
fieldC = value02C
filedX = textstring
[key02-new]
fieldA = Nvalue02-1
fieldB = Nvalue02-2
fieldC = Nvalue02-3
[key-03]
fieldA = value03
fieldB = value03B
fieldC = value03C
[key03-new]
fieldA = Nvalue03-1
fieldB = Nvalue03-2
fieldC = Nvalue03-3
filedH = somevalue
[key05]
fieldA = value04
fieldB = value05B
fieldC = value05C
filedD = othervalue  
[key06]
fieldA = value06
fieldB = value06B
fieldC = value06C

Below is what I have tried so far, but as you can see is not working very well.
Key matching works, and presume in order to have a single iteration, I need to read keys and values into an array, and I'm afraid this is beyond my capabilities.

Code:
echo -e "key01 Nvalue01-1 Nvalue01-2 Nvalue01-3\nkey02 Nvalue02-1 Nvalue02-2 Nvalue02-3 \nkey03 Nvalue03-1 Nvalue03-2 Nvalue03-3"  | while read k1 v1 v2 v3 ; do awk -v key=$k1 -v vl1=$v1 -v vl2=$v2 -v vl3=$v3  '/\[.*\]/ {if ($0 ~ key"-new") replace=1; else replace=0;} {if (replace && $1 == "fieldA" ) $3=vl1 ; else if (replace && $1 == "fieldB" ) $3=vl2 ; else if (replace && $1 == "fieldC" ) $3=vl3 ;}1' file1 ; done

Code:
[key00]
fieldA = value00
fieldB = value00B
fieldC = value00C
[key01]
fieldA = value01 
fieldB = value01B
fieldC = value01C
[key01-new]
fieldA = Nvalue01-1
fieldB = Nvalue01-2
fieldC = Nvalue01-3
[key02]
fieldA = value02
fieldB = value02B
fieldC = value02C
filedX = textstring
[key02-new]
fieldA = value02
fieldB = value02B
fieldC = value02C
[key-03]
fieldA = value03
fieldB = value03B
fieldC = value03C
[key03-new]
fieldA = value03
fieldB = value03B
fieldC = value03C
filedH = somevalue
[key05]
fieldA = value04
fieldB = value05B
fieldC = value05C
filedD = othervalue  
[key06]
fieldA = value06
fieldB = value06B
fieldC = value06C

[key00]
fieldA = value00
fieldB = value00B
fieldC = value00C
[key01]
fieldA = value01 
fieldB = value01B
fieldC = value01C
[key01-new]
fieldA = value01
fieldB = value01B
fieldC = value01C
[key02]
fieldA = value02
fieldB = value02B
fieldC = value02C
filedX = textstring
[key02-new]
fieldA = Nvalue02-1
fieldB = Nvalue02-2
fieldC = Nvalue02-3
[key-03]
fieldA = value03
fieldB = value03B
fieldC = value03C
[key03-new]
fieldA = value03
fieldB = value03B
fieldC = value03C
filedH = somevalue
[key05]
fieldA = value04
fieldB = value05B
fieldC = value05C
filedD = othervalue  
[key06]
fieldA = value06
fieldB = value06B
fieldC = value06C

[key00]
fieldA = value00
fieldB = value00B
fieldC = value00C
[key01]
fieldA = value01 
fieldB = value01B
fieldC = value01C
[key01-new]
fieldA = value01
fieldB = value01B
fieldC = value01C
[key02]
fieldA = value02
fieldB = value02B
fieldC = value02C
filedX = textstring
[key02-new]
fieldA = value02
fieldB = value02B
fieldC = value02C
[key-03]
fieldA = value03
fieldB = value03B
fieldC = value03C
[key03-new]
fieldA = Nvalue03-1
fieldB = Nvalue03-2
fieldC = Nvalue03-3
filedH = somevalue
[key05]
fieldA = value04
fieldB = value05B
fieldC = value05C
filedD = othervalue  
[key06]
fieldA = value06
fieldB = value06B
fieldC = value06C

Thanks is advance.
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find required files by pattern in xml files and the change the pattern on Linux

Hello, I need to find all *.xml files that matched by pattern on Linux. I need to have written the file name on the screen and then change the pattern in the file just was found. For instance. I can start the script with arguments for keyword and for value, i.e script.sh keyword... (1 Reply)
Discussion started by: yart
1 Replies

2. Shell Programming and Scripting

Find Where Values Change From Positive To Negative and viceversa

Hi all, I have a file that looks like shown below. I want to find places where the value in column 2 change from negative to positive and vice versa and return the value on column 1 at that point. I wonder if this is possible in shell script or awk .. please help! Here is the original data ... (6 Replies)
Discussion started by: malandisa
6 Replies

3. Shell Programming and Scripting

how to find entries, NOT starting with specific pattern

Hey,I have a file in following format >1 ABC........ >2 XYZ..... >3 ABC........ >4 MNO....... >5 ABC....... now I would like to find only those entries that doesn't start with ABC (specific pattern)e.g preferred output: >2 XYZ.... >4 MNO....... it will be nice if anybody how... (2 Replies)
Discussion started by: ankitachaurasia
2 Replies

4. Shell Programming and Scripting

How to find a file with a specific pattern for current sysdate & upon find email the details?

I need assistance with following requirement, I am new to Unix. I want to do the following task but stuck with file creation date(sysdate) Following is the requirement I need to create a script that will read the abc/xyz/klm folder and look for *.err files for that day’s date and then send an... (4 Replies)
Discussion started by: PreetArul
4 Replies

5. Shell Programming and Scripting

[awk] find pattern, change next two lines

Hi, hope you can help me... It seems like a straightforward problem, but I haven't had any success so far using my basic scripting and awk "skills": I need to find a pattern /VEL/ in an input file that looks like this: 1110SOL OW25489 1.907 7.816 26.338 -0.4365 0.4100 -0.0736 ... (3 Replies)
Discussion started by: origamisven
3 Replies

6. Shell Programming and Scripting

Using sed to change values after a specific string

Hello I have a script that searches a file for a specific string and then changes the nth column after that string. I have searched online for how to do this with sed but have not seemed to find a solution that works for me. I am using bash. Some background info: - Currently I am using awk to... (4 Replies)
Discussion started by: prodigious8
4 Replies

7. Shell Programming and Scripting

Find duplicate values in specific column and delete all the duplicate values

Dear folks I have a map file of around 54K lines and some of the values in the second column have the same value and I want to find them and delete all of the same values. I looked over duplicate commands but my case is not to keep one of the duplicate values. I want to remove all of the same... (4 Replies)
Discussion started by: sajmar
4 Replies

8. Shell Programming and Scripting

awk to remove mutiple values from specific pattern, leaving a single value

In the awk below I am trying to remove all instances after a ; (semi-colon) or , (comma) in the ANN= pattern. I am using gsub to substitute an empty string in these, so that ANN= is a single value (with only one value in it the one right after the ANN=). Thank you :). I have comented my awk and... (11 Replies)
Discussion started by: cmccabe
11 Replies

9. Shell Programming and Scripting

Using awk to change a specific column and in a specific row

I am trying to change the number in bold to 2400 01,000300032,193631306,190619,0640,1,80,,2/ 02,193631306,000300032,1,190618,0640,CAD,2/ I'm not sure if sed or awk is the answer. I was going to use sed and do a character count up to that point, but that column directly before 0640 might... (8 Replies)
Discussion started by: juggernautjoee
8 Replies
All times are GMT -4. The time now is 09:09 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy