这可以通过以下方式更轻松地完成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