pan*_*ear 1 shell awk printf sed
说我在这样的文件中有一些文字:
lorem ipsum asdf gh 12345
hello, world! asdf gh this is a test
ee asdf gh ii
Run Code Online (Sandbox Code Playgroud)
请注意,每行包含"asdf gh".我希望能够使用一些shell命令来基于该分隔符字符串对齐该文本,所以我可以这样做:
$ cat my-file | some-command
lorem ipsum asdf gh 12345
hello, world! asdf gh this is a test
ee asdf gh ii
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我在分隔符周围添加了额外的空格,但这两种方式都没有关系.也就是说,输出也可以是:
$ cat my-file | some-command
lorem ipsum asdf gh 12345
hello, world! asdf gh this is a test
ee asdf gh ii
Run Code Online (Sandbox Code Playgroud)
有一个简单的方法吗?我知道column可以列出内容,但它只能使用空格或字符(不是字符串或模式)作为分隔符.
awk是一种更好的格式输出工具:
awk -F 'asdf gh' '{printf "%-15s%-10s%-10s\n", $1, FS, $2}' file
lorem ipsum asdf gh 12345
hello, world! asdf gh this is a test
ee asdf gh ii
Run Code Online (Sandbox Code Playgroud)
您可以更改数字以printf进行更多自定义.