- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Getting rid of extra spaces and tabs???
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
тАО04-25-2002 08:13 AM
тАО04-25-2002 08:13 AM
I have a large textfile (5,000 lines or so) that originally came from a well-known Windows word processor. After running it through a re-formatter the file is filled with extra spaces and tabs between words. Some lines have extra spaces and tabs at the beginning and end of the lines as well. For example:
[TAB] Test1 [TAB] [TAB] Test2 [TAB]
should be
Test1 Test2
I would like to remove all the extra spaces and tabs. All the spaces at the beginning and end of the line should be removed and multiple spaces and tabs inside each line should be replaced with a single space. Does anyone have an idea? Sed? Awk? Help!!!
TIA, Bob
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-25-2002 08:21 AM
тАО04-25-2002 08:21 AM
SolutionWhile sed or awk could be used, my weapon of choice for this is Perl. Perl has the substitution operator with '/s+/ which can represent one or more whitespace characters. You don't have to specify separate code for tabs or sspaces or other whitespace. Perl's pattern match is really regular expressions on steroids.
I already had a pet subroutine 'trim_ws' to remove lead/trailing whitespace so I simply added a trim_middle_whitespace. Less than 2 minutes of Perl.
cat oldfile | strip.pl > newfile
This should be very close, Clay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-25-2002 08:27 AM
тАО04-25-2002 08:27 AM
Re: Getting rid of extra spaces and tabs???
You could use 'vi' or 'sed' and strip unnecessary spaces and tabs from the ends of lines with:
s/[ \t]*$//
...and from the beginning, with:
s/[ \t]*^//
You can substitute pressing the tab key on your keyboard in lieu of the '\t'.
You could also use 'expand' to change tabs to spaces (see man 'expand').
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-25-2002 08:28 AM
тАО04-25-2002 08:28 AM
Re: Getting rid of extra spaces and tabs???
to replace spaces with nothing....
sed 's/ //g'
to replace tabs with nothing...
sed 's/ //g'
tx,
c
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-25-2002 09:10 AM
тАО04-25-2002 09:10 AM
Re: Getting rid of extra spaces and tabs???
If you need only one space between fields then use the tr command
cat test | tr -s "\t" " "
will trim all the uneven spaces/tabs to one space ...
Thanks,
Shabu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-25-2002 09:24 AM
тАО04-25-2002 09:24 AM
Re: Getting rid of extra spaces and tabs???
A correction. To strip leading spaces and/or tabs, use:
# sed 's/^[ \t]*//'
...substituting the actual keyboard TAB for the \t
...and similarly for trailing spaces and/or tabs:
# sed 's/[ \t]*$//'
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-25-2002 09:29 AM
тАО04-25-2002 09:29 AM
Re: Getting rid of extra spaces and tabs???
My choice is sed
# delete leading whitespace (spaces, tabs) from front of each line
# aligns all text flush left
cat file | sed 's/^[ \t]*//' >newfile
#mv newfile file
Then
# delete leading whitespace (spaces, tabs) from front of each line
# aligns all text flush left
cat file | sed 's/^[ \t]*//' > newfile
#mv newfile file
Then remove extraspace from middle
cat file | sed 's/\s+/\s/g' > newfile
#mv newfile file
Sachin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-25-2002 09:32 AM
тАО04-25-2002 09:32 AM
Re: Getting rid of extra spaces and tabs???
# delete trailing whitespace (spaces, tabs) from end of each line
#cat file sed 's/[ \t]*$//' > newfile
#mv newfile file
Sachin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-25-2002 10:09 AM
тАО04-25-2002 10:09 AM
Re: Getting rid of extra spaces and tabs???
Thanks again, Bob.