Search for a value and replace other field in the same set


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for a value and replace other field in the same set
# 1  
Old 12-16-2013
Search for a value and replace other field in the same set

Hello friends,

I have huge file with many sets where each "set" has few lines and each set always begins with "Set" in Sq brackets as shown above.
Code:
# cat file1

[Set 1]
[Name "rnd"]
[Date "2013.12.03"]
[Result "WIN"]
[VALUE "A1B2C3"]

[Set 2]
[Name "rnd"]
[Date "2013.12.03"]
[Result "WIN"]
[VALUE "X9Y8Z7"]

[Set 3]
[Name "rnd"]
[Date "2013.12.03"]
[Result "WIN"]
[VALUE "P4Q5R6"]

Now I need to grep for a value and modify its result field. ex.

Code:
./script.sh  file1   X9Y8Z7   LOSS  >file2

first parameter is input file 2nd parameter is value to be searched and 3rd parameter is new value.
then file2 should have the new values like below:

Code:
# cat file2

[Set 1]
[Name "rnd"]
[Date "2013.12.03"]
[Result "WIN"]
[VALUE "A1B2C3"]

[Set 2]
[Name "rnd"]
[Date "2013.12.03"]
[Result "LOSS"]
[VALUE "X9Y8Z7"]

[Set 3]
[Name "rnd"]
[Date "2013.12.03"]
[Result "WIN"]
[VALUE "P4Q5R6"]

I think we can do this using awk but i have no idea. Pls help me!
Thanks!!
# 2  
Old 12-16-2013
Code:
awk -v S="X9Y8Z7" -v  R="LOSS" '
        /Result/ {
                v = $0
                getline
                if ( $2 ~ S )
                sub ( /\"[A-Z]*\"/, "\"" R "\"", v )
                $0 = v RS $0
        }
        1
' file1

These 2 Users Gave Thanks to Yoda For This Post:
# 3  
Old 12-16-2013
try this script:

Code:
infile=$1
val="$2"
new="$3"
awk -vv="$val" -vn="$new" '
$0 ~ "VALUE \"" v "\"" {
    gsub("[[]Result[^\n]*\n", "[Result \"" n "\"]\n")
}
1' RS='' ORS='\n\n' OFS='\n' $infile

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

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help needed with shell script to search and replace a set of strings among the set of files

Hi, I am looking for a shell script which serves the below purpose. Please find below the algorithm for the same and any help on this would be highly appreciated. 1)set of strings need to be replaced among set of files(directory may contain different types of files) 2)It should search for... (10 Replies)
Discussion started by: Amulya
10 Replies

2. UNIX for Dummies Questions & Answers

Search and replace the last field

Hi All, Seeking for your assistance on how to search and replace the last field/column. please see sample below: inputfile1.csv ="8923523434",="543623534"="afd23535623",="100"="200" ="8923523431",="543623536"="afd23535626",="101"="201"... (3 Replies)
Discussion started by: poginiks
3 Replies

3. Shell Programming and Scripting

Search field in text file and replace value

Hi there, First of all this is my first post here. Thank you in advance for your help. What I am trying to do is the following. I have a text file where each field of each row is separated by a tabulator. Looks like this: ATOM 1 N HSE A 26 3.033 -10.429 -2.262 1.00 17.07 ... (8 Replies)
Discussion started by: doom4
8 Replies

4. Shell Programming and Scripting

awk search and replace in a targeted field instead of $0

Hi I would like to apply this gawk command: gawk '{$0=gensub(/\y+\y/,"","g"); print}' file not to the whole $0 but just to the part of $0 that is between: (a number)"> and </mrk> Is it possible? thanks for your help. (4 Replies)
Discussion started by: louisJ
4 Replies

5. Shell Programming and Scripting

Search and replace field?

I have 2 files A.txt and B.txt A.txt 3 fields and separate by a comma some,thing,florida any1,thing1,california some2,thing2,dallas just,fun,kansas B.txt has 8 fields and separate by a comma what,ever,florida-state,,,,,, some,one,dallas_state,,,,,, You will see 3rd fields are the... (5 Replies)
Discussion started by: sabercats
5 Replies

6. Shell Programming and Scripting

Search duplicate field and replace one of them with new value

Dear All, I have file with 4 columns: 1 AA 0 21 2 BB 0 31 3 AA 0 21 4 CC 0 41 I would like to find the duplicate record based on column 2 and replace the 4th column of the duplicate by a new value. So, the output will be: 1 AA 0 21 2 BB 0 31 3 AA 0 -21 4 CC 0 41 Any suggestions... (3 Replies)
Discussion started by: ezhil01
3 Replies

7. Shell Programming and Scripting

Perl - search and replace a particular field

Hi, I have a file having around 30 records. Each record has 5 fields delimited by PIPE. Few records in the file having Junk characters in the field2 and field4. I found the junk charcter and I tested it and replace the junk with space with the command below perl -i -p -e "s/\x00/ /g"... (1 Reply)
Discussion started by: ramkrix
1 Replies

8. Shell Programming and Scripting

awk search and replace field

I am writing a c++ program that has many calls of pow(input,2). I now realize that this is slowing down the program and these all should be input * input for greater speed. There should be a simple way of doing this replacement throughout my file with awk, but I am not very familiar with awk.... (2 Replies)
Discussion started by: bluejayek
2 Replies

9. UNIX for Dummies Questions & Answers

vi search/replace using a set

Hi, I'm trying to do a global search/replace in vi using a set - I want to find every occurance of a carriage return followed by a character and replace it with a space. I've tried the following: :%s/\n/ /g It does the search ok, but it replaces the characters with the literal value ""... (2 Replies)
Discussion started by: HudZo
2 Replies
Login or Register to Ask a Question