我想改变这个:
--- BEGINNING OF file.txt ---
# use another authentication method.
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all
--- END ---
......对此:
--- BEGINNING OF file.txt ---
# use another authentication method.
# TYPE DATABASE USER ADDRESS METHOD
--- END ---
这是我最近的尝试(“ - i”要添加):
sed "s/^\(.*type\s*database\s*user\s*address\s*method\).*$/\1/i" file.txt
谢谢!
答案 0 :(得分:3)
s
命令确实是sed的瑞士军刀,但是当你想删除整条线时,这是错误的工具。您需要d
命令:
sed '0,/type\s\+database\s\+user\s\+address\s\+method/I!d'
地址0,/.../I
匹配从文件开头(0
)到匹配正则表达式的第一行的所有行,不区分大小写(I
- 在GNU sed中可用但是不是POSIX sed)。 !
会将匹配项反转,因此d
会删除不匹配的行。 Try it online!
答案 1 :(得分:1)
使用maximumBy' func list = foldl1' (\max i -> let (maxby, iby) = (func max, func i) in if iby > maxby then i else max) list
:
awk
或者,
$ awk -v f=1 'f
{s=tolower($0)}
s~/type\s+database\s+user\s+address\s+method/{f=!f}' file
概要
$ awk 'BEGIN{f=1}
f
{s=tolower($0)}
s~/type\s+database\s+user\s+address\s+method/{exit}' file
可以进一步简化为:
BEGIN{f=1} # set a flag to true value
f # print if that flag is true
{s=tolower($0)} # make the line lowercase
s~/regex/{exit} # at the regex but after printing -- exit