Need Quick help on Understanding sed Regex


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need Quick help on Understanding sed Regex
# 1  
Old 11-24-2014
Need Quick help on Understanding sed Regex

Hi Guys,

Could you please kindly explain what exactly the below SED command will do ?

I am quite confused and i assumed that,
Code:
sed 's/[ \t]*$/ /'

1. It will remove tab and extra spaces .. with single space.

The issue is if it is removing tab then it should be Î right ..
please assist.


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data. Thanks

Last edited by vbe; 11-24-2014 at 09:55 AM..
# 2  
Old 11-24-2014
it looks like it's *trying* to remove tabs and spaces, with a single space.
the items in square brackets are the list of matches it'll look for, the "*" after the square bracket says "0 or more of these". the "$" is end of line.

Rest is pretty self explanatory (I hope) Smilie

Honestly, though, I've never had much luck using sed for replacing control characters, I've had much better luck using the "tr" command.

ie:
Code:
echo "blah#blah" | tr -s "#" "\t" | tr -s "\t" "-"

(since I can't display a tab properly, I'll do a 2 step, convert to a tab, then from .. obviously you'd just use one or the other in practice Smilie )
# 3  
Old 11-24-2014
It will remove trailing whitespace with a single space, but only if it is GNU sed. Regular sed does not understand \t, so then it will remove trailing spaces, backslashes and t's..

Regular sed
Code:
$ printf "%s\n" "Just wait      " | sed 's/[ \t]*$/ /'
Just wai

GNU sed:
Code:
$ printf "%s\n" "Just wait      " | sed 's/[ \t]*$/ /'
Just wait

Regular sed takes a hard TAB character (you can enter it with CTRL-V TAB)
Code:
printf "%s\t\t\n" "Just wait      " | sed 's/[       ]*$/ X/'
Just wait X

In the square brackets are a space and a TAB-character. This is also understood by GNU sed, so for portability use that instead of \t

Last edited by Scrutinizer; 11-24-2014 at 01:14 PM..
# 4  
Old 11-24-2014
I interpret the sed command as follows:
  1. If the line is a blank line, it will substitute any number of spaces and tabs with a single space
  2. If the line contains some text, it will substitute any number of trailing spaces and tabs with a single space

---------- Post updated at 07:33 PM ---------- Previous update was at 07:32 PM ----------

Oh, too late, again Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Regarding a GREAT site for understanding and Visualizing regex patterns.

Hello All, While googling on regex I came across a site named Regulex Regulex:JavaScript Regular Expression Visualizer I have written a simple regex ^(a|b|c)(*)@(.*) and could see its visualization; one could export it too, following is the screen shot. ... (3 Replies)
Discussion started by: RavinderSingh13
3 Replies

2. UNIX for Beginners Questions & Answers

Help with understanding this regex in a Perl script parsing a 'complex' string

Hi, I need some guidance with understanding this Perl script below. I am not the author of the script and the author has not leave any documentation. I supposed it is meant to be 'easy' if you're a Perl or regex guru. I am having problem understanding what regex to use :confused: The script does... (3 Replies)
Discussion started by: newbie_01
3 Replies

3. Shell Programming and Scripting

Need help understanding this Regex.

Hi everyone, This regex looks simple and yet it doesn't make sense how it's manipulating the output. ifconfig -a eth0 Link encap:Ethernet HWaddr 00:0c:49:c2:35:6v inet addr:192.16.1.1 Bcast:192.168.226.255 Mask:255.255.255.0 inet6 addr:... (2 Replies)
Discussion started by: xcod3r
2 Replies

4. Shell Programming and Scripting

Understanding regex behaviour when using quantifiers

# echo "Teest string" | sed 's/e*/=>replaced=</' =>replaced<=Teest string So, in the above code , sed replaces at the start. does that mean sed using the pattern e* settles to zero occurence ? Why sed was not able to replace Teest string. # echo "Teest string" | sed 's/e*//g' Tst string ... (6 Replies)
Discussion started by: chidori
6 Replies

5. Shell Programming and Scripting

help understanding regex with grep & sed

I have the following line of code that works wonders. I just don't completely understand it as I am just starting to learn regex. Can you help me understand exactly what is happening here? find . -type f | grep -v '^\.$' | sed 's!\.\/!!' (4 Replies)
Discussion started by: trogdortheburni
4 Replies

6. Shell Programming and Scripting

Understanding a regex

Hi, Please help me to understand the bold segments in the below regex. Both are of same type whose meaning I am looking for. find . \( -iregex './\{6,10\}./src' \) -type d -maxdepth 2 Output: ./20111210.0/src In continuation to above: sed -e 's|./\(*.\{1,3\}\).*|\1|g' Output: ... (4 Replies)
Discussion started by: vibhor_agarwali
4 Replies

7. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

8. Shell Programming and Scripting

Perl: Any quick way to use regex on hash keys?

Hi, Is there any quick way to use pull out keys that match a specific regex pattern? eg %hash ; $hash(123,456) = xxx; $hash(123,457) = xxx; $hash(123,458) = xxx; $hash(223,459) = xxx; I need a fast way to get all the keys that start with 123.. Meaning I should get ... (5 Replies)
Discussion started by: Leion
5 Replies

9. Shell Programming and Scripting

Quick regex question about alphabetic string

Hi guys, Pretty new to regex, and i know im doing something wrong here. I'm trying to get a regex command that restricts a string to be 8 characters long, and the first character cannot be 0. Here's what i have so far... echo "01234" | grep "^{8}*$" Thanks very much! -Crawf ... (7 Replies)
Discussion started by: crawf
7 Replies

10. Shell Programming and Scripting

Quick regex question

Say that I want to match any of the following: abc def ghi The letters will either be "abc", "def", or "ghi", only those three patterns. The numbers will vary, but there will only be numbers between the brackets. I've only been able to match abc, using the following: abc.*. I'm... (1 Reply)
Discussion started by: retrovertigo
1 Replies
Login or Register to Ask a Question