Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
匹配后显示多行的Shell脚本_Shell_Unix_Awk_Grep_Cat_Perl - Fatal编程技术网

匹配后显示多行的Shell脚本

匹配后显示多行的Shell脚本,shell,unix,awk,grep,cat,perl,Shell,Unix,Awk,Grep,Cat,Perl,我有这个档案 root: admin = true SYSTEM = "compat" registry = files account_locked = false su = true sugroups = system,security,sec_grp daemon: admin = true expires = 0101000070 account_locked = true bin: admin = tr

我有这个档案

root:
    admin = true
    SYSTEM = "compat"
    registry = files
    account_locked = false
    su = true
    sugroups = system,security,sec_grp

daemon:
    admin = true
    expires = 0101000070
    account_locked = true

bin:
    admin = true
    expires = 0101000070
    account_locked = true
我想使用shell脚本只打印特定用户及其属性。我尝试使用cat、grep和awk,但我还没有找到解决方案

有什么想法吗

我已经用我的系统Solaris 9测试了所有答案。这根本不起作用。我不确定我是否做错了什么事?

您可以使用awk。例如:

user=daemon
awk '{if(NF==1 && $1 == "'"$user"':") {flag=1;} else if(NF>1 && flag==1) {print} else {flag=0}}' output.txt
输出:

admin = true
expires = 0101000070
account_locked = true
files
在这种形式下,awk脚本更具可读性:

{
    if(NF==1 && $1 == "'"$user"':") {
        # set a flag when the desired user name appears
        flag = 1;
    } else if (NF > 1 && flag==1) {
        # print regular lines if flag is 1
        print;
    } else {
        # if another user name appears reset the flag
        flag = 0;
    }
}
下面是一个快速的sed解决方案:

sed -ne '/^daemon:/,/^$/p' filename

结尾处包含空白行,但短且值得记住。 另一种策略是在每行上重复节级数据,以使结果对面向行的工具更友好,例如

awk '/^[a-z]/ {user=$1} (NF>1) {$1=user$1; print}' filename
这将生成以下输出:

root:admin = true
root:SYSTEM = "compat"
root:registry = files
root:account_locked = false
root:su = true
root:sugroups = system,security,sec_grp
daemon:admin = true
daemon:expires = 0101000070
daemon:account_locked = true
bin:admin = true
bin:expires = 0101000070
bin:account_locked = true
因此,您可以在末端粘贴一个| grep,以查看该用户的属性。

使用:

输出:

admin = true
expires = 0101000070
account_locked = true
files

awk非常简单:

$ awk '/^daemon:$/,/^$/' file
daemon:
    admin = true
    expires = 0101000070
    account_locked = true
或者,如果希望用户来自变量,可以执行以下操作:

awk -v u="$daemon" '$0 == u,/^$/' file
gnu awk版本:


假设您知道行号是什么:-

sed -n '9,12p' file > output_file
否则,第一个答案是完美的。第一个答案可能更好,因为它是动态的;您将获得整个守护进程节,无论它占用多少行。如果您使用行地址,并且它们发生更改,则脚本将中断

awk '$1 == "daemon:"' RS= input-file

当RS是空字符串时,awk将段落视为记录,并且您的格式可以方便地排列,以便第一个字段$1是用户名。

您从何处获得此输出,哪个命令生成输出?来自AIX平台的cat/etc/security/user我猜AIX上有一些工具专门为某个用户输出值。但不幸的是,我手头没有AIXinvestigation@sputnick表达式表示:打印从$0行等于变量u的行开始的所有行,直到空行^$^是行的开始,$是行的结束。我喜欢你的想法。无论如何,当我尝试使用我的系统Solaris 9时,它不起作用…当您尝试时,会发生什么?bash-2.05 awk'/^[a-z]/{user=$1}NF>1{$1=user$1;print}'aix_user.txt admin=true expires=0101000070 account\u locked=true admin=true expires=0101000070 account\u locked=true admin=false rlogin=false-sugroups=biscore-umask=002 su=true-admin=false-rlogin=false-sugroups=biscore-umask=002似乎只是切断了用户名