Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using sed, remove everything before the first occurence of a character

Tags:

unix

sed

Let's say I have a line looking like this

Hello my first name is =Bart and my second is =Homer

How can I do if I want to get everything after the first = or : using sed?

In this example, I would like to get the result

Bart and my second is =Homer

I am using sed 's/.*[=:]//' right now but I get Homer as result (everything after the last = or :) and I would like to get everything after the first, and not the last = or :

like image 326
Whin3 Avatar asked May 16 '17 13:05

Whin3


People also ask

What does sed '/ $/ D do?

env | sed '/^#/ d' | sed '/^$/ d' Concatenate FILE(s), or standard input, to standard output. With no FILE, or when FILE is -, read standard input.

What does \b mean in sed?

\b marks a word boundary, either beginning or end. Now consider \b' . This matches a word boundary followed by a ' . Since ' is not a word character, this means that the end of word must precede the ' to match. To use \b to match at beginnings of words, reverse the order: '\b .


1 Answers

Normally, quantifiers in sed are greedy, which is why you will always match the last =. What defines the first = is that all the characters before it are not =, so:

sed 's/^[^=]*=//'

Your question implies that either : or = are valid markers, in which case

sed 's/^[^=:]*[=:]//'
like image 115
Mad Physicist Avatar answered Sep 28 '22 02:09

Mad Physicist