【问题标题】:Bash-script, cut the line and paste to next [duplicate]Bash-script,剪切线并粘贴到下一个[重复]
【发布时间】:2017-07-09 11:51:25
【问题描述】:

我是 linux 新手,不知道如何解决我的问题。我有这样的输出(大约 200 行):

Peter Griffin
222
Vasya Pupkin
333

我需要将其转换为:

222, Peter Griffin
333, Vasya Pupkin

我试过了

ldapsearch -LLL -D $bindDN -w $password -p $port -h $host -b $searchBase -S "telephoneNumber" -s sub cn telephoneNumber | grep -v "dn: " | grep "\\n" | awk -F": " '{ print $2 }'

我怎样才能意识到这一点?

【问题讨论】:

  • 请展示您的编码工作。
  • ldapsearch -LLL -D $bindDN -w $password -p $port -h $host -b $searchBase -S "telephoneNumber" -s sub cn 电话号码 | grep -v "dn:" | grep "\\n" | awk -F": " '{ 打印 $2 }'
  • 在此之后,我有一个类似线程的输出

标签: linux bash line


【解决方案1】:

可以单独在Awk完成,

awk '{getline nextLine; print nextLine,$0}' OFS="," file
222,Peter Griffin
333,Vasya Pupkin

因此,使用您的命令,只需将产生这些行的输出通过管道传输到上面的Awk

.. | awk '{getline nextLine; print nextLine,$0}' OFS=","

【讨论】:

  • 很好......
  • 谢谢,它可以工作:)
【解决方案2】:
xargs -L2 < inputfile|awk '{x=$NF;$NF=""; print x "," $0 }'
222,Peter Griffin
333,Vasya Pupkin

解释:xargs -L2是将连续的两行合二为一。将输入更改为单行后,将其提供给awk 以进行列操作。

awk 单独解决方案:

awk 'NR%2{x=$0;next} {print $0,",",x}' inputfile

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-18
    • 2010-12-10
    • 2013-05-13
    相关资源
    最近更新 更多