data searching and pasting with in the file

Hi All,

I have .csv file in which I am trying to manipulate a column date, I started with awk but i am not sure how to do the below logic in .

the file has a 23 columns and in the first row if the value is Trend and in the second column the value is Analysis then the program has to search(with in the file) in the column starting with INS_FLAG and analysis combination in first and second places and see if either of the value is present i.e SINGLE or MULTIPLE( only these combinations will be there) then it has to take this value and write it in the 6th column data where the first column value is trend and second column data is analysis.

I know it's confusing I am posting the sample data below.

source

INS_FLAG,Analysis,,, single,,,
INS_FLAG,ABC,,, NA,,,
INS_FLAG,XYZ,,, ABC,,,
trend,Analysis,,,/abc/xyz/runtime,,,
trend,abc,,,/abc/xyz/runtime,,,

target

INS_FLAG,Analysis,,, single,,,
INS_FLAG,ABC,,, NA,,,
INS_FLAG,XYZ,,, ABC,,,
trend,Analysis,,,/abc/xyz/single,,,
trend,abc,,,/abc/xyz/runtime,,,

Please give me a hint, how can I implement this in unix.

Thanks,Shruthi

Well, you need to be a little more clear.

Input file

INS_FLAG,Analysis,,, single,,, 
INS_FLAG,ABC,,, NA,,, 
INS_FLAG,XYZ,,, ABC,,, 
trend,Analysis,,,/abc/xyz/runtime,,, 
trend,abc,,,/abc/xyz/runtime,,,

What do you mean by column? Is it comma (,) separated?
Your output has the line

trend,Analysis,,,/abc/xyz/single,,, 

where "runtime" is replaced with "single"

yeah, we are confused.
Help us to help you.

regards,
Ahamed

Hi Ahamed,

I am referring column as a comma separated value, and the value of 6th column is like line, I need to pick the value from the
INS_FLAG,Analysis,,, single,,, and replace the single with runtime
trend,Analysis,,,/abc/xyz/runtime,,,

Thx,Shruthi

Try this

#with awk
awk -F, '/INS_FLAG,Analysis/ {val=$5} /trend,Analysis/ {sub(/runtime/,val,$0)} {print}' file > newfile

With the script

#!/bin/ksh
line=$( grep -n "trend,Analysis" file )
if [ ! -z "$line" ]
then
  lineNum=$( echo $line | cut -f1 -d":" ) 
  lineData=$( echo $line | cut -f2 -d":" ) 
  newData=$( grep "INS_FLAG,Analysis" file | awk -F, '{print $5}' )
  lineDat=$( echo $lineData | sed "s!runtime!$newData!g" ) 
  sed -i "${lineNum}d" file
  sed -i "${lineNum} i $lineDat" file
fi

regards,
Ahamed