2016-04-22 52 views
1

我是一個noob當涉及到編碼,所以忍受着我...我試圖編寫一個腳本,將數據從3個單獨的文件輸入到文本文件中的3個特定位置 - 例如: 編輯閱讀更容易從多個文件位置匹配後追加文本

#start of script 
start_of_line1_text "$1" end_of_line1_text 
start_of_line2_text "$2" end_of_line2_text 
start_of_line3_text "$3" end_of_line3_text 
#output to text when done 

$1 is the value in text1 
$2 is the value in text2 
$3 is the value in text3 

我想用SED,但不能完全奏效瞭如何做到這一點做的......

或者剛插入爲$ 1字匹配random_text後?即:

sed '/start_of_line1_text/ a middle_of_line1_text' input 

而且在更大的範圍 - 如果text1,2,3人在你怎麼能導入一次這些值1,每次保存新文件多個值嗎?因此,例如:

text1 = 
a 
b 
c 

text2 = 
e 
f 
g 

text3 = 
h 
i 
j 


#start of script 
start_of_line1_text "line one of text1" end_of_line1_text 
start_of_line2_text "line one of text2" end_of_line2_text 
start_of_line3_text "line one of text3" end_of_line3_text 
#output to text when done 

則:

#start of script 
start_of_line1_text "line two of text1" end_of_line1_text 
start_of_line2_text "line two of text2" end_of_line2_text 
start_of_line3_text "line two of text3" end_of_line3_text 
#output to text when done 

我不是挑剔的語言使用,我只是有點堅持以如何適應這一切在一起....

許多在此先感謝

+0

我努力理解你想做什麼,但我不明白。看起來你混合了僞代碼和輸入/輸出;此外,文字過於簡單。每條線上它真的是一樣的嗎?可能不會。你可以通過更清楚你想要發生什麼,分離(僞)代碼和輸入/輸出,並顯示你到目前爲止嘗試過的東西來改善這個問題。 –

回答

0

這個問題很適合awk

試試這個:

awk '!f[FNR] {f[FNR]=("out" FNR ".txt"); print "#start of script" >f[FNR]} {print "random_text \"" $0 "\" some_other_text" >f[FNR]} END {for(n in f) print "#output to text when done">f[n]}' text1 text2 text3 

輸出文件的當前目錄out1.txtout2.txt等產生

您可以在命令末尾提供任何數目的文本文件作爲輸入。

有下面的腳本文件形式與其他參數:

#!/usr/bin/awk -f 
!f[FNR] { 
    f[FNR]=("out" FNR ".txt") 
    print first >f[FNR] 
} 
{ 
    print start $0 end >f[FNR] 
} 
END { 
    for(n in f) print last>f[n] 
} 

測試:

chmod +x ./script.awk 
./script.awk -v first="#start of script"       \ 
      -v start="random_text \"" -v end="\" some_other_text"\ 
      -v last="#output to text when done"     \ 
      text1 text2 text3 
0

我至今是:

#!/usr/bin/perl 

use strict; 
use warnings; 

sub rtrim { my $s = shift; $s =~ s/\s+$//;  return $s }; 

sub read_file_line { 
my $fh = shift; 

if ($fh and my $line = <$fh>) { 
chomp $line; 
return [split(/\t/,$line)]; 
} 
return; 
} 

open(my $f1, "file1.txt"); 
open(my $f2, "file2.txt"); 
open(my $f3, "file3.txt"); 
open(FILE, ">group.txt") or die "Cannot open file"; 

my $pair1 = read_file_line($f1); 
my $pair2 = read_file_line($f2); 
my $pair3 = read_file_line($f3); 

while ($pair1 and $pair2 and $pair3) { 

printf '%s,',$pair1->[0] ; 
printf "%s\n",$pair2->[0] ; 
printf '%s,',$pair3->[0] ; 

printf FILE " line1_text\n" ; 
printf FILE " line2_text\n" ; 
printf FILE " line3_text\n" ; 
printf FILE " start_of_line4_text\"%s\end_of_line4_text\n",$pair3->[0] ; 
printf FILE " start_of_line5_text\"%s\end_of_line5_text\n",$pair2->[0] ; 
printf FILE " start_of_line6_text\"%s\end_of_line6_text\n",rtrim($pair1->[0]) ; 
printf FILE " line7_text\n" ; 
printf FILE " line8_text\n\n\n" ; 



$pair1 = read_file_line($f1); 
$pair2 = read_file_line($f2); 
$pair3 = read_file_line($f3); 

} 

close($f1); 
close($f2); 
close($f3); 
close(FILE) ;