从AWK字符串删除空间 [英] removing a space from a string by awk
问题描述
我如何删除使用AWK位于名和姓之间的一个字符串的空间?
大部分的例子是有关从命令行AWK重定向数据,但我需要处理一个awk脚本中的字符串。
How can I remove a space located between name and lastname from a string by using awk? most of the examples are about redirecting data from command line to awk but I need to manipulate a string inside an awk script.
此转换:
"steve john"
到
"stevejohn"
我有一个字符串变量,我要求用户输入一个awk脚本中。我需要把空格去掉。
I have a string variable which I asked user to input inside an awk script. I need to remove the spaces.
推荐答案
GSUB
是你的朋友。以下命令基本上做一个普通的前pression(在这种情况下,一个空格)的全局替换,用一个空字符串替换它,对目标 $ 1,0
(整行)。
gsub
is your friend. The following command basically does a global substitution of a regular expression (a single space in this case), replacing it with an empty string, on the target $0
(the whole line).
pax> echo "steve john" | awk '{ gsub (" ", "", $0); print}'
stevejohn
您可以通过用户使用的任何目标,包括一个输入:
You can use any target, including one input by a user:
pax> awk 'BEGIN {getline xyzzy ; gsub(" ","", xyzzy) ; print xyzzy}'
hello there my name is pax
hellotheremynameispax
这篇关于从AWK字符串删除空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!