How can I change file value based on condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can I change file value based on condition
# 1  
Old 09-01-2011
How can I change file value based on condition

Hi, Gurus,
I got a problem to resolve following issue:
I have one file file1as following:
Code:
[title1]
start_dt=2010-01-01 12:00:02
[title2]
start_dt=2011-01-01 09:00:02
[title3]
start_dt=2009-01-01 11:00:02

I have another file file2 as following:
Code:
title1, 2010-01-03 10:00:02
title2, 2011-01-04 11:00:02
title3, 2009-02-04 13:00:02

I need replace file1's start_dt value with file2's value based on title value. final file should like following:
Code:
[title1]
start_dt=2010-01-03 10:00:02
[title2]
start_dt=2011-01-04 11:00:02
[title3]
start_dt=2009-02-04 13:00:02

Smilie

Could any one among you may able to help please ?

Thanks in advance
ken002
# 2  
Old 09-01-2011
this would be what you need:

Code:
  awk -F, 'NR==FNR{ a["["$1"]"]=$2} NR>FNR{ if($0 in a){ f =1;i=$0; print $0; next;}else{ $0 = "start_dt="a[i]; print $0; f=0;i=0; } }' file2 file1

output:
Code:
[title1]
start_dt= 2010-01-03 10:00:02
[title2]
start_dt= 2011-01-04 11:00:02
[title3]
start_dt= 2009-02-04 13:00:02

This User Gave Thanks to sk1418 For This Post:
# 3  
Old 09-01-2011
Quote:
Originally Posted by sk1418
this would be what you need:
Code:
  awk -F, 'NR==FNR{ a["["$1"]"]=$2} NR>FNR{ if($0 in a){ f =1;i=$0; print $0; next;}else{ $0 = "start_dt="a[i]; print $0; f=0;i=0; } }' file2 file1

The solution will not work for the following data:

File1:
Code:
[title1]
start_dt=2010-01-01 12:00:02
[title2]
start_dt=2011-01-01 09:00:02
[title3]
start_dt=2009-01-01 11:00:02
[title9]
start_dt=2011-01-01 07:00:02

File2:
Code:
title2, 2010-01-03 10:00:02
title3, 2011-01-04 11:00:02
title7, 2009-02-04 13:00:02

# 4  
Old 09-01-2011
Thanks, sk1418,
after I change awk to nawk (since my OS is sun sparc) it works perfectly.
ken002
# 5  
Old 09-01-2011
OK, corrected version comes:
Code:
awk -F, 'NR==FNR{ a["["$1"]"]=$2} 
NR>FNR{ if($0 in a){ 
f =1;i=$0; print $0; next;      }
if(f==1&&i){ 
        $0 = "start_dt="a[i]; 
        print $0; f=0;i=0; }else print $0 }' file2 file1

output based on your example:

Code:
[title1]
start_dt=2010-01-01 12:00:02
[title2]
start_dt= 2010-01-03 10:00:02
[title3]
start_dt= 2011-01-04 11:00:02
[title9]
start_dt=2011-01-01 07:00:02

# 6  
Old 09-01-2011
Code:
nawk 'BEGIN {
   while (getline < "./file1" > 0)
      if ($0 ~ "title") {
         gsub("\[|\]","",l=$0)
         x[l]=$0
         while (getline s < "./file1" > 0)
            if (s ~ "start_dt=") {
               split(s,a,"=")
               y[l] = a[1]
               z[l] = a[2]
               break
            }
      }
} {
   FS=", "
   if ($1 in x)
      printf("%s\n%s=%s\n", x[$1],y[$1],$2)
}' ./file2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Change the field color based on condition in email

Request your help to change the field color based on condition , if it is otherthan 0. using html in unix. Here is my condition for(i=1;i<=NF;i++) { print "<td> "$i"</td> } Please use CODE tags when displaying sample input, output, and code segments. (17 Replies)
Discussion started by: CatchMe
17 Replies

2. Shell Programming and Scripting

awk to change contents of field based on condition in same file

In the awk below I am trying to copy the entire contents of $6 there may be multiple values seperated by a ;, to $8, if $8 is . (lines 1 and 3 are examples). If that condition $8 is not . (line2 is an example) then that line is skipped and printed as is. The awk does execute but prints the output... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

Join fields in a same file based on condition

I have an input file like this... All iI want to do is If the lines are identical except for the last field i want to merge them into single line input_file I feel something is nothing I feel something is everything apple mango banana apple mango grapes I want to get output like this:... (3 Replies)
Discussion started by: raj_k
3 Replies

4. Shell Programming and Scripting

Adding data from a file based on some condition

I collect data in a file in below format(Month Day Year Size) in RedHat Linux. Now i want to calculate the data size date wise. As i code shell script after long time, i forgot the features and syntax. Can anyone help me regard this please. Feb 8 2014 15 Feb 10 2014 32 Feb 10 2014 32 Feb 12... (2 Replies)
Discussion started by: makauser
2 Replies

5. Shell Programming and Scripting

Getting data from a flat file based on condition

Hi, I have a flaty file from which i am fetching few columns in tablular form as below code. Now i want to fetch the column 6 and 7 in below code only if it either of them is non zero.However below startement awk -F, '$6==0 && $7==0{exit 1}' ${IFILE} is not working..Not sure where is the... (36 Replies)
Discussion started by: Vivekit82
36 Replies

6. Shell Programming and Scripting

Update a field in a file based on condition

Hi i am new to scripting. i have a file file.dat with content as : CONTENT_STORAGE PERCENTAGE FLAG: /storage_01 64% 0 /storage_02 17% 1 I need to update the value of FLAG for a particular CONTENT_STORAGE value I have written the following code #!/bin/sh threshold=20... (1 Reply)
Discussion started by: kichu
1 Replies

7. Shell Programming and Scripting

Change in Input feed based on condition file

Sorry Guys for not being able to explain in one of my earlier post. I am now putting my requirement with the input file and desired output file. In the below input file - Transaction code is at position 31:40. Business code is from position 318:321 TSCM00000005837 ... (7 Replies)
Discussion started by: varunrbs
7 Replies

8. Shell Programming and Scripting

Spliting file based on condition

Hi, I have a comma separated file with millions of records in it. I have a requirement to split the file based on the value in a one of the columns. Suppose i have a text file with columns like C1, C2,C3,C4 Column C4 can hold the values either 01 or 02 03 or 04. I nned to extract... (2 Replies)
Discussion started by: Raamc
2 Replies

9. Shell Programming and Scripting

Read file based on condition

Hi Friends, Can any one help with this: I have a huge file with the format as A SAM 4637 B DEPT1 4758 MILAN A SMITH 46585 B DEPT2 5385 HARRYIS B SAMUL 63547 GEORGE B DANIEL 899 BOISE A FRES 736 74638 I have to read this file and write only the records that starts with "B" only ... (5 Replies)
Discussion started by: sbasetty
5 Replies

10. Shell Programming and Scripting

Splitting a file based on some condition and naming them

I have a file given below. I want to split the file where ever I came across ***(instead you can put ### symbols in the file) . Also I need to name the file by extracting the report name from the first line which is in bold(eg:RPT507A) concatinated with DD(day on which the file runs). Can someone... (1 Reply)
Discussion started by: srivsn
1 Replies
Login or Register to Ask a Question