Using `sed` and Regular Expressions (Unix/Linux command line)

แชร์
ฝัง

ความคิดเห็น • 168

  • @annexedolphin2238
    @annexedolphin2238 5 ปีที่แล้ว +183

    This command would still print blank lines with spaces so use '^\s*$' for maximum autism

    • @diogofelix8626
      @diogofelix8626 5 ปีที่แล้ว +3

      '^\s{1,}?$' would be better
      edit: actually just * matches 0 or more times, so, not better (but maybe more expressive)

    • @HousedHorse
      @HousedHorse 5 ปีที่แล้ว +3

      came to the comments for this

    • @gunrugger
      @gunrugger 5 ปีที่แล้ว +2

      Doesn't catch tab characters.

    • @HadToChangeMyName_YoutubeSucks
      @HadToChangeMyName_YoutubeSucks 5 ปีที่แล้ว +5

      @@diogofelix8626 -- Not better but certainly more OCD. I myself was looking for the use '^\s*$' post so I wouldn't be repeating someone...hate it when I do that.
      That said, you should use use '^\s*$' (hate it when I do that, but I repeat myself again). I'm currently working on code someone else wrote, for some ridiculous OCD reason he put a blank line after every single line in his code and it was driving me crazy so I wrote a script that ran sed against every file and took them out, better no blank lines at all than only half the code that would fit on my screen, at least that's what my OCD tells me. Strangely considering this annoying habit he seemed to also insist on conciseness like foreach($x in $y) while (blah) if (j) dosomething; all in a single line with no braces instead of in tabbed blocks. One annoying extreme to the exact opposite annoying extreme. He also wrote all his mysql statements inline with no returns (didn't use views at all) so I have to deal with 4000 character long sql select statements in one line (inside the call to mysql, not even a separate variable). It's like reading War and Peace with no hard returns and worse if you're trying to debug it. Plus it's almost always with nested queries and no views, like he was still working with mysql 4 (he was actually on 5.2). And the inserts/updates are worse, all built by concatenating , x=$y onto a sql string variable into one huge insert/update statement, and not even a prepared statement, just one long string that has to have quotes in all the right places. And on top of that not only do the forms have inputs with names completely different than the table column names, they're often retrieved into local variables that have completely different names that also don't match the table columns before this concatenation occurs. His tables are like spreadsheets so I have tables with 160 columns with the vast majority unused and half of them also in a related table but every single one goes into the update and insert statements because even if there's no change he saves it, so I have multiple tables with the same data but each have different values depending on what form it's saved from. I spend a lot of time cursing.
      Well that went longer than necessary and completely left the subject at hand, just had to get that off my chest.

    • @GottZ
      @GottZ 5 ปีที่แล้ว +2

      @diogofelix \+ does the same as {1,}
      if you think more characters in your expression make it more expressive you clearly forgot every character has meaning. by adding more it becomes less obvious

  • @Argletrough
    @Argletrough 5 ปีที่แล้ว +58

    7:07 Vim muscle memory detected.

    • @sanjacobs6261
      @sanjacobs6261 3 ปีที่แล้ว

      Hahah, I think he has vi mode in his terminal, so he probably just forgot to hit Esc before k

  • @mkd1113
    @mkd1113 5 ปีที่แล้ว +164

    Luke: "You're not autistic, you're normal"
    Also Luke: Makes torrent status bar module that utilizes sed when you could just open your torrent client instead.

    • @mattk6343
      @mattk6343 4 ปีที่แล้ว +3

      I liked the idea of sorting those text fields by sed and sort at 13:50, that was ingenious. (but, I'm quite green at scripting)

    • @nekoill
      @nekoill 3 ปีที่แล้ว +13

      Why would you decide to do something in a way that has already been tried and proven to work? It's not gonna be the art of solving problems of your own making, and that's cringe bro.

    • @toxic_narcissist
      @toxic_narcissist 3 ปีที่แล้ว +9

      yep, because opening the torrent client just to see torrent statistics is definitely not autistic

    • @Its_Pritam_
      @Its_Pritam_ 3 ปีที่แล้ว +5

      Man is just flexing his skills. And that was actually useful!

    • @claudiotorres9138
      @claudiotorres9138 ปีที่แล้ว

      @O B to sort them

  • @auslegungssache
    @auslegungssache 5 ปีที่แล้ว +13

    The name comes from the program ed, as in editor. Ed uses special regex for editing. Sed is the same as ed but it ONLY uses regex for editing. Vi, the text editor, is built ON TOP of ed so you can use all the commands from sed in vi. Vim is BASED ON the idea of vi and all the commands work there too.
    Then there's acme/sam and vis. Regex is a big part of acme and sam because you have to use the mouse otherwise. It uses special regex called plan9 regex. Vis is basically a cut down version of vim that uses the plan9 regex (which is superior). I recommend trying to write scripts in ed instead of using awk, if you learn the sed syntax!

    • @greyshopleskin2315
      @greyshopleskin2315 7 หลายเดือนก่อน

      I know only a little bit of ed. But is is as powerful for text manipulation as awk?
      It would be nice if you have some ed script example.

  • @MrHamsterbacke756
    @MrHamsterbacke756 5 ปีที่แล้ว +171

    Please make more videos like this.

    • @rodrigorodriguez509
      @rodrigorodriguez509 3 ปีที่แล้ว

      You could have learned this on your own anytime through innumerable sources, and the same is true for any other video he would do.

    • @leonardotag
      @leonardotag 3 ปีที่แล้ว +4

      @@rodrigorodriguez509 The thing is that we often don't know these features even exist, but once we are aware of them we can learn further from other sources.

  • @michalvlasak
    @michalvlasak 5 ปีที่แล้ว +30

    "sed 11q" actually prints the 11th line. To quit before the 11th line gets printed you would need to use GNU extension 'Q' ( "sed 11Q" ), or actually use "sed 10q". ( try "seq 20 | sed 10q" vs "seq 20 | sed 11Q" ).

  • @cigimigi87
    @cigimigi87 5 ปีที่แล้ว +34

    It' amazing how he organize and sort everything. Because of him, my i3 looks beautiful. Thank you Luke for videos like this... 😁

  • @jamesfraser7394
    @jamesfraser7394 5 หลายเดือนก่อน

    This content will never get old. All LINUX newbies should see this to begin exploration of SED, regular expressions and AWK.

  • @thescrawl6594
    @thescrawl6594 5 ปีที่แล้ว +13

    This is very helpful, I appreciate this sort of content. I've been on linux for a few months now, but I struggle with scripts because I dont understand basic stuff like this.

  • @juliotencio5695
    @juliotencio5695 4 ปีที่แล้ว +6

    Thank you Luke, i'm actually studying for the LFCS certification and videos like this one are a great resource !!!

  • @tristanbellingham6759
    @tristanbellingham6759 5 ปีที่แล้ว +45

    You might not have to download 500mb of python modules to make a torrent statusbar thing but you do need to download 500mb of fonts to find one with the emojis in it

    • @GottZ
      @GottZ 5 ปีที่แล้ว

      the struggle is real

    • @thingsiplay
      @thingsiplay 3 ปีที่แล้ว +4

      Decide yourself which task is easier to do.

  • @timhilt1185
    @timhilt1185 5 ปีที่แล้ว +6

    I would be interested in videos about grep/pgrep and awk too! These small videos just show the peak of the iceberg of what's possible with these utilities but you showcasing a real-world-example gave me an idea what i could use sed for; i guess it got me started and woke my interest! Having similar videos for other useful utilities would be really nice!

  • @mosukiton
    @mosukiton 5 ปีที่แล้ว +2

    This is extremely helpful. I didn't realise that a lot of these sed commands are the same in vim, this greatly reduced the learning curve for me

  • @anastaziuskaejatidarjan4711
    @anastaziuskaejatidarjan4711 4 ปีที่แล้ว +5

    The syntax you used for providing multiple expressions to sed ( sed 'expression;anotherexpression' ) works with GNU, but it's non-standard. Not every implementation of sed can handle that. The standard way afaik is to do sed -e 'expression' -e 'anotherexpression'

  • @michelle732mn
    @michelle732mn 4 ปีที่แล้ว +2

    Thank you so so much! Your skills show through when explaining. Einstein said something like, If you can't explain it to a five-year-old, then you don't understand it yourself. Awesome video!

  • @marcussmithwick6326
    @marcussmithwick6326 5 ปีที่แล้ว +24

    Cool as, been waiting for a sed/awk video

  • @MsSemki
    @MsSemki 9 หลายเดือนก่อน

    This is so thorough. Exactly what I've been looking for!

  • @KupriyanovKirill
    @KupriyanovKirill 5 ปีที่แล้ว +2

    Sometimes, unintentionally, people leave blank lines with trailing whitespaces (you will see them in visual mode, but the "s/^$/ d" won't). So, it would be safer to delete blank lines like that: "s/^\s*$/ d"

  • @sorachii8932
    @sorachii8932 5 ปีที่แล้ว +2

    I have a funny(for me) story about sed. Me and my mentor wrote a script to setup a DNS server just because we were curious if we could do it. We could but had to replace " to ' and had to thinker a bit to figure it out that we should have used sed to replace them. It was a good experience

  • @eli7693
    @eli7693 5 ปีที่แล้ว +5

    I'd just like to interject for a moment. What you’re referring to as Linux, is in fact, GNU/Linux, or as I’ve recently taken to calling it, GNU plus Linux. Linux is not an operating system unto itself, but rather another free component of a fully functioning GNU system made useful by the GNU corelibs, shell utilities and vital system components comprising a full OS as defined by POSIX.
    Many computer users run a modified version of the GNU system every day, without realizing it. Through a peculiar turn of events, the version of GNU which is widely used today is often called “Linux”, and many of its users are not aware that it is basically the GNU system, developed by the GNU Project. There really is a Linux, and these people are using it, but it is just a part of the system they use.
    Linux is the kernel: the program in the system that allocates the machine’s resources to the other programs that you run. The kernel is an essential part of an operating system, but useless by itself; it can only function in the context of a complete operating system. Linux is normally used in combination with the GNU operating system: the whole system is basically GNU with Linux added, or GNU/Linux. All the so-called “Linux” distributions are really distributions of GNU/Linux.

  • @tc40390
    @tc40390 5 ปีที่แล้ว +2

    I love how you make everything so simple

  • @philome4811
    @philome4811 2 ปีที่แล้ว

    I'm once again impressed by how knowledgeable Luke is o.O. Thank you Sir Luke.

  • @humanbeing_
    @humanbeing_ 2 ปีที่แล้ว

    This was FANTASTIC Luke. Thank you!

  • @occamsrazor1285
    @occamsrazor1285 5 ปีที่แล้ว +2

    Ah! /g = "greedy." If one understands regular expressions, one knows of greedy.

  • @mytech6779
    @mytech6779 ปีที่แล้ว +1

    The substitute command in vim is very similar but generally used with line ranges and can work with visually selected blocks, or from courser plus n lines.
    :%s/a/b/n # % is shorthand for all lines, n just prints number of matches no actual change
    :3,12s/a/b/c # lines 3 to 12, c is the confirm-each flag which highlights and asks before doing

  • @bloody_fool
    @bloody_fool 5 ปีที่แล้ว +3

    That gruvbox look fresh af

  • @Usertrappedindatabase
    @Usertrappedindatabase 4 ปีที่แล้ว +1

    It’s really great to have these references, please reach out and donate to Luke if you can, he’s one of the few Linux youtubers (i’m aware of) posting video documentation at this level. But also, RTFM. Always read the manual, stop telling yourself it’s hard to do, just dont be ADD like me and have patience. I am hoping i can contribute to Artix at some point, they really need talent, genius guys

  • @amorsmor8528
    @amorsmor8528 3 หลายเดือนก่อน

    watching this after finishing a "awk" course, makes sed looks very easy and simple, awk is the strongest

  • @davidczepanski1359
    @davidczepanski1359 5 ปีที่แล้ว +1

    That's an excellent tutorial, thanks, Luke. Very helpful.

  • @theboss15539
    @theboss15539 3 ปีที่แล้ว

    Something about "Two instances of LMAO" makes me LMAO

  • @Radd25
    @Radd25 3 ปีที่แล้ว

    Finally the sed tutorial I have been looking for

  • @qwarlock4126
    @qwarlock4126 3 หลายเดือนก่อน

    I always learn cool stuff from your vids. sed awk commands... just really cool

  • @yousif91y
    @yousif91y 5 ปีที่แล้ว

    man your explanation is far beyond this universe , i know i know , many thanks

  • @Fakheet
    @Fakheet 5 ปีที่แล้ว +5

    >uses emojis
    >uses 'LMAO'
    I think I'm starting to see a pattern

  • @thaidoan868
    @thaidoan868 4 หลายเดือนก่อน

    I appreciate what you did in this video.

  • @escravovoluntario6698
    @escravovoluntario6698 5 ปีที่แล้ว +1

    Luke is a real human bean!1!! A real hero!

  • @limitless1692
    @limitless1692 2 ปีที่แล้ว

    Linux is addictive.
    The more I lear about Linux the more I can do with less effort...
    The more I want Linux.

  • @sevenred2803
    @sevenred2803 4 ปีที่แล้ว

    Thank you, Luke. You are a real human bean.

  • @edwardmacnab354
    @edwardmacnab354 ปีที่แล้ว

    getting rid of trailing white space can be required under certain circumstances otherwise you end up with errors you have to figure out the cause of and since you can't see trailing white space it can be a nightmare .

  • @occamsrazor1285
    @occamsrazor1285 5 ปีที่แล้ว

    11:13 For anyone that wants to look these up and learn what they do, they're called 'anchors'

  • @SimGunther
    @SimGunther 5 ปีที่แล้ว +6

    TFW you realize that Plan9 regular expressions exists to supplant grep's regular expressions :(

    • @auslegungssache
      @auslegungssache 5 ปีที่แล้ว +1

      Finally someone who uses plan9! Do you use vis?

    • @SimGunther
      @SimGunther 5 ปีที่แล้ว

      @@auslegungssache I actually use vis, neovim, and emacs evil mode depending on what files I'm editing. Out of the three editors, vis and neovim are my favorites just because I've been infected with the vim bug for 2.5 years now :)

  • @snakeylps
    @snakeylps 4 ปีที่แล้ว +1

    This helped me understand sed, thanks man!!

  • @sharperguy
    @sharperguy 4 ปีที่แล้ว +1

    btw I read that usually when you combine sed, grep and awk, you can usually create the same effect by just using one of them if you know all the commands. I never figured out how though. I only ever use awk to for stuff like "{print $2}" but I guess its a fully functional scripting language.

  • @eduardojreis
    @eduardojreis 4 ปีที่แล้ว +1

    Replacing new line seems to be complicated, compared to `tr` command. I though we could use `
    ` such as in vim.

  • @Flash136
    @Flash136 5 ปีที่แล้ว +6

    nice

  • @douwehuysmans5959
    @douwehuysmans5959 5 ปีที่แล้ว +3

    Quality content

  • @ericr5481
    @ericr5481 5 ปีที่แล้ว

    This video is fantastic. Please do more like this.

  • @katzda
    @katzda 4 ปีที่แล้ว

    Amazing, thanks to you I leaned sed. Great tool to master!

  • @manzhoska
    @manzhoska 4 ปีที่แล้ว

    Thanks a lot! This is a great explanation! It would be cool to see more ways of sed and awk usage. Waiting on videos! ;)

  • @sharperguy
    @sharperguy 4 ปีที่แล้ว +2

    Lol I always used to write "command | head -5 | tail -1" to get the 5th line in a command output. Now I can just do "command | sed -n 5p". I learned some regex but only really used s/ commands

  •  5 ปีที่แล้ว

    I love this type of videos. I learnt a lot!

  • @ats1995
    @ats1995 5 ปีที่แล้ว

    Excellent! Recently dipped my toe in regex, so this was a perfect follow up

  • @Cmdrlucky8
    @Cmdrlucky8 4 ปีที่แล้ว

    Fantastic tutorial, thanks for sharing your talent dude!

  • @UNKPCRIDE
    @UNKPCRIDE 5 ปีที่แล้ว

    Awesome job! Your very smart and I like your creativity. Keep it up

  • @shankaranarayana6568
    @shankaranarayana6568 4 ปีที่แล้ว +1

    Thanks a lot! This was really helpful.

  • @superfunguy380
    @superfunguy380 4 ปีที่แล้ว

    Need more videos like this. Thanks for you a good tutorial.

  • @jamalkhan815
    @jamalkhan815 5 ปีที่แล้ว

    You are awesome Luke, bless ya, Thanks a lot for such a great video :)

  • @farjamful
    @farjamful 3 ปีที่แล้ว

    thanks, I wish I could like this video more than once!!!

  • @davinderpalsingh6412
    @davinderpalsingh6412 2 ปีที่แล้ว

    Nice video on sed command.

  • @user-ej3iw8lw3w
    @user-ej3iw8lw3w 2 ปีที่แล้ว

    sed - stream editor for filtering and transforming text

  • @drewmakes9274
    @drewmakes9274 4 ปีที่แล้ว

    I enjoyed this one a lot, thanks

  • @TeuFortMan
    @TeuFortMan 3 ปีที่แล้ว

    Pls don't place significant text in top bcz youtube hides it when i move mouse or control video stream

  • @alexiswiftrock
    @alexiswiftrock ปีที่แล้ว

    Your terminal color scheme/transparency is so nice ! If you don't mind could you detail the setup? Np if not.

  • @JoseArenasAlarcon
    @JoseArenasAlarcon 5 ปีที่แล้ว +2

    Great video. To delete blank lines with spaces: sed “/^[ \s]*$/ d” .file

  • @kastallion
    @kastallion 5 ปีที่แล้ว

    Nice practical video!

  • @pij3333
    @pij3333 5 ปีที่แล้ว

    Nice, short and concise.
    Have you looked into Wayland at all? Could make for an interesting video.

  • @kalyanirajalingham1286
    @kalyanirajalingham1286 2 ปีที่แล้ว

    Very nice!

  • @Zg4dNij
    @Zg4dNij ปีที่แล้ว

    good tutor Luke! old video as fuck but maybe for somene it will be helpful, I propose to use ^\s*$ as a pattern to delete blank lines, lot of time blank lise are in fact lines with some tabs/spaces, so with this pattern you also will delete it

  • @directx872
    @directx872 3 ปีที่แล้ว

    daddy fitmc is that you?

  • @loveanimals-0197
    @loveanimals-0197 4 ปีที่แล้ว +2

    Spoke 15 minutes for one command. That's America in a nutshell for you.

    • @dlarge6502
      @dlarge6502 4 ปีที่แล้ว

      Sed requires more than 15 mins. Especially to read the books published on it.

  • @LPFan33
    @LPFan33 5 ปีที่แล้ว

    thanks i luked at this video

  • @mikeziri
    @mikeziri 5 ปีที่แล้ว +1

    the ' /^$/ d' doesn't remove "visually empty" lines. so lines with only spaces or tabs

    • @rogo7330
      @rogo7330 3 ปีที่แล้ว

      /^\s*$/ d
      solved

  • @tebicap
    @tebicap 2 ปีที่แล้ว

    What terminal is it? How do you clean it without lost your current text? Thanks for the tutorial 🙋

  • @gouravsaini7446
    @gouravsaini7446 2 ปีที่แล้ว

    How to comment a line if you find a string in the mid of the line and that string you are getting from a file in a variable. please help me

  • @yuu-kun3461
    @yuu-kun3461 5 ปีที่แล้ว

    I personally use 's/something/&_else/g' quite a bit. Is damn useful.

  • @Mankepanke
    @Mankepanke 5 ปีที่แล้ว

    Som chat applications support the substitute command. Skype, for example, will replace "I care" with "IDGAF" in your last message if you send "s/I care/IDGAF/" in a new message.

  • @n.w.r5411
    @n.w.r5411 4 ปีที่แล้ว

    thanks man this is great

  • @testbug2421
    @testbug2421 5 ปีที่แล้ว

    Great video, would be cool to hear how you have configured your fonts, unicode, emojis and how you made them all work in the terminal

    • @mattia558
      @mattia558 5 ปีที่แล้ว

      do you have a specific issue? I've struggled with that recently, maybe there's a chance I can help

  • @MrJackojc90
    @MrJackojc90 5 ปีที่แล้ว +1

    Does this man ever rest?

  • @RichardLaya.
    @RichardLaya. 5 ปีที่แล้ว

    Nice work

  • @omerresnikoff3565
    @omerresnikoff3565 4 ปีที่แล้ว

    bruh, I've used vim for months and this is the first time I learn why some of their keybindings are so weird (don't tell me that ^ for first non blank character and $ for the end of the line is intuitive because it's not)

  • @GertBoers
    @GertBoers 5 ปีที่แล้ว

    Very informative, as usual. (y)

  • @lordsamich755
    @lordsamich755 5 ปีที่แล้ว

    I'm too sexy for your gawk, too sexy for your mawk... and so on.

  • @thingsiplay
    @thingsiplay 3 ปีที่แล้ว

    For you everything is easy. :D

  • @yomajo
    @yomajo ปีที่แล้ว

    why the whitespace before d in sed when deleting newlines? (newbie)

  • @555aboud
    @555aboud 5 ปีที่แล้ว +1

    hey luke, can you make a video on BSD in general?

  • @hannesthiersen8170
    @hannesthiersen8170 5 ปีที่แล้ว

    How and when were you introduced to GNU/Linux operating systems and how long did it take you to become comfortable with Linux (especially with Arch)? I've watch almost all your videos and I don't think you've mention a lot about this. Your first Linux related video was a little more than a 2 years ago, but you already seem to be somewhat of a power user in it.

  • @zwiebec78
    @zwiebec78 5 ปีที่แล้ว

    SO Good

  • @amarr9988
    @amarr9988 ปีที่แล้ว

    how to replace the third occurence using sed ?

  • @amzxa7574
    @amzxa7574 2 ปีที่แล้ว

    what if we want to make a change in more than one file?

  • @abbadkamel8839
    @abbadkamel8839 4 ปีที่แล้ว

    perfect thank you

  • @errrzarrr
    @errrzarrr 4 ปีที่แล้ว

    Inspiring

  • @MrKristian252
    @MrKristian252 5 ปีที่แล้ว +2

    8:35 itching nipple

    • @mnemonic_de
      @mnemonic_de 5 ปีที่แล้ว

      14:07 el el an eh

    • @desktorp
      @desktorp 5 ปีที่แล้ว

      nipplepit

  • @sachinvashishtha2137
    @sachinvashishtha2137 5 ปีที่แล้ว

    Hi, can you please make me understand the meaning of the "sed -i 's@^.*$@> awk.txt" command ? it will add > in the end of whole lines in file ....and how can we delete these > from the whole file again ?

  • @user-pw3pu8wu3f
    @user-pw3pu8wu3f 4 ปีที่แล้ว

    How would you replace 'print "a"' with 'print("a")'?

  • @brands2131
    @brands2131 5 ปีที่แล้ว

    You said you almost always use the g option unless you know you don't have to, then go on to show an example of removing comments with g. But you don't need g here, because you can only have at most, 1 comment per line, if you have 2 comments on a line, the first comment will already comment out the second comment anyway.
    (Also since you use \s* in the comment example, for consistence, you can do ^\s*$ for the blank line removal)

  • @D-Daws
    @D-Daws 2 ปีที่แล้ว

    Do you have a video on how you made the torrent status module?

  • @blackychan8175
    @blackychan8175 ปีที่แล้ว

    Hi, how to remove line after colon (:) ex: george:1234516 i need to remove george or number (not less the colon) Thankyouuu..

  • @cookiedestroyer402
    @cookiedestroyer402 5 ปีที่แล้ว

    can you do a video about larbs? like how it works and interchanging things inside it

  • @JR-pk1fr
    @JR-pk1fr 3 ปีที่แล้ว

    Do an in depth vid on awk