2017-03-17 73 views
1

我正在自動執行一些任務。sed從另一個文件的內容逐行替換

我想知道是否有可能從不同的文件替換每一行中 導師將需要和源文件中的時間之間

每一行被構造爲

blahblahblah....string_I_need_to_replace....blahblahblah 

我用sed命令解壓我需要更換,因爲它需要去從data1,data2,data3,data1-description; data2 - description; data3 - description

data1,data2,data3並不總是個字符串Ë相同,僅data1是保證在線路

此行

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

需求是

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

在第一行我解析的文件包含

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

我已經使用sed命令來提取每行的內部部分Collaboration-description;Analytical Skills - description; Integrity & Ethics - description在一個單獨的文件中。

有很多,;和數字之前和之後我有興趣改變的數據和計數因行而異所以替換第n個,將是一個問題有多達三個,當data2和data3是空是原因outfile2outfile3處理從而消除最後,

#!/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 
+0

我不確定我在這裏看到一個問題。也就是說,'sed -i -e'...'-e'...'-e'...''會表現更好。 – bishop

+0

謝謝我的問題被埋沒了,很難看到我把真正的問題放在頂部附近「我想知道是否有可能用不同的文件替換每一行,導師將需要和源文件中的日期」 – brad

+0

提出了一個醜陋的非靈活的解決方案,日期形式爲,年/月/年只發生一次每一行和導師將需要的數據之前,我需要改變,而不是做出單獨的文件我使用導師...和作爲標記的日期和替換內部的一切。工程,但醜陋 – brad

回答

2

的回答你的問題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但不簡潔,可測試的樣本輸入和預期的輸出是作爲可以提供真正的儘可能多的信息。

話雖如此 - 你目前的一系列的sed腳本可以改寫爲一個單一的awk腳本:

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 

,這將是一個更好的起點,不管它是你想下一步該怎麼做。

相關問題