Using sed, regular expression not working

is this the right way to print lines that start with 2015?

sed '/^2015/p' myfile.txt
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi Rudy,

That sed command doesn't work for me, so I guess the answer is "no".  If you were wanting more than that, read on...

The problem is, sed prints all lines (not just those which match) by default.  To suppress this, you can do things like:
    sed -n '/^2015/p' myfile.txt
    sed '/^2015/!d' myfile.txt     # This 'd'eletes from the output stream, rows which don't (!) match

Or use grep:
    grep ^2015 myfile.txt

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dan CraciunIT ConsultantCommented:
+1 for grep.
sed is not the right tool for this.
I would have used grep for this job too, Dan, but mainly because grep is designed specifically for this kind of task, the syntax is simpler, and I didn't even know how to do this in sed until I looked it up.  However, I don't know about how the performance of each generally compares, but I notice that the sed executable is a lot smaller than the grep executable, so I guess sed would be minutely faster to load into RAM if it wasn't already in cache, but such things usually aren't significant.

How about you, Dan?  Why do you say sed is not the right tool for this?  It's generally good to give at least one reason for your preference, so that the reader gets to understand why you're saying what you're saying, and that it's not just a baseless opinion.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Dan CraciunIT ConsultantCommented:
sed is designed to modify text files.
grep is designed to search in text files.

You're searching for something...

You could also use perl, python, C or anything that is capable of opening a file and read the contents. Does not mean you should use those, unless it's as an exercise.
Dan CraciunIT ConsultantCommented:
Here is a speed test to illustrate the difference while searching:
Gerwin Jansen, EE MVETopic Advisor Commented:
You can do this just fine with sed, like this:
sed -n '/^2015/p' myfile.txt

Open in new window

So you just add -n to your command.
rudy201Author Commented:
Thanks!  I wanted to know how to do something like this with sed before I could learn more complicated stuff with sed
Hi Dan,
Now you're talkin'.

Hi Gerwin,
Did you check what solutions had already been provided before you posted that?  I already provided that solution.  Scroll up.

Hi Rudy,
Thanks for the points!
I personally haven't bothered to go very far with learning sed, because Perl is a lot more flexible, so you might like to consider learning Perl if you want flexibility.  awk is another alternative, but Perl is more flexible than awk, too.
Gerwin Jansen, EE MVETopic Advisor Commented:
@tel2 - I don't see your issue, you've got the points ;) Dan is giving a +1 for grep and saying sed can't be used where it can be used just fine here. I'm just acknowledging that sed is usable and summarizing that just adding -n is sufficient.
Dan CraciunIT ConsultantCommented:
@Gerwin: when did I say that it can't be used??? I said it should not be used for a simple search.
To quote myself "sed is not the right tool for this".

You could use a screwdriver to hit a nail, but that does not mean you should. Get a hammer.
Hi Gerwin,
"@tel2 - I don't see your issue, you've got the points ;)"
I'm well aware of that, which is why I thanked Rudy for the points.  I'm not complaining about points.

"Dan is giving a +1 for grep and saying sed can't be used where it can be used just fine here."
That sounds like a straw man argument to me, because he didn't exactly say "sed can't be used", but "sed is not the right tool for this", and he explained what he meant by that after I asked him to, and gave a link with performance timings.

"I'm just acknowledging that sed is usable and summarizing that just adding -n is sufficient."
So you effectively repeated the 1st of my 3 solutions?
Q1. How does repeating a solution add value to what has already been posted?
And here's my previous question for you again:
Q2.  "Did you check what solutions had already been provided before you posted yours?"
Please answer Q1 & Q2.  Hopefully that should make things clearer.
Gerwin Jansen, EE MVETopic Advisor Commented:
Ok guys, let's stop quoting each other :P The question was about sed (see title) and was solved with sed. Yes, there are always better ways to do things, agreed, I would have used grep myself here as well ;)
Hi Gerwin,

Yes, the question was about sed, but it was solved with 3 options, 2 of which used sed.  There was no indication in the original post that the asker was aware that there were better tools (like grep) but didn't want to hear about them, so I suggested grep as an alternative, and Dan backed that up and later explained why.

The quoting above has been used to:
a) Clearly show which parts of your post I was responding to.
b) Clear up the issue of Dan being misquoted by you.
And I don't see what's wrong with either of those.
Q3. So, why are you asking us to stop quoting each other?  (Please be specific, preferably with examples.)

Q4. Why are you avoiding answering my clearly labelled questions (i.e. Q1 & Q2)?  I've already asked Q2 twice.

Please answer my questions Q1, Q2, Q3 & Q4.  And please don't just give response which doesn't answer the questions again.

Gerwin Jansen, EE MVETopic Advisor Commented:
tel2 - I'm happy to answer your questions (Q4), I'm not just repeating what you said, I did check and I'm supporting one solution by highlighting that just the -n would have been enough (answer to Q1 and Q2). Q3: see the smiley? As the question has been solved already, discussion now is off topic so that's why I'm suggesting to stop quoting, it's of no use to the asker. If you want to discuss further, you can always send me a message. Thanks.
Dan CraciunIT ConsultantCommented:
@tel2: probably Gerwin did not bother reading your solution, saw the sed line with no -n so he responded too.

The rest is just noise.
Hi Gerwin,

Thanks for answering my questions.

Yes, I saw the smiley.  It didn't answer my question though, but now you have.

Regarding not repeating what I said, while you're technically right, the code you posted is identical to mine, but you made no reference to my solution, despite the fact that you had already checked my solutions.

If you wanted to support my solution and highlight the '-n' switch, then I would have hoped you would say something like:
  "As you can see by tel2's first solution, the only change you need to make to your one-liner is to include the '-n' switch, which suppresses printing by default".
However, since those points were either already stated or should have been obvious by comparing the 2 short one-liners (the asker's and mine), I don't see what value it would add.

I wasn't quoting for the benefit of the asker.  I was quoting for the purpose of clear communication, as I have previously indicated.

You're right - we are off the original topic now, but if you were serious about going private with this, then you could have lead the way and sent your last post to me privately, since it was also off the original topic.  If you want to discuss further, feel free to send me a private message.  But I've got nothing to hide in this matter, so I don't mind if you continue to post here, if you have anything more to say.  I posted this here because I don't mind anyone seeing it, including Dan and mods, and I think it's good for accountability.

The reason I spent my time on all this is not just for this question.  I'm hoping you will bear these things in mind when you post future solutions, if you think there's any merit in what I've said.

Here ends today's sermon.  8)

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.