2015-06-12 53 views
0

在下面的代碼中,我們將單詞添加到另一個下面。我想在行尾添加如何在bash中添加一個單詞到一行的末尾?

doc_in=$1 #document to read 
main_d=$2 #dictionary of words 
ignored_d=$3 #other dictionary of words  
doc_out=$4 #document to write 

word="example"   

function consult_user() { 
    echo "word not found:[$word] Accept (a) - Discard (i) - Replace (r):" 

    read opt 
    if [ $opt == "a" ] 
    then  
     #Here I want to add at the end of a line and not "echo" 
     echo $word >>$doc_out # me agrega todo con un \n 
    elif [ $opt == "i" ] 
    then 
     echo $word >>$ignored_d 
    else 
     read new_w 
     echo $new_w >>$doc_out 
    fi 
} 

consult_user 
+0

因此,這基本上是一個拼寫檢查器,你想寫出一個輸入文件的副本,刪除一些單詞並替換一些單詞,而不改變折行?你孤立的代碼沒有什麼意義,如果我猜對了,你需要改變大部分代碼,然後才能得到你要求的代碼。 – tripleee

+0

'echo word >> file'在'word'後添加一個換行符。所以你必須讓* previous * echo省略換行符才能在同一行上得到另一個單詞。 – tripleee

回答

0

您可以使用sed命令來完成它。

sed -i -e "s/$/$word/" $doc_out 

[[email protected] ~]# word=example 
[[email protected] ~]# doc_out=test 
[[email protected] ~]# cat $doc_out 
Test Line1 
Test Line2 
Test Line3 
Test Line4 
[[email protected] ~]# sed -e "s/$/ $word/" $doc_out 
Test Line1 example 
Test Line2 example 
Test Line3 example 
Test Line4 example 
+0

它給我一個錯誤:「無法讀取文件:沒有這樣的文件或目錄」 –

+0

我可以看到它正在工作,在答案中添加了測試輸出 –

相關問題