Linux awk-根据字符串长度打印x个字符数
因此,我尝试读取一些配置文件,并将find中的信息通过管道传输到grep,然后发送到awk,如下所示:Linux awk-根据字符串长度打印x个字符数,linux,bash,shell,awk,Linux,Bash,Shell,Awk,因此,我尝试读取一些配置文件,并将find中的信息通过管道传输到grep,然后发送到awk,如下所示: find /path/to/file/ -name config.php -print0 | xargs -0 grep -E 'cfg_database|cfg_user|cfg_password' | awk -F"[:']" -v OFS="" '{print (NR%3==1) ? $1 " | Database: " : "", (NR%3==2) ? "Username: " :
find /path/to/file/ -name config.php -print0 | xargs -0 grep -E 'cfg_database|cfg_user|cfg_password' | awk -F"[:']" -v OFS="" '{print (NR%3==1) ? $1 " | Database: " : "", (NR%3==2) ? "Username: " : "", (NR%3==0) ? "Password: " : "", $5}'
将有多个结果,因此默认情况下,find | xargs grep输出为:
/path/to/file/dir1/config.php:define('cfg_database', 'db1');
/path/to/file/dir1/config.php:define('cfg_user', 'user1');
/path/to/file/dir1/config.php:define('cfg_password', 'pass1');
/path/to/file/dir2/config.php:define('cfg_database', 'db2');
/path/to/file/dir2/config.php:define('cfg_user', 'user2');
/path/to/file/dir2/config.php:define('cfg_password', 'pass2');
/path/to/file/dir1/config.php | Database: db1
Username: user1
Password: pass1
使用我现在拥有的命令,输出为:
/path/to/file/dir1/config.php:define('cfg_database', 'db1');
/path/to/file/dir1/config.php:define('cfg_user', 'user1');
/path/to/file/dir1/config.php:define('cfg_password', 'pass1');
/path/to/file/dir2/config.php:define('cfg_database', 'db2');
/path/to/file/dir2/config.php:define('cfg_user', 'user2');
/path/to/file/dir2/config.php:define('cfg_password', 'pass2');
/path/to/file/dir1/config.php | Database: db1
Username: user1
Password: pass1
但我希望能够预先添加空格,以便根据文件$1的路径长度再加上三个空格来排列所有结果
/path/to/file/dir1/config.php | Database: db1
Username: user1
Password: pass1
我在这里看到的唯一方法是在print中使用长度为$1的循环,或者使用printf。我无法找到在当前命令中获取循环的任何方法,我对printf也不太了解,也无法找到确定读取哪一行的方法。不管怎样,如果有人能给我指出正确的方向,我将不胜感激
awk 'NR==1{len=length($0)} {printf "%"len"s\n",$0}' yourfile
/path/to/file/dir1/config.php | Database: db1
Username: user1
Password: pass1
这里,第一行的长度用于计算字符串输出的格式,它存储在len变量中
your_command | awk 'FNR==1{l=index($0,"|")+1}FNR>1{$0=sprintf("%*s%s",l,"",$0)}1'
这里,第一行的长度用于计算字符串输出的格式,它存储在len变量中
your_command | awk 'FNR==1{l=index($0,"|")+1}FNR>1{$0=sprintf("%*s%s",l,"",$0)}1'
例如:
$ cat f
/path/to/file/dir1/config.php | Database: db1
Username: user1
Password: pass1
$ awk 'FNR==1{l=index($0,"|")+1}FNR>1{$0=sprintf("%*s%s",l,"",$0)}1' f
/path/to/file/dir1/config.php | Database: db1
Username: user1
Password: pass1
解释
indexstr,sub
它检查sub是否是str的子字符串。在成功的时候,它
返回sub开始的位置;否则返回0。这个
str的第一个字符位于位置1
Sprintformat、表达式1…
返回而不打印printf将打印的字符串
以同样的论据出局
sprintf%*s%s,l,,$0
Like指定为字符串分配多少空间
*宽度不是在格式字符串中指定的,而是作为
必须执行的参数之前的附加整数值参数
要格式化
与
sprintf("%5s", "");
范例
用于动态指定字段的宽度:
$ awk 'BEGIN{printf("%*s%s\n",5,"","mystring")}'
mystring
$ awk 'BEGIN{printf("%5s%s\n","","mystring")}'
mystring
例如:
$ cat f
/path/to/file/dir1/config.php | Database: db1
Username: user1
Password: pass1
$ awk 'FNR==1{l=index($0,"|")+1}FNR>1{$0=sprintf("%*s%s",l,"",$0)}1' f
/path/to/file/dir1/config.php | Database: db1
Username: user1
Password: pass1
解释
indexstr,sub
它检查sub是否是str的子字符串。在成功的时候,它
返回sub开始的位置;否则返回0。这个
str的第一个字符位于位置1
Sprintformat、表达式1…
返回而不打印printf将打印的字符串
以同样的论据出局
sprintf%*s%s,l,,$0
Like指定为字符串分配多少空间
*宽度不是在格式字符串中指定的,而是作为
必须执行的参数之前的附加整数值参数
要格式化
与
sprintf("%5s", "");
范例
用于动态指定字段的宽度:
$ awk 'BEGIN{printf("%*s%s\n",5,"","mystring")}'
mystring
$ awk 'BEGIN{printf("%5s%s\n","","mystring")}'
mystring
或
或