移动模式文件在一列如果匹配awk - move pattern in file over one column if match awk

- 此内容更新于:2015-12-20
主题:

我寻找一个回答,但短,如果我知道正确的方向,这将是有益的。这是这个问题。我有一个文件。用以下。每个codeblock应该是一个新行我希望我希望能够将第一列移动到第二列是否存在的模式。像下面我可以处理其余的我相信但还想执行一个命令在封闭的值如值,然后跑去把cn等相关联的用户id值。我知道这是一个简单的任务。但想要自动化。谢谢你!

原文:

I have looked for an anwser but came up short, if i know the right direction to head that would be helpful.

heres the problem . I have a file. with the following inside. each codeblock should be a new line

nisNetgroupTriple: (,USER,)
cn:example
nisNetgroupTriple: (,USER,)
nisNetgroupTriple: (,USER,)

I wish i want to be able to move the first column over to the second column if the pattern is present. like below

nisNetgroupTriple: (,USER,)
                 *cn:example
nisNetgroupTriple: (,USER,)
nisNetgroupTriple: (,USER,)

I could deal with the rest I believe but wanted to also perform a command within the values enclosed in the (,,) such as the value USER, such as then running ldapsearch to pull the cn of the associated USERID values.

I know this is a simple task in itself. but wanted to automate it.

Thank You.

网友:你想匹配模式是什么?是吗?

(原文:What pattern are you trying to match? Is it cn:?)

网友:整个线匹配“cn:名称*”

(原文:the entire line matching "cn: NAME*")

解决方案:
第一个字段复制到第二个字段,星号。替换第一个字段标签,得到压痕(你也可以用一堆空间如果你喜欢)。
原文:
awk '/^cn:/ { $2 = "*"$1; $1 = "\t"} 1' input_file > output_file

$2 = "*"$1 copies the first field to the second field, with an asterisk before it. $1 = "\t" replaces the first field with a tab, to get the indentation (you could also use a bunch of spaces if you prefer).

网友:谢谢你先生!!!:-)

(原文:Thank you Sir !!! :-))