brad
brad

Reputation: 1023

sed subsitution line by line with contents from another file

I am automating some tasks.

I would like to know if it is possible to substitute each line from a different file in between mentor will be required and the date in the source file

Each line is structured as

blahblahblah....string_I_need_to_replace....blahblahblah

I used sed commands to extract the strings I need to replace because it needs to go from data1,data2,data3, to data1-description; data2 - description; data3 - description

data1,data2,data3 are not always the same and only data1 is guaranteed to be in the line

this line

blahblah....mentor will be required.",Collaboration,Analytical Skills,Integrity & Ethics,3/1/17 20:04 ... blahblah

needs to be

blahblah....mentor will be required.","Collaboration-description;Analytical Skills - description; Integrity & Ethics - description " 3/1/17 20:04 ....blah blah blah

on the first line my parsed file contains

Collaboration-description;Analytical Skills - description; Integrity & Ethics - description 

I have used sed commands to extract the inner part Collaboration-description;Analytical Skills - description; Integrity & Ethics - description on each line in a separate file.

There is many , and ; and numbers before and after the data I am interested in changing and the count varies by line so replace the nth , is going to be an issue there are up to three , when data2 and data3 are empty is reason for the outfile2 and outfile3 processing which removes last ,

#!/bin/bash
unset LANG
cat export-14.csv | sed -n -e 's/^.*mentor will be required.",//p' > outfile
sed 's/[0-9].*//'  outfile > outfile2
sed 's/,$//'       outfile2 > outfile3
sed 's/,$//'       outfile3 > outfile4
sed 's/,/;/g'      outfile4 >outfile5
sed 's/Analytical SKills/Analytical Skills - identifying and solving problems/' outfile5 > outfile6
sed 's/Adaptability/Adaptability - embracing opportunities for improvement and resilience/' outfile6 > outfile7
sed 's/Collaboration/Collaboration - working with others/' outfile7 > outfile8
sed 's/Technology/Technology - employing current and emerging software\/tools/' outfile8 > outfile9
sed "s/Communication/Communication - articulating one\'s self/g" outfile9 > outfile10

Upvotes: 1

Views: 70

Answers (1)

Ed Morton
Ed Morton

Reputation: 203684

The answer to your question I would like to know if it is possible to substitute each line from a different file in between mentor will be required and the date in the source file is yes but without concise, testable sample input and expected output that's really as much info as can be provided.

Having said that - your current series of sed scripts can be rewritten as a single awk script:

awk 'sub(/^.*mentor will be required.",/,"") {
    sub(/,{1,2}[0-9].*/,"")
    gsub(/,/,";")
    sub(/Analytical SKills/,"Analytical Skills - identifying and solving problems")
    sub(/Adaptability/,"Adaptability - embracing opportunities for improvement and resilience")
    sub(/Collaboration/,"Collaboration - working with others")
    sub(/Technology/,"Technology - employing current and emerging software/tools")
    sub(/Communication/,"Communication - articulating one\047s self")
}' export-14.csv > outfile10

and that would be a much better starting point for whatever it is you're trying to do next.

Upvotes: 2

Related Questions