14

My sed command is,

 sed '/(.*:)/d' <<< 'abcd:bcde:cdeaf'

It must return,

bcde:cdeaf

(i.e.) all characters before the first colon in the line and the colon itself must be removed.

But this is not removing anything.

My confusion arises mainly due to,

1) Does parens for pattern matching need to be escaped inside sed regex-es?

2) In either case(with escaping/no escpaing), it is nt working. I tried,

sed -E '/\\(.*:\\)/d' <<< 'abcd:bcde'
  • 1
    you want sed 's/[^:]*://'. And you're not deleting the input line, by the way, you're modifying it with a s///ubstitution command. You have to replace the first not colon bit and the colon that follows it with nothing at all. – mikeserv Sep 29 '15 at 2:29
  • that solves it... thanks, man... this is an example i took to learn regex pattern matching inside sed... so, i am looking for an answer that uses group/pattern match with parens... – Madhavan Sep 29 '15 at 2:31
  • 3
    Or, just using bash: printf "%s\n" "${line#*:}"... – jasonwryan Sep 29 '15 at 2:31
  • 1
    @jasonwryan - good point, considering the example source. it's definitely the more efficient way to handle it. but if it's a while read line that gets the $line, probably sed should be preferred. – mikeserv Sep 29 '15 at 2:33
20
$ echo 'abcd:bcde:cdeaf' | sed 's/^[^:]*://g'
bcde:cdeaf

The first ^ means beginning of the line. The [^:] is just the only way I know how to write not a colon. The * after the colon means any number of the things right before me (in this case the not-colon). Finally, the : selects the colon.

In other words, select the beginning of the line, any number of things that aren't a colon, and the first colon.

The //g means delete every matched instance.

  • 3
    you do not need to ^ anchor your match, except because you also add a global flag. there can only ever be one 1st occurrence of a pattern, and so the global flag doesn't remove all [^:]*: patterns from a line, as it would do if you didn't ^ anchor it. rather than complicating the regex with two unnecessary flags that only serve to unbalance one another you could just leave them out, which is what the edited version of this answer demonstrated before you rolled it back. why you would insist on disseminating bad information i dont know, but it makes this a bad answer. – mikeserv Sep 29 '15 at 2:55
  • @mikeserv, as I said already, thanks for pointing this out. I sincerely appreciate you helping me improve my sed skills. I am new to sed and am not yet comfortable straying from the very limited syntax I have picked up so far. That sed (heh), I think my answer solves OP's problem even though it is not the optimal (i.e., your) answer. This is Stack Exchange, not Wikipedia, so correct me if I am wrong but if you know of a better answer you should post it so people can see the variety of approaches and compare them. Please do not turn my answer into your answer with the edit function. – user1717828 Sep 29 '15 at 3:04
  • 4
    it wasn't my answer. it was your answer, edited. that's all. and it was good. it isn't anymore. – mikeserv Sep 29 '15 at 3:06
5

To operate with columns there is cut:

echo 'abcd:bcde:cdeaf' | cut -d: -f2-

same do

echo 'abcd:bcde:cdeaf' | cut -d: -f1 --complement

And other version with sed (more quick for big data):

echo 'abcd:bcde:cdeaf' | sed 's/^://;t;s/:/\n:/;D'

And rather exotic in bash

echo 'abcd:bcde:cdeaf' | { IFS=: read -r first last ; echo "$last" ; }

or

echo 'abcd:bcde:cdeaf' | { read -r line ; echo ${line#*:} ; }

or

echo 'abcd:bcde:cdeaf' | { IFS=: read -a a ; printf '%b:' "${a[@]:1}\c" ; echo ;}
  • You could also add the proper way to do it with sed, that is sed 's/[^:]*://' – don_crissti Feb 15 '17 at 23:44
  • @don_crissti The version is noted in the answer above. Additionally due to regexp use it is slower as have to compile expression in each line. – Costas Feb 16 '17 at 7:11
  • No, it's not. The answer above sucks big time and deserves lots of downvotes - especially if you read the revisions and the comments there. – don_crissti Feb 16 '17 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.