Grep 无法识别白色 space
Grep not recognizing white space
我有一个文件(哈利波特第一章)有大量的白色space。例如:
CHAPTER ONE
The Boy Who Lived
M r and Mrs Dursley, of number four, Privet Drive, were
proud to say that they were perfectly normal, thank
you very much. They were the last people you’d expect to be
involved in anything strange or mysterious, because they just
didn’t hold with such nonsense.
Mr Dursley was the director of a fi rm called Grunnings,
which made drills. He was a big, beefy man with hardly
any neck, although he did have a very large moustache.
Mrs Dursley was thin and blonde and had nearly twice the
usual amount of neck, which came in very useful as she spent
so much of her time craning over garden fences, spying on the
neighbours. The Dursleys had a small son called Dudders and
我的objective,在学习命令行工具的时候,就是(先用grep
识别然后)去掉所有的白色space,如下:
CHAPTER ONE
The Boy Who Lived
M r and Mrs Dursley, of number four, Privet Drive, were
proud to say that they were perfectly normal, thank
you very much. They were the last people you’d expect to be
involved in anything strange or mysterious, because they just
didn’t hold with such nonsense.
Mr Dursley was the director of a fi rm called Grunnings,
which made drills. He was a big, beefy man with hardly
any neck, although he did have a very large moustache.
Mrs Dursley was thin and blonde and had nearly twice the
usual amount of neck, which came in very useful as she spent
so much of her time craning over garden fences, spying on the
neighbours. The Dursleys had a small son called Dudders and
我正在尝试使用 grep
来识别具有多个白色 space 的行。在此,我尝试了以下(除其他外):
$ grep "(\s){2,}" file
$ grep "(\ ){2,}" file
$ grep "([[:space:]]){2,}" file
$ grep "[[:space:]]{2,}" file
其中 None 产生了任何匹配项。我已经确认那里有白色 space 和 Vim。我在 regex101.com 上同样确认了这些语法中的每一个。我还根据 grep " " file
(和品种)检查了文件,并看到所有带有 any 白色 space 的行都正确输出。
此查询的正确语法是什么?
给定:
cat file
CHAPTER ONE
The Boy Who Lived
M r and Mrs Dursley, of number four, Privet Drive, were
proud to say that they were perfectly normal, thank
you very much. They were the last people you’d expect to be
involved in anything strange or mysterious, because they just
didn’t hold with such nonsense.
Mr Dursley was the director of a fi rm called Grunnings,
which made drills. He was a big, beefy man with hardly
any neck, although he did have a very large moustache.
Mrs Dursley was thin and blonde and had nearly twice the
usual amount of neck, which came in very useful as she spent
so much of her time craning over garden fences, spying on the
neighbours. The Dursleys had a small son called Dudders and
你最好的选择是 sed
删除前导空格:
sed -E 's/^[[:blank:]]{2,}//' file
CHAPTER ONE
The Boy Who Lived
M r and Mrs Dursley, of number four, Privet Drive, were
proud to say that they were perfectly normal, thank
you very much. They were the last people you’d expect to be
involved in anything strange or mysterious, because they just
didn’t hold with such nonsense.
Mr Dursley was the director of a fi rm called Grunnings,
which made drills. He was a big, beefy man with hardly
any neck, although he did have a very large moustache.
Mrs Dursley was thin and blonde and had nearly twice the
usual amount of neck, which came in very useful as she spent
so much of her time craning over garden fences, spying on the
neighbours. The Dursleys had a small son called Dudders and
或 awk
:
awk '{sub(/^[[:blank:]]{2,}/,"")} 1' file
# same output
如果你只想识别那些开头有2个或更多空格的行 grep
:
grep -E '^[[:blank:]]{2,}' file
您遇到的问题是 grep
和 sed
默认使用基本正则表达式 (BRE)。您需要使用 -E
选项来触发使用扩展正则表达式 (ERE)。
HERE 是 BRE 和 ERE 的差值。
awk
默认使用 ERE。
我有一个文件(哈利波特第一章)有大量的白色space。例如:
CHAPTER ONE
The Boy Who Lived
M r and Mrs Dursley, of number four, Privet Drive, were
proud to say that they were perfectly normal, thank
you very much. They were the last people you’d expect to be
involved in anything strange or mysterious, because they just
didn’t hold with such nonsense.
Mr Dursley was the director of a fi rm called Grunnings,
which made drills. He was a big, beefy man with hardly
any neck, although he did have a very large moustache.
Mrs Dursley was thin and blonde and had nearly twice the
usual amount of neck, which came in very useful as she spent
so much of her time craning over garden fences, spying on the
neighbours. The Dursleys had a small son called Dudders and
我的objective,在学习命令行工具的时候,就是(先用grep
识别然后)去掉所有的白色space,如下:
CHAPTER ONE
The Boy Who Lived
M r and Mrs Dursley, of number four, Privet Drive, were
proud to say that they were perfectly normal, thank
you very much. They were the last people you’d expect to be
involved in anything strange or mysterious, because they just
didn’t hold with such nonsense.
Mr Dursley was the director of a fi rm called Grunnings,
which made drills. He was a big, beefy man with hardly
any neck, although he did have a very large moustache.
Mrs Dursley was thin and blonde and had nearly twice the
usual amount of neck, which came in very useful as she spent
so much of her time craning over garden fences, spying on the
neighbours. The Dursleys had a small son called Dudders and
我正在尝试使用 grep
来识别具有多个白色 space 的行。在此,我尝试了以下(除其他外):
$ grep "(\s){2,}" file
$ grep "(\ ){2,}" file
$ grep "([[:space:]]){2,}" file
$ grep "[[:space:]]{2,}" file
其中 None 产生了任何匹配项。我已经确认那里有白色 space 和 Vim。我在 regex101.com 上同样确认了这些语法中的每一个。我还根据 grep " " file
(和品种)检查了文件,并看到所有带有 any 白色 space 的行都正确输出。
此查询的正确语法是什么?
给定:
cat file
CHAPTER ONE
The Boy Who Lived
M r and Mrs Dursley, of number four, Privet Drive, were
proud to say that they were perfectly normal, thank
you very much. They were the last people you’d expect to be
involved in anything strange or mysterious, because they just
didn’t hold with such nonsense.
Mr Dursley was the director of a fi rm called Grunnings,
which made drills. He was a big, beefy man with hardly
any neck, although he did have a very large moustache.
Mrs Dursley was thin and blonde and had nearly twice the
usual amount of neck, which came in very useful as she spent
so much of her time craning over garden fences, spying on the
neighbours. The Dursleys had a small son called Dudders and
你最好的选择是 sed
删除前导空格:
sed -E 's/^[[:blank:]]{2,}//' file
CHAPTER ONE
The Boy Who Lived
M r and Mrs Dursley, of number four, Privet Drive, were
proud to say that they were perfectly normal, thank
you very much. They were the last people you’d expect to be
involved in anything strange or mysterious, because they just
didn’t hold with such nonsense.
Mr Dursley was the director of a fi rm called Grunnings,
which made drills. He was a big, beefy man with hardly
any neck, although he did have a very large moustache.
Mrs Dursley was thin and blonde and had nearly twice the
usual amount of neck, which came in very useful as she spent
so much of her time craning over garden fences, spying on the
neighbours. The Dursleys had a small son called Dudders and
或 awk
:
awk '{sub(/^[[:blank:]]{2,}/,"")} 1' file
# same output
如果你只想识别那些开头有2个或更多空格的行 grep
:
grep -E '^[[:blank:]]{2,}' file
您遇到的问题是 grep
和 sed
默认使用基本正则表达式 (BRE)。您需要使用 -E
选项来触发使用扩展正则表达式 (ERE)。
HERE 是 BRE 和 ERE 的差值。
awk
默认使用 ERE。