使用 awk 删除空格

发布于 2024-12-25 21:34:58 字数 293 浏览 4 评论 0原文

我有一个以下形式的文件:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

还给你自由 2025-01-01 21:34:58

如果您希望删除所有字段中的前导空格并保留姓名和职位字段之间的空格那么你可以做这样的事情 -

awk -F";" -v OFS=";" '{for (i=1;i<=NF;i++) gsub (/^ */,"",$i);print}' INPUT_FILE

测试:

[jaypal:~/Temp] cat file
Firstname LastName; 123-4567; Job Title
    Firstname LastName;   123-4567;      Job Title
Firstname LastName;      123-4567; Job Title

[jaypal:~/Temp] awk -F";" -v OFS=";" '{for (i=1;i<=NF;i++) gsub (/^ */,"",$i);print}' file
Firstname LastName;123-4567;Job Title
Firstname LastName;123-4567;Job Title
Firstname LastName;123-4567;Job Title

If you wish to remove leading space from all fields and keep the space in between the Names and Job title fields then you can do something like this -

awk -F";" -v OFS=";" '{for (i=1;i<=NF;i++) gsub (/^ */,"",$i);print}' INPUT_FILE

Test:

[jaypal:~/Temp] cat file
Firstname LastName; 123-4567; Job Title
    Firstname LastName;   123-4567;      Job Title
Firstname LastName;      123-4567; Job Title

[jaypal:~/Temp] awk -F";" -v OFS=";" '{for (i=1;i<=NF;i++) gsub (/^ */,"",$i);print}' file
Firstname LastName;123-4567;Job Title
Firstname LastName;123-4567;Job Title
Firstname LastName;123-4567;Job Title
那小子欠揍 2025-01-01 21:34:58

使用 sed 可以更轻松地完成此操作:

sed 's/^ *//; s/; */;/g'

这假设所有空白都只是空格字符。要包含所有空白字符,请查看 POSIX 字符类

sed 's/^[[:space:]]*//; s/;[[:space:]]*/;/g'

演示(在 OSX 上):

% echo 'Firstname LastName; 123-4567; Job Title
    Firstname LastName;   123-4567;      Job Title
Firstname LastName;      123-4567; Job Title' | sed 's/^[[:space:]]*//; s/;[[:space:]]*/;/g'
Firstname LastName;123-4567;Job Title
Firstname LastName;123-4567;Job Title
Firstname LastName;123-4567;Job Title

如果您的 sed 版本不支持用分号分隔语句,您可以使用 -e 发出单独的命令:

% echo 'Firstname LastName; 123-4567; Job Title
    Firstname LastName;   123-4567;      Job Title
Firstname LastName;      123-4567; Job Title' | sed -e 's/^[[:space:]]*//' -e 's/;[[:space:]]*/;/g'
Firstname LastName;123-4567;Job Title
Firstname LastName;123-4567;Job Title
Firstname LastName;123-4567;Job Title

This can be done far more easily with sed:

sed 's/^ *//; s/; */;/g'

This assumes that all of your whitespace is just space characters. To include all whitespace characters, look at POSIX character classes, viz:

sed 's/^[[:space:]]*//; s/;[[:space:]]*/;/g'

Demo (on OSX):

% echo 'Firstname LastName; 123-4567; Job Title
    Firstname LastName;   123-4567;      Job Title
Firstname LastName;      123-4567; Job Title' | sed 's/^[[:space:]]*//; s/;[[:space:]]*/;/g'
Firstname LastName;123-4567;Job Title
Firstname LastName;123-4567;Job Title
Firstname LastName;123-4567;Job Title

If your version of sed doesn't support separating statements with semicolons, you can issue separate commands using -e:

% echo 'Firstname LastName; 123-4567; Job Title
    Firstname LastName;   123-4567;      Job Title
