lacrosse1991
lacrosse1991

Reputation: 3162

How can I replace all duplicate white spaces in a string with tabs

I have the following string:

'1   NXA-PAC-1100W-B      AC    1080.00     90.00     ok'

I'm trying to convert the white spaces between the words to single tabs using sed, but the spacing in the output does not appear to be equal. What am I doing wrong here?

command:

echo '1   NXA-PAC-1100W-B      AC    1080.00     90.00     ok' | sed 's/\s\s\+/\t/g'

result:

'1       NXA-PAC-1100W-B AC      1080.00 90.00   ok'

Upvotes: 2

Views: 60

Answers (1)

Mort
Mort

Reputation: 3549

+ means "one or more", so in your example you're specifying a space plus one or more spaces, therefore two or more spaces. You just want

echo '1   NXA-PAC-1100W-B      AC    1080.00     90.00     ok' | sed 's/\s\+/\t/g'

Or more verbose but more posix-y

echo '1   NXA-PAC-1100W-B      AC    1080.00     90.00     ok' | sed 's/[[:space:]]\+/\t/g'

There is possibly some misunderstanding about how tabs work in your statement "the spacing in the output does not appear to be equal". But there isn't enough information for me to know for sure and I don't want to insult you if there is not.

Upvotes: 4

Related Questions