- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Single white space replacement whilst ignoring mul...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-15-2010 05:47 AM
тАО11-15-2010 05:47 AM
does anyone know how to replace a single white space and leave and multiple spaces alone....
so, for example:
echo "12 3 4 5 6"
should produce
12 34 56
(where the only single spaces were between 3 &4 and 5& )
awk/sed or perl is fine thks - preferable in one line - thks
Solved! Go to Solution.
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-15-2010 07:08 AM
тАО11-15-2010 07:08 AM
Re: Single white space replacement whilst ignoring multiple spaces
$ echo '12 3 4 5 6' | \
sed -e 's/\([^ ]\) \([^ ]\)/\1\2/g'
12 34 56
(Replace non-space1+space+non-space2 with
non-space1+non-space2 everywhere.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-15-2010 08:06 AM
тАО11-15-2010 08:06 AM
Re: Single white space replacement whilst ignoring multiple spaces
A bit late, but in Perl:
echo "12 3 4 5 6"|perl -ple 's/(\S)\s(\S)/$1$2/g'
12 34 56
The '\s' is a whitespace (space, tab) character whereas the '\S' is a non-whitespace character.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-15-2010 11:46 AM
тАО11-15-2010 11:46 AM
Solution# echo "12 3 4 5 6"|perl -ple 's/\b\s\b//g'
12 345 6
(12ss3s4s5ss6)
Please observe how this replaced ALL single-white-space instances.
JRF's solution, would replace the single space between 3 and 4, but would NOT replace the single space between 4 and 5 for the example I used, as the match engine has already dealt with '4'.
You have to decide what is 'right' for your usage. I happen to think the using \b is more true to the original problem statement.
fwiw,
Hein
# echo "12 3 4 5 6"|perl -ple 's/(\S)\s(\S)/$1$2/g'
12 34 5 6
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-15-2010 06:54 PM
тАО11-15-2010 06:54 PM
Re: Single white space replacement whilst ignoring multiple spaces
Hein, I think your solution was the most robust
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-16-2010 07:57 PM
тАО11-16-2010 07:57 PM
Re: Single white space replacement whilst ignoring multiple spaces
It seems Steven's fails the same way. But one solution is simple, do it again:
echo '12 3 4 4 5 6' | sed -e 's/\([^ ]\) \([^ ]\)/\1\2/g' -e 's/\([^ ]\) \([^ ]\)/\1\2/g'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-16-2010 08:12 PM
тАО11-16-2010 08:12 PM
Re: Single white space replacement whilst ignoring multiple spaces
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-17-2010 04:24 AM
тАО11-17-2010 04:24 AM
Re: Single white space replacement whilst ignoring multiple spaces
> I re-opened this thread as I think that this is an interesting problem ...
From a Perl perspective, I think Hein provided the most general, most direct solution. It works for the original cases as well as Dennis's sample data.
Dennis does offer an interesting solution, though. My original offering of:
# perl -ple 's/(\S)\s(\S)/$1$2/g'
...could be modified to perform the repetitive substitutions of Dennis's 'sed' by writing:
# echo "12 3 4 4 5 6"|perl -ple '1 while s/(\S)\s(\S)/$1$2/g'
12 344 56
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-17-2010 05:02 AM
тАО11-17-2010 05:02 AM
Re: Single white space replacement whilst ignoring multiple spaces
Ive tested it with more variations of the sample data...and files . Both remove white spaces and ignore multiple instances of them -thks!