LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   concatenate string with in a comma delimited file (http://www.linuxquestions.org/questions/linux-general-1/concatenate-string-with-in-a-comma-delimited-file-4175457004/)

j-me 04-05-2013 03:49 PM

concatenate string with in a comma delimited file
 
have a few questions here.

I have a comma delimited file that contains several lines like:
Code:

Deploy_ACC_FBL.Legacy.ACV_Production,1,2012.09.32,SUCCESS,
Deploy_ALU_Production,1,2012.09.3,FAILURE,e012835
Deploy_CMI_AdjusterXML_Production,2013-03-13_16-50-14,MBC.ClientMidtierInterface_2013.02.58,FAILURE,e009137
Deploy_PRT_Production,2013-03-15_22-05-00,prt_2012.11.57,SUCCESS,e009137
Deploy_RRE_Production,2012-08-29_09-52-17,2012.08.134,SUCCESS,e012835
Deploy_RRE_Production,2012-09-02_07-35-57,RRE_2012.08.141,FAILURE,e009137

I need to take the first string in the file:
Deploy_ up to the first comma(,) then strip off the 'Deploy_' and the '_Production' then concatenate the result to the third string in the same line. I have found the method to accomplish the separation and selection:
Code:

sysid=`awk -F"," '{print $1}' $inputfile | cut -d_ -f2- | rev | cut -d_ -f2- | rev `
  tagnfo=`awk -F"," '{print $3}' $inputfile`

but my problem is concatenating the result while in a loop reading the file a line at a time and output the result into a new file.

resulting line evaluated line:

Deploy_RRE_Production,2012-08-29_09-52-17,2012.08.134,SUCCESS,e012835

like:
Deploy_RRE_Production,2012-08-29_09-52-17,RRE_2012.08.134,SUCCESS,e012835

another hanging issue is detecting the existence of a value in the third string anything prior to the _2012.08.141; remove the value prior to the '_' and replace with the value from the first string 'sysid' as some are not correct or in proper case.

hope this makes sense. thank you.

allend 04-05-2013 11:57 PM

This is a bash solution that I think meets your requirements.
Code:

#!/bin/bash

IFS=","
file=$1

while read -a aline ; do
  temp=${aline[0]#Deploy_};
  temp=${temp%_Production};
  echo ${aline[0]},${aline[1]},$temp"_"${aline[2]##*_},${aline[3]},${aline[4]};
done < $file

Using your data I get this output.
Quote:

Deploy_ACC_FBL.Legacy.ACV_Production,1,ACC_FBL.Legacy.ACV_2012.09.32,SUCCESS,
Deploy_ALU_Production,1,ALU_2012.09.3,FAILURE,e012835
Deploy_CMI_AdjusterXML_Production,2013-03-13_16-50-14,CMI_AdjusterXML_2013.02.58,FAILURE,e009137
Deploy_PRT_Production,2013-03-15_22-05-00,PRT_2012.11.57,SUCCESS,e009137
Deploy_RRE_Production,2012-08-29_09-52-17,RRE_2012.08.134,SUCCESS,e012835
Deploy_RRE_Production,2012-09-02_07-35-57,RRE_2012.08.141,FAILURE,e009137

j-me 04-08-2013 09:08 AM

well, thank you very much. That works great.

I had a few questions to understand the script but after performing some searches, I found the answers.

I greatly appreciate the assistance. I constantly am learning new methods and am amazed by what one can accomplish with linux. Plus, the online community support is much better than most believe nor care to understand the benefit.

Thank you!!!!


All times are GMT -5. The time now is 11:58 AM.