Firstname LastName;      123-4567; Job Title' | sed -e 's/^[[:space:]]*//' -e 's/;[[:space:]]*/;/g'
Firstname LastName;123-4567;Job Title
Firstname LastName;123-4567;Job Title
Firstname LastName;123-4567;Job Title
云胡 2025-01-01 21:34:58

只需在字段编号上执行 gsub,例如:

gsub (/^ */, "", $1);

这会将所有前导空格替换为空,同时保留所有其他空格不变。 gsub 函数使用指定变量上的新值对给定模式进行全局替换。

在本例中,模式为 ^ *,表示字符串开头后跟零个或多个空格。替换模式是一个空字符串,正在操作的变量是该行中的第一个字段,$1

以下文字记录显示了行中所有列的实际情况,由 i 变量控制。 NF 是当前行中的字段数,$i 指位置 i 处的字段。

$ cat file | awk -F\; -vOFS=\; '{
    for (i = 1; i <= NF; i++) {
        gsub (/^ */, "", $i);
    };
    print}'
Firstname LastName;123-4567;Job Title
Firstname LastName;123-4567;Job Title
Firstname LastName;123-4567;Job Title

Simply execute a gsub on your field number, such as in:

gsub (/^ */, "", $1);

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 position i.

$ cat file | awk -F\; -vOFS=\; '{
    for (i = 1; i <= NF; i++) {
        gsub (/^ */, "", $i);
    };
    print}'
Firstname LastName;123-4567;Job Title
Firstname LastName;123-4567;Job Title
Firstname LastName;123-4567;Job Title
め可乐爱微笑 2025-01-01 21:34:58

有很多方法可以实现你的目标。

只需添加一个有趣的:

awk -v OFS=";" -F'; *' '{gsub(/^ */,"")}$1=$1' file

甚至更短:

awk -v OFS=";" -F'; *' 'gsub(/^ */,"", $1)' file

测试

kent$  echo "Firstname LastName; 123-4567; Job Title
    Firstname LastName;   123-4567;      Job Title
Firstname LastName;      123-4567; Job Title
"|awk -v OFS=";" -F'; *' '{gsub(/^ */,"")}$1=$1'
Firstname LastName;123-4567;Job Title
Firstname LastName;123-4567;Job Title
Firstname LastName;123-4567;Job Title


kent$  echo "Firstname LastName; 123-4567; Job Title
    Firstname LastName;   123-4567;      Job Title
Firstname LastName;      123-4567; Job Title
"|awk -v OFS=";" -F'; *' 'gsub(/^ */,"",$1)'
Firstname LastName;123-4567;Job Title
Firstname LastName;123-4567;Job Title
Firstname LastName;123-4567;Job Title

many ways could achieve your goal.

just add one more for fun:

awk -v OFS=";" -F'; *' '{gsub(/^ */,"")}$1=$1' file

even shorter:

awk -v OFS=";" -F'; *' 'gsub(/^ */,"", $1)' file

test

kent$  echo "Firstname LastName; 123-4567; Job Title
    Firstname LastName;   123-4567;      Job Title
Firstname LastName;      123-4567; Job Title
"|awk -v OFS=";" -F'; *' '{gsub(/^ */,"")}$1=$1'
Firstname LastName;123-4567;Job Title
Firstname LastName;123-4567;Job Title
Firstname LastName;123-4567;Job Title


kent$  echo "Firstname LastName; 123-4567; Job Title
    Firstname LastName;   123-4567;      Job Title
Firstname LastName;      123-4567; Job Title
"|awk -v OFS=";" -F'; *' 'gsub(/^ */,"",$1)'
Firstname LastName;123-4567;Job Title
Firstname LastName;123-4567;Job Title
Firstname LastName;123-4567;Job Title
丑疤怪 2025-01-01 21:34:58

试试这个

{
    gsub(";  *",";")
    gsub("^  *","")
    print
}

Try this

{
    gsub(";  *",";")
    gsub("^  *","")
    print
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文