【问题标题】:Inserting text at begining of each line of text file using shell script [duplicate]使用shell脚本在每行文本文件的开头插入文本[重复]
【发布时间】:2013-08-15 23:14:41
【问题描述】:

我的任务是在每行文本文件的开头输入单词 http://。如何使用 shell 脚本来做到这一点

我的文本文件是这样的:

agr.nc.in
mpi.ni.in
ir.o.in
chemis.go.in
da.ni.in
dgt.go.in
dgn.go.in

输出文件应该是这样的:

http://agr.nc.in
http://mpi.ni.in
http://ir.o.in
http://chemis.go.in
http://da.ni.in
http://dgt.go.in
http://dgn.go.in

【问题讨论】:

  • “行首”的正则表达式是^

标签: linux shell


【解决方案1】:

你可以使用 sed:

$ echo -e 'foo\nbar\nbaz'
foo
bar
baz
$ echo -e 'foo\nbar\nbaz' | sed 's|^|http://|'
http://foo
http://bar
http://baz

【讨论】:

    【解决方案2】:

    使用 sed 就地编辑:

    sed -i 's|^|http://|' infile

    仅使用 shell:

    while read LINE || [ "$LINE" ];do echo "http://$LINE";done <infile >outfile

    【讨论】:

      【解决方案3】:
      awk '$0="http://"$0' your_file
      

      【讨论】:

        猜你喜欢
        • 2011-05-04
        • 2020-01-08
        • 1970-01-01
        • 2020-12-27
        • 2012-03-20
        • 2022-10-19
        • 2016-12-26
        • 1970-01-01
        • 2018-03-07
        相关资源
        最近更新 更多