2013-02-16 87 views
1

如何在UNIX中的特定行之後插入文本行到文件中?如何在unix中的特定行之後插入文本行到文件中

背景:該文件是一個自動生成的文本文件,但手動編輯它時,每次重新生成一行後添加4行。我可以保證這條線總是在文件夾中,但我不能保證它會在哪條線上出現問題,所以我想根據這條線的位置添加額外的線,而不是增加一個固定的排列號。我想將這個過程自動化,因爲它是我構建過程的一部分。

我使用的是Mac OSX,所以我可以使用unix命令行工具,但我不是很熟悉這樣的工具,不能解決如何做到這一點。

編輯

感謝您的解決方案,雖然我還沒有設法讓他們工作尚未:

我試圖桑達解決方案

sed -i '/<string>1.0</string>/ a <key>CFBundleHelpBookFolder</key>\ 
<string>SongKongHelp</string>\ 
<key>CFBundleHelpBookName</key>\ 
<string>com.jthink.songkong.Help</string> 
' /Applications/SongKong.app/Contents/Info.plist 

,但得到的錯誤

sed: 1: "/Applications/SongKong. ...": invalid command code S 

,我嘗試了bash解決方案離子

#!/bin/bash 

    while read line; do 
     echo "$line" 
     if [[ "$line" = "<string>1.0</string>"]]; then 
     cat mergefile.txt # or echo or printf your extra lines 
     fi 
    done < /Applications/SongKong.app/Contents/Info.plist 

,但得到的錯誤

./buildosx4.sh: line 5: syntax error in conditional expression: unexpected token `;' 
./buildosx4.sh: line 5: syntax error near `;' 
./buildosx4.sh: line 5: ` if [[ "$line" = "<string>1.0</string>"]]; then' 

EDIT 2 現在的工作,我錯過了一個空間

#!/bin/bash 

    while read line; do 
     echo "$line" 
     if [[ "$line" = "<string>1.0</string>" ]]; then 
     cat mergefile.txt # or echo or printf your extra lines 
     fi 
    done < /Applications/SongKong.app/Contents/Info.plist 
+0

***該文件是如何自動生成的?你能修理發電機嗎? – Johnsyweb 2013-02-16 12:04:20

+0

不,我不能,發電機不是由我提供的,也許從長遠來看,它會被固定(我提出了一個問題),但不是在短期內。 – 2013-02-16 12:12:28

+0

你嘗試過搜索嗎?這是一個非常普遍的問題。 – tripleee 2013-02-16 12:15:16

回答

0

另一種方式來看待這個是要在文件中的一個合併兩個文件在某些​​時候。如果你的額外四行是在一個單獨的文件中,你可以做一個比較通用的工具,這樣的:

#!/usr/bin/awk 

BEGIN { 
    SEARCH=ARGV[1]; # Get the search string from the command line 
    delete ARGV[1]; # Delete the argument, so subsequent arguments are files 
} 

# Collect the contents of the first file into a variable 
NR==FNR { 
    ins=ins $0 "\n"; 
    next; 
} 

1 # print every line in the second (or rather the non-first) file 

# Once we're in the second file, if we see the match, print stuff... 
$0==SEARCH { 
    printf("%s", ins); # Using printf instead of print to avoid the extra newline 
    next; 
} 

我已經闡明瞭這一點,以方便文檔的;你顯然可以縮短它看起來更像三合一的答案。你會調用此類似:

$ scriptname "Text to match" mergefile.txt origfile.txt > outputfile.txt 

使用這種方式時,你有可能被用來實現這種合併對不同的文件,並用不同的文字工具。

或者,你當然可以在純粹的bash中做到這一點。

#!/bin/bash 

while read line; do 
    echo "$line" 
    if [[ "$line" = "matchtext" ]]; then 
    cat mergefile.txt # or echo or printf your extra lines 
    fi 
done < origfile.txt 
+0

嘗試了您的腳本,但遇到錯誤./buildosx4.sh:第5行:條件表達式中的語法錯誤:意外標記';' ./buildosx4.sh:第5行:';'附近的語法錯誤 ./buildosx4.sh:line 5:'if [[「$ line」=「 1.0」]];然後' – 2013-02-16 13:15:59

+0

@PaulTaylor - 我從你更新的問題中看到你錯過了']]'之前的空間。你需要這個空間。 – ghoti 2013-02-16 14:40:20

+0

啊,謝謝你現在有用 – 2013-02-16 15:11:09

2

假設標記線包含fnord,並沒有別的;

awk '1;/^fnord$/{print "foo"; print "bar"; 
    print "baz"; print "quux"}' input >output 
+0

+1的方法,但你需要1分號後。 – 2013-02-16 12:59:16

+0

啊,是的,謝謝你的修復。 – tripleee 2013-02-16 13:02:52

+0

+1,但可以更簡單:'printf「foo \ nbar \ n」'甚至是'system(「cat repl-file」)' – 2013-02-16 14:39:56

0

用了sed的「一個與你需要匹配

sed -i '/the target line looks like this/ a this is line 1\ 
this is line 2\ 
this is line 3\ 
this is line 4 
' FILENAME 
+0

我喜歡這個解決方案,除了我喜歡ghotis將行保存在另一個文件中的解決方案,你的解決方案是否可以修改爲從文件中取出4行? – 2013-02-16 13:17:57

+0

它對我的錯誤sed:1失敗:「/ Applications/SongKong。」:無效的命令代碼S – 2013-02-16 13:56:26

+1

保羅因爲'-i'不是標準的'sed'而失敗。它在gnu'sed'中工作,但不是全部'sed'。 – 2013-02-16 14:34:40

0

這個問題可以通過該算法的任何文件大小有效地解決了線的正則表達式的命令:

  1. 閱讀每行從原始文件中打印出來並打印到臨時文件中。
  2. 如果最後一行是標記線,打印您的插入線的臨時文件
  3. 打印其餘線路
  4. 的臨時文件重命名爲原始文件名。

作爲一個Perl腳本:

#!perl 
use strict; use warnings; 

$^I = ".bak"; # create a backup file 

while (<>) { 
    print; 
    last if /regex to determine if this is the line/; 
} 

print <<'END'; 
Yourstuff 
END 

print while <>; # print remaining lines 
# renaming automatically done. 

TESTFILE:

foo 
bar 
baz 
qux 
quux 

正則表達式是​​。

用法:$ perl this_script.pl source-file

處理後的testfile的:

foo 
bar 
Yourstuff 
baz 
qux 
quux 
+0

好吧,你去:perl,awk或者sed。隨你選! – user340140 2013-02-16 12:31:29

+0

您可能應該逃避目標正則表達式,以避免元字符被評估。 – TLP 2013-02-16 14:31:16

相關問題