LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-22-2012, 01:30 PM   #1
uncle-c
Member
 
Registered: Oct 2006
Location: The Ether
Distribution: Ubuntu 16.04.7 LTS, Kali, MX Linux with i3WM
Posts: 299

Rep: Reputation: 30
sed regex and removing 'whitespace'


Was just reading the classic 'Sed One Liners' and I came up with this problem.

Code:
 $ cat file
one
  two
    three
 $
Could someone explain why
Code:
 $ sed 's/^[ \t]*//' file
removes any leading tabs & white spaces whereas

Code:
  sed 's/^[ \t]+//' file
does not ? What is the subtle difference between the two which causes only the former to remove leading white spaces from each line ?

Last edited by uncle-c; 04-23-2012 at 04:55 AM.
 
Old 04-22-2012, 01:42 PM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Because the '+' is matching literally. You need

Code:
sed 's/^[ \t]\+//' file
 
1 members found this post helpful.
Old 04-22-2012, 10:58 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
To be more specific, it's the difference between basic and extended regular expressions. grep and sed use basic regex by default, and most of the more advanced regex devices like '+' are not supported.

But gnu grep and sed also offer extended regex, which allows you to "activate" the special meanings of the characters by backslashing them. Perhaps a better way to do it, however, is to enable them globally with the use of "grep -E" and "sed -r". Then the behavior becomes reversed; the special meanings are enabled by default, and backslash escaping them makes them literal.

Code:
sed -r 's/^[ \t]+//' file
The grep man page goes into good detail about basic vs. extended regex.

Incidentally, if all you want to do is remove all instances of (a) certain character(s), you'll get better performance with tr.

Code:
tr -d '[ \t]' <file
 
1 members found this post helpful.
Old 04-23-2012, 03:27 AM   #4
uncle-c
Member
 
Registered: Oct 2006
Location: The Ether
Distribution: Ubuntu 16.04.7 LTS, Kali, MX Linux with i3WM
Posts: 299

Original Poster
Rep: Reputation: 30
Cheers guys. I had been using tr but knew that there was a method using sed. It was only when I read the Sed One Liners page that the '+' problem got me thinking. Could you somehow use a white space character class - [:space:] instead of ' [\t] ' to achieve the same result ?
 
Old 04-23-2012, 03:52 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by uncle-c View Post
Could you somehow use a white space character class - [:space:] instead of ' [\t] ' to achieve the same result ?
Yes, but it is available using the extended regexp as well:
Code:
sed -r 's/^[[:space:]]+//' file
 
1 members found this post helpful.
Old 04-23-2012, 08:25 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Note that the [:space:] character class covers several other characters as well; the full list being tab, newline, vertical tab, form feed, carriage return, and space. There's also [:blank:] which contains only the regular space and tab characters, and so is exactly equivalent to the above.

The grep info page is one place you'll find definitions for what the various classes cover.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed whitespace substitution problem. arashi256 Linux - Newbie 18 07-17-2009 11:03 AM
sed, replacing underscore with whitespace fjkum Programming 3 10-31-2007 12:09 AM
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 05:10 AM
BASH: Removing ALL whitespace from variable eur0dad Programming 1 09-07-2006 10:25 AM
Whitespace parsing sed? carl.waldbieser Programming 1 12-12-2005 04:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration