- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: delete leading blanks in a file
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
Forums
Discussions
Discussions
Discussions
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
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
01-20-2003 06:06 AM
01-20-2003 06:06 AM
This site ignores leading blanks so I substitute all blanks with "B" in my example:
#cat file
BBline1
line2
Bline3
BBBBBline4BwithBblank
#
should lead to
#cat file2
line1
line2
line3
line4BwithBblank
#
I found a solution with sed that deletes me only one leading blank :
sed -e 's/^ //g'
but there may be 20 leading blanks in a line...
TIA Clemens
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2003 06:11 AM
01-20-2003 06:11 AM
Re: delete leading blanks in a file
# sed -e 's/^[ \t]*//' filename
Note: Type a TAB character in lieu of \t. Some versions of 'sed' simply don't recognize the '\t'.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2003 06:22 AM
01-20-2003 06:22 AM
Re: delete leading blanks in a file
you can also try this:
sed -e 's/^[: :]*//'
This should remove all white space (tab or spaces) from the beginning of the line.
Regards,
Jochen
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2003 06:23 AM
01-20-2003 06:23 AM
Re: delete leading blanks in a file
[: and :] ...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2003 06:58 AM
01-20-2003 06:58 AM
Re: delete leading blanks in a file
it's great! It works.
But can you explain why?
Clemens
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2003 07:17 AM
01-20-2003 07:17 AM
Re: delete leading blanks in a file
it goes somehow like this:
^ --> beginning of line (as you probably already knew)
[\t]* --> followed by zero or more characters like \t (although I could not find an explanation for \t or tab in 'man 5 regexp' it probably means the same as [:SPACE:] whitespace -- space or tab)
Regards,
Jochen
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2003 07:30 AM
01-20-2003 07:30 AM
Re: delete leading blanks in a file
The perl version might be easier to read:
# perl -pe 's/^\s+//' infile >outfile
-p = print every line
-e = using the following expression
s/// = substitute in the current line
^ = start of line
\s = white space (blanks, tabs, spaces)
+ = one or more
Enjoy, have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2003 07:55 AM
01-20-2003 07:55 AM
Re: delete leading blanks in a file
To remove any space:
#cat
To remove tabs:
#cat
To remove both of them once:
#cat
Rgds.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2003 08:46 AM
01-20-2003 08:46 AM
Re: delete leading blanks in a file
You can do:
sed -e 's/\ \{1,\}/ /g' file
This change one or more blank by one.
Regards,
Justo.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2003 11:01 AM
01-20-2003 11:01 AM
Re: delete leading blanks in a file
I'll try to resume:
Jochen, your first answer
sed -e 's/^[: :]*//' doesn't
work although I recognized the blank.
The explanation of \t is what I don't understand. I knew \t only for a tab. But here I works only for a blank -- that's what I want!
[:SPACE:] or [:BLANK:] doesn't work.
Procura, your perl is easy to understand but I never used perl before... Now I think about it!
Thanks to Jose Maria and Justo. You didn't recognize that I only want to process leading blanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2003 10:55 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2003 02:38 AM
01-21-2003 02:38 AM
Re: delete leading blanks in a file
that's it! It works just like James' 's/^[ \t]*//' but it seems to be logical.
Thanks
Clemens
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2003 03:20 AM
01-21-2003 03:20 AM
Re: delete leading blanks in a file
-e - script
s/^ *// - s/regular expression/replacement/flags
BR,
Jannik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2003 12:59 PM
01-21-2003 12:59 PM
Re: delete leading blanks in a file
...Laurie :{)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2003 01:45 PM
01-21-2003 01:45 PM
Re: delete leading blanks in a file
An easier read and understood variation of my original post is this:
# sed -e 's/^[[:space:]]*//' filename
The [:space:] character class or characters which produce white space (blanks, tabs) in displayed text.
I choose to match *either* spaces or tabs, or a mixture in any combination, in order to provide the most general match. Had you originally used only space characters between the fields of your file, but then used 'unexpand' to change spaces to tabs, a regular expression consisting of only the space character would not have achieved your goal.
Your original 'sed' substitution failed to consider anything other than blank (space) characters (i.e. not TABS), More importantly, your regular expression (RE) consisted only of *one* character. By adding the asterisk following the character class we constructed a RE that matched zero or more occurrences of the characters found in the character class that preceded the asterisk. Thus, the matching string was represented by the maximum number of characters which defined the RE.
Regards!
...JRF...