使用 awk 删除空格
我有一个以下形式的文件:
Firstname LastName; 123-4567; Job Title
Firstname LastName; 123-4567; Job Title
Firstname LastName; 123-4567; Job Title
...
我正在尝试使用 awk 将文件解析为 makedbm 可读的形式(以制作自定义 NIS 映射)。字段分隔符是分号。我需要能够删除每行每个字段中的所有前导空格,但在名称字段和标题字段中保留空格。谢谢。
I have a file in the form of:
Firstname LastName; 123-4567; Job Title
Firstname LastName; 123-4567; Job Title
Firstname LastName; 123-4567; Job Title
...
I am trying to use awk to parse the file into a form readable by makedbm (to make a custom NIS map). Field separator is a semicolon. I need to be able to remove all leading whitespace from each field on each line, but leave the spaces in the name field and the title field. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您希望删除
所有字段中的前导空格
并保留姓名和职位字段之间的空格
那么你可以做这样的事情 -测试:
If you wish to remove
leading space from all fields
and keep thespace in between the Names and Job title fields
then you can do something like this -Test:
使用 sed 可以更轻松地完成此操作:
这假设所有空白都只是空格字符。要包含所有空白字符,请查看 POSIX 字符类,即:
演示(在 OSX 上):
如果您的
sed
版本不支持用分号分隔语句,您可以使用-e
发出单独的命令:This can be done far more easily with
sed
:This assumes that all of your whitespace is just space characters. To include all whitespace characters, look at POSIX character classes, viz:
Demo (on OSX):
If your version of
sed
doesn't support separating statements with semicolons, you can issue separate commands using-e
:只需在字段编号上执行 gsub,例如:
这会将所有前导空格替换为空,同时保留所有其他空格不变。 gsub 函数使用指定变量上的新值对给定模式进行全局替换。
在本例中,模式为
^ *
,表示字符串开头后跟零个或多个空格。替换模式是一个空字符串,正在操作的变量是该行中的第一个字段,$1
。以下文字记录显示了行中所有列的实际情况,由
i
变量控制。NF
是当前行中的字段数,$i
指位置i
处的字段。Simply execute a gsub on your field number, such as in:
This will substitute all leading spaces with nothing, while leaving all other spaces intact. The
gsub
function does a global substitution of a given pattern with a new value on a specified variable.In this case, the pattern is
^ *
, meaning the start of string followed by zero or more spaces. The replacement pattern is an empty string, and the variable being operated on is the first field in the row,$1
.The following transcript shows this in action, for all columns in the row, controlled by the
i
variable.NF
is the number of fields in the current row and$i
refers to the field at positioni
.有很多方法可以实现你的目标。
只需添加一个有趣的:
甚至更短:
测试
many ways could achieve your goal.
just add one more for fun:
even shorter:
test
试试这个
Try this