Live data from Hacker News

Useful sed scripts and patterns

github.com

91–100 of 124 posts

Re: Useful sed scripts and patterns

#92
post #29

Earlier quoted context omitted.

I'm mostly with you (except for your last sentence). I've picked up a lot of languages over the years, and can fall back into most of them pretty fast, but sed and awk especially are hard. On the occasions that they really are the best tool for the job, I get actual books out and set aside a window of a few hours to plod through it. I think the problem is that they fall into an uncanny valley between "simple tool" an…

If I measure time to solve a problem and not line count, using a language like Python or Ruby might be 2x the lines , for a line count approaching zero anyways, not sure that matters. Performance is comparable and I don't need to relearn anything and someone else who doesn't know awk/sed doesn't need to learn it. And when one is using awk/sed that script is often invoked from a shell, bash for instance. And since the…

>Performance is comparable

Can't find links now, but fairly certain were stories in past about how switching to AWK from other tools increased performance

Re: Useful sed scripts and patterns

#93
post #65
post #48

Earlier quoted context omitted.

I agree with the people here saying that it's better to just use your favourite scripting language unless your job is mostly about writing little bash scripts like that (maybe sysops people?)... Here's a groovy script to do the same: new File('file.txt').eachLine { println it.split()[0] } Not as neat as awk, but not much worse either... and as I use Groovy a lot for testing, and its syntax is simplified Java (which I…

Use the best tool for the job. sed is just a DSL (domain specific language) specifically designed for for manipulating text files line by line. Note that for matching strings even in Groovy/AWK/Python/Perl/... you'll probably use regexp which are another DSL. Would you recommend to use string functions instead?

> Would you recommend to use string functions instead?

Actually, yes, of course. Regex should be the last resort, really. It's unreadable, presents security issues depending on where it's used, and much of the time using string functions in a "proper" language will be actually easier unless you're a regex expert (which is a pretty silly thing to acquire expertise on).

Re: Useful sed scripts and patterns

#95
post #22

I really dislike sed, awk, and all these utilities because I just can’t remember how to use them. Every time I want to use one of these tools I have to relearn the stuff, and then after a while I forget how to use it. If I don’t use it all the time I just forget. I really don’t think non-interactive CLIs are a good way to do complicated tasks.

Which is why I've always stuck to `perl -pi -e`. It may not be as powerful but it's just one thing to learn. Also it understands regexs like '\d', etc which are kinda universal but for some reason don't work with many of these utils.

I also read once that perl was the most ubiquitous tool across all Unixen flavors, but I don't have a link to cite for it

Re: Useful sed scripts and patterns

#96
post #22

I really dislike sed, awk, and all these utilities because I just can’t remember how to use them. Every time I want to use one of these tools I have to relearn the stuff, and then after a while I forget how to use it. If I don’t use it all the time I just forget. I really don’t think non-interactive CLIs are a good way to do complicated tasks.

Which is why I've always stuck to `perl -pi -e`. It may not be as powerful but it's just one thing to learn. Also it understands regexs like '\d', etc which are kinda universal but for some reason don't work with many of these utils.

Perl is certainly more powerful than sed or awk :)

Re: Useful sed scripts and patterns

#97

I use sed -ibak ‘10d’ ~/.ssh/.known_hosts so many times a week. awk, sed, and regex really are the two programs I am happy to have learned well at my first job. They’ve helped me immensely throughout my 10 years as someone who bridges developer and ops roles.

For that one specifically, you may want to check your copy of "ssh-keygen" as it has recently(?) learned to manage those known_hosts entries without resorting to text file manipulation (`ssh-keygen -r something.example.com` for example)

Re: Useful sed scripts and patterns

#98
post #22

I really dislike sed, awk, and all these utilities because I just can’t remember how to use them. Every time I want to use one of these tools I have to relearn the stuff, and then after a while I forget how to use it. If I don’t use it all the time I just forget. I really don’t think non-interactive CLIs are a good way to do complicated tasks.

May I ask what is the alternative? Creating an entire application for a single use? designing a generalized case and making a monolithic application with lots of obscure options?

What about setting up the project for the application, setting up instrumentation and testing, debugging, etc.?

Even supposing that all that extra work makes sense for your unique string manipulation needs, what about the next time something like this comes up but is out of scope for your first tool? Do you end up with lots of little applications, with lots of little features and arguments? When you come across that similar-but-not-quite need down the road, do you have to scour your library of full blown apps to find something that fits? Not to mention maintaining them against future system updates...

OR - you could just force yourself to get over the hump, learn the conceptual basics at play and then when you come across a novel situation down the line you can just rattle off a tailored one-liner and have your answer in a few moments...

Re: Useful sed scripts and patterns

#99
post #22

I really dislike sed, awk, and all these utilities because I just can’t remember how to use them. Every time I want to use one of these tools I have to relearn the stuff, and then after a while I forget how to use it. If I don’t use it all the time I just forget. I really don’t think non-interactive CLIs are a good way to do complicated tasks.

Which is why I've always stuck to `perl -pi -e`. It may not be as powerful but it's just one thing to learn. Also it understands regexs like '\d', etc which are kinda universal but for some reason don't work with many of these utils.

I wouldn't call having the de-facto standard for an advanced regular expression system, a full programming language and the entirety of the CPAN module system easily pulled in and used less powerful.

I use sed for a lot of simple stuff, but I use Perl all the time for more advanced stuff. Perl is great for grabbing complex chunks out of a line and doing something with them that requires a bit of state. For example, writing a quick and dirty logfile parser to figure out how many requests and how many bytes were used by each IP over a time period (the time period limiting usually being a prior grep before being passed to Perl). I've probably taken 60 seconds to write out something to parse exactly what I want from a log hundreds of times.

Re: Useful sed scripts and patterns

#100
post #32

Earlier quoted context omitted.

I basically gave up on relearning them considering the number of times I’ve had to do it and the amount of effort involved to relearn them. Maybe it’s just me, but I just write python now.

You need to have a mental model for each tool and some idea of syntax associated with it. Once I learned about IFS,OFS, BEGIN, END and {}, as well as the fact that variables and their references have the same syntax, unlike in Perl... I could pick up coding in awk at any moment

Perl has BEGIN and END as well, and when using it with -n or -p for line processing mode uses -F to set word boundaries just like awk. In fact, it shared many flags and features with awk, which it was meant to be able to be used as a more powerful version of the same tool.
Post reply on HN