4

How do I remove the preceding spaces before 809 in the output of my echo??

here is my sample:

$ echo '   809 23/Dec/2008:19:20'
   809 23/Dec/2008:19:20
^^^3 spaces here preceding the 809    

I can remove 3 or 4 spaces using sed:

        ...4 spaces here   
$ echo '    809 23/Dec/2008:19:20' | sed 's/^.\{3\,4\}//'
809 23/Dec/2008:19:20
0 spaces here preceding the 809    

But what I want is my sed command to work on anything greater than 3

        .....5 spaces here
$ echo '     809 23/Dec/2008:19:20' | sed 's/^.\{3\,4\}//'
 809 23/Dec/2008:19:20
^1 spaces here preceding the 809    

Ho do I write my regex in the sed to remove 3 or more spaces preceding 809?

4
  • 1
    Use sed 's/^[[:blank:]]\{3,\}//' if you want to remove 3 or more spaces at the start. Commented Feb 22, 2018 at 23:06
  • Maybe ^\s{3,} Commented Feb 22, 2018 at 23:07
  • do you have to use sed? xargs chops off white spaces automatically. Commented Feb 22, 2018 at 23:10
  • Why are you using . in the regexp? That matches any character, not just spaces. Commented Feb 22, 2018 at 23:13

4 Answers 4

6

If you want to remove 3 or more spaces at the start use

sed 's/^[[:blank:]]\{3,\}//'

Details

  • ^ - start of input
  • [[:blank:]]\{3,\} - 3 or more consecutive occurrences of any horizontal whitespace.

If you need to remove 3+ spaces at the start before a specific value, say, 809, just add that value to the regex and replacement pattern:

sed 's/^[[:blank:]]\{3,\}809/809/'

or use a capturing group and placeholder:

sed 's/^[[:blank:]]\{3,\}\(809\)/\1/'

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

3

xargs frees you from thinking about regular expressions.

$ echo '   809 23/Dec/2008:19:20' | xargs
809 23/Dec/2008:19:20

It's a more or less unconventional trick of xargs. This tool is meant to be used to build up commands with long argument lists. From man page:

xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command

However, it a part of the preprocess that white space will be trimmed, before sending to subsequent command. In this case, there isn't subsequent command, and therefore xargs just send the argument as it is, modulo with the spaces trimmed, to standard output, which does exactly what you are asking for.

2 Comments

good to know echo ' 809 23/Dec/2008:19:20' | xargs 809 23/Dec/2008:19:20 can you briefly explain xargs?
@HattrickNZ I added a few more words explaining why it would work.
1

You can specify how many characters you want to for using {minimum,maximum}. If you leave maximum blank it will match anything with at least the minimum amount of characters. So selecting three spaces would be “\{3,\}”. Good luck!

Comments

0

Don't escape comma:

$ echo '             809 23/Dec/2008:19:20' | sed -s 's/^ \{3,\}//'
809 23/Dec/2008:19:20

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.