Using Awk to remove whitespace

夙愿已清 提交于 2019-12-21 06:28:27

问题


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.


回答1:


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



回答2:


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



回答3:


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



回答4:


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



回答5:


Try this

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


来源:https://stackoverflow.com/questions/8766165/using-awk-to-remove-whitespace

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!