0

I need a way to slice everything after an email appears and before certain text appears.

Example formats are shown here:

[email protected]:0:3rw3e:weofkew:StackOverflow=
[email protected]:19.2132.1:StackOverflow=

Format needed would be:

[email protected]:StackOverflow= (followded by everything else on the line).

So essentially slicing after an email appears, and before StackOverflow= appears.

Notes: All emails are unique, it's not literally [email protected]

3
  • 1
    Could you please include examples in your question? Commented Aug 21, 2018 at 0:44
  • I did in the hastebin below, I assumed posting examples here would make the post untidy. Commented Aug 21, 2018 at 0:59
  • 3
    The preference is for self-contained questions and answers, in case the external link goes away. Commented Aug 21, 2018 at 1:24

1 Answer 1

0
sed -r 's/(@[^:]+:).*(StackOverflow=)/\1\2/' input.txt

Explanation

  • sed -r 's/foo/bar/' input.txt: use sed with extended regular expressions, replacing pattern foo with bar, for file input.txt.
  • (@[^:]+:).*(StackOverflow=): match from the @ in the email address, which is followed by 1 or more non-: characters ([^:]+). This is then followed by a :. Capture this all in the first capturing groups with (). This is followed by a number of characters .*, then StackOverflow=, which we also capture in a group ().
  • /1/2: replace this expression with the partial email address (the first capturing group above), skip the part in between, then followed by the second capturing group.

N.B. this makes a lot of assumptions about the format of the input file, e.g. the format of the email address, the number of @ and StackOverflow= per line, etc.

2
  • I appreciate the time you've spent on this, thankyou so much. Commented Aug 21, 2018 at 3:50
  • Glad I could help @StackStackAndStack. If this answers your question, you can click the tick on the left of the answer (and upvote with the up arrow if you feel that is appropriate). Welcome to U/L! Commented Aug 21, 2018 at 3:52

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.