digdug
digdug

Reputation: 221

sed command - branching to label

I am not able to get the same output as the example given in the SED tutorial for branching below, http://www.grymoire.com/Unix/Sed.html#uh-59

Quoting the code here:

#!/bin/sh
sed '
:again
    s/([ ^I]*)//
    t again
'

The spaces are still in the brackets after this filter.

[UPDATE] Here is my output:

$echo "( ( test ) )" | sed '
> :again
>      s/([ ]*)//
>      t again
> '
( ( test ) )
$

Shouldn't that be ((test))? How do I get the script to delete the blank spaces in the nested parenthesis as demonstrated by the author?

[/UPDATE]

[UPDATE2]

$echo " ( ( ) ) " | sed '
> :again
> s/\([ ]*\)//
> t again
> '

Prompt is not back.

[/UPDATE2]

Also how do I enter the "^I" character? I think it is the horizontal tab, but I am not able to key in like other control characters via puTTY(for eg, to get "Enter", I type "Ctrl-V" followed by the "Enter" key, but this isn't working for tab). I tried with spaces only(using regex [ ]* instead of [ ^I]*), but this also failed to work.

Upvotes: 1

Views: 1772

Answers (2)

William Pursell
William Pursell

Reputation: 212268

( test ) does not match the regex ([ ]*). ([ ]*) only matches strings that contain nothing but spaces inside parens. Perhaps you are looking for ([ ]* to remove leading spaces inside and [ ]*) to remove trailing spaces.

Upvotes: 2

shellter
shellter

Reputation: 37288

Bully for you to work thru some tutorials.

Assuming you're using vi or vim all you need to do to include a tab char inside the [ .. ] grouping, is to type the tab key. ( I use putty all the time, and if pressing tab char doesn't "insert" a tab char into document/command-line, then you have a putty configuration problem ).

The ^I is from the vi list mode. List mode is handy to see where are line-feed chars (\n) will show as the reg-exp char $ (which in reg-ex is an "end-of-line anchor", the other being ^ char (beginning of line)).

So turning on vi list mode, with :li and you'll see all tab chars expanded as ^I and all end of lines as $

As you say

How do I get the script to delete the blank spaces in the nested parenthesis as demonstrated

That is slightly ambiguous, as newer seds use plain parens as grouping chars to create replacement group like \1 for the replacement-side of the s/pat/repl/ substitute cmd.

Given that your example has no numbered-replacement value in the replacement-side, I'll assume that the purpose is the remove a literal () pair AND that it should work as indicated. Once you :set list, add a tab-char inside the [ ... ], it should work. If not, please edit your question with any error messages that might appear.

I hope this helps.

Upvotes: 2

Related Questions