this post was submitted on 09 Apr 2024
296 points (98.7% liked)

Linux

48372 readers
1352 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 5 years ago
MODERATORS
 

I thought I'll make this thread for all of you out there who have questions but are afraid to ask them. This is your chance!

I'll try my best to answer any questions here, but I hope others in the community will contribute too!

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 15 points 7 months ago (5 children)

Maybe not a super beginner question, but what do awk and sed do and how do I use them?

[–] mumblerfish 15 points 7 months ago (1 children)

This is 80% of my usage of awk and sed:

"ugh, I need the 4th column of this print out": command | awk '{print $4}'

Useful for getting pids out of a ps command you applied a bunch of greps to.

”hm, if I change all 'this' to 'that' in the print out, I get what I want": command | sed "s/this/that/g"

Useful for a lot of things, like "I need to change the urls in this to that" or whatever.

Basically the rest I have to look up.

[–] Nibodhika 7 points 7 months ago (1 children)

I say that covers around 99% of the awk/sed I use.

[–] mumblerfish 1 points 7 months ago

I was gonna write 99%, but then I remember I also need capture groups quite often. That would make 99% I'd say

[–] [email protected] 8 points 7 months ago

Awk lets you do operations based on patterns. You can make little scripts and mini programs with it.

Sed lets you edit streams.

Almost everything can be treated like a stream so with those two tools you have the power to do damn near everything ever.

There’s a book called sed & awk that’s real good.

[–] [email protected] 4 points 7 months ago* (last edited 7 months ago)

If you’re gonna dive into sed and awk, I’d also highly recommend learning at least the basics of regular expressions. The book Mastering Regular Expressions has been tremendously helpful for me.

Edit: a letter. Stupid autocorrect.

[–] [email protected] 4 points 7 months ago* (last edited 7 months ago)

Awk is a programming language designed for reading files line by line. It finds lines by a pattern and then runs an action on that line if the pattern matches. You can easily write a 1-line program on the command line and ask Awk to run that 1-line program on a file. Here is a program to count the number of "comment" lines in a script:

awk 'BEGIN{comment_count=0;} /^[[:space:]]*[#]/{comment_count++;} END{print(comment_count);}' file.sh

It is a good way to inspect the content of files, espcially log files or CSV files. But Awk can do some fairly complex file editing operations as well, like collating multiple files. It is a complete programming language.

Sed works similar to Awk, but it is much simplified, and designed mostly around CLI usage. The pattern language is similar to Awk, but the commands are usually just one or two letters representing actions like "print the line" or "copy the line to the in-memory buffer" or "dump the in-memory buffer to output."

[–] [email protected] 2 points 7 months ago* (last edited 7 months ago)

Probably a bit narrow, but my usecases:

  • awk: modify STDIN before it goes to STDOUT. Example: only print the 3rd word for each line
  • sed: run a regex on every line.