【问题标题】:Remove odd lines in a text file删除文本文件中的奇数行
【发布时间】:2010-12-09 09:40:27
【问题描述】:

文件:

/home/USER/DIR/a
http://www.here.is.a.hyper.link.net/
/home/USER/DIR/b
http://www.here.is.another.hyper.link.net/

需要删除此文件 (PUBLIC-DIRECTORY-LIST) 中的所有奇数行吗?它适用于我的批处理脚本,可以在下面找到(Dropbox 批处理 puburl 创建者):

for PATH in `cat LIST`
do
echo $PATH
dropbox puburl $PATH
done > PUBLIC-DIRECTORY-LIST

我是否只是将命令附加到脚本末尾的 prune PUBLIC-DIRECTORY-LIST

【问题讨论】:

  • 我仍然非常了解批处理脚本 - 我的第一个脚本 XD。
  • 不要像这样对 cat 使用 for 循环...因为以后会出现空格问题。使用 while 读取循环

标签: linux text sed awk dropbox


【解决方案1】:

为了完整起见,这里是sed 表达式:

sed -e '1d;n;d' file

它和here 完全一样,除了额外的1d 命令,这会删除第一行,因此打印奇数行而不是偶数行。

【讨论】:

    【解决方案2】:
    # awk 'NR%2==0' file
    http://www.here.is.a.hyper.link.net/
    http://www.here.is.another.hyper.link.net/
    

    【讨论】:

      【解决方案3】:

      我会使用 awk,但这只是我自己:

      awk '{if(i++%2)print}' foo.txt
      

      【讨论】:

        猜你喜欢
        • 2014-02-14
        • 1970-01-01
        • 2020-01-02
        • 2011-09-22
        • 1970-01-01
        • 2015-12-22
        • 2011-01-05
        • 2015-02-09
        相关资源
        最近更新 更多