2013-02-16 86 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

相关问题