How to on one line sed commands?



  • We have our sample data_file with file contents as follows: Refer to this file to see the original contents.

    hamster, furry little creature, 2 hamster drive
    donkey, back breaking helper, 8 donkey jacket road
    horse,  top racer, 12 Horse throat Lane
    badger,  night rider, 15 Badger Close 
    cow,  loves eating grass all day, 16 Moofield Aveune
    bull,  these horns need shapening, 16 Moofield Aveune
    sheep, can you take this coat off, 6 Wool road
    

    As good practice in a live environment work on copies of files, make sure the output is how you want first.

    Oneliners

    Delete all the contents, remember sed does not made edits on the file itself, so the original contents will still be there, this is probably not something you will be doing, but to demonstrate sed's delete command "d". There is no output, but your original file remains unchanged.

    $ sed 'd' data_file
    

    Delete the first line of a file.

    $ sed '1d' data_file 
    donkey, back breaking helper, 8 donkey jacket road
    horse,  top racer, 12 Horse throat Lane
    badger,  night rider, 15 Badger Close 
    cow,  loves eating grass all day, 16 Moofield Aveune
    bull,  these horns need shapening, 16 Moofield Aveune
    sheep, can you take this coat off, 6 Wool road
    

    Delete the last line in a file.

    $ sed '$d' data_file 
    hamster, furry little creature, 2 hamster drive
    donkey, back breaking helper, 8 donkey jacket road
    horse,  top racer, 12 Horse throat Lane
    badger,  night rider, 15 Badger Close 
    cow,  loves eating grass all day, 16 Moofield Aveune
    bull,  these horns need shapening, 16 Moofield Aveune
    

    Delete only blank lines in a file. Prints the file as is, because no blank lines are in the file, you can add your own blank lines to verify this.

    $ sed '/^$/d' data_file 
    hamster, furry little creature, 2 hamster drive
    donkey, back breaking helper, 8 donkey jacket road
    horse,  top racer, 12 Horse throat Lane
    badger,  night rider, 15 Badger Close 
    cow,  loves eating grass all day, 16 Moofield Aveune
    bull,  these horns need shapening, 16 Moofield Aveune
    sheep, can you take this coat off, 6 Wool road
    

    Delete range of lines in file, by using an address of matching words.

    $ sed '/horse/,/badger/d' data_file 
    hamster, furry little creature, 2 hamster drive
    donkey, back breaking helper, 8 donkey jacket road
    cow,  loves eating grass all day, 16 Moofield Aveune
    bull,  these horns need shapening, 16 Moofield Aveune
    sheep, can you take this coat off, 6 Wool road
    

    Delete lines 3 through 6 in the file.

    $ sed '3,6d' data_file 
    hamster, furry little creature, 2 hamster drive
    donkey, back breaking helper, 8 donkey jacket road
    sheep, can you take this coat off, 6 Wool road
    

    Delete line 1 through the first blank line. I have inserted a blank line for demonstration purposes between, badger and cow. Note that if your file has no blank lines and you run this command it removes all the lines, not literally because sed does not edit the original file, but does shows you the output after the changes.

    $ sed '1,/^$/d' data_file 
    cow,  loves eating grass all day, 16 Moofield Aveune
    bull,  these horns need shapening, 16 Moofield Aveune
    sheep, can you take this coat off, 6 Wool road
    

    Delete the inverse of the matching address using an exclamation mark.
    First a normal address delete.

    $ sed '3,6d' data_file 
    hamster, furry little creature, 2 hamster drive
    donkey, back breaking helper, 8 donkey jacket road
    sheep, can you take this coat off, 6 Wool road
    

    Then a inverse, delete everything but line 3 through 6.

    $ sed '3,6!d' data_file 
    horse,  top racer, 12 Horse throat Lane
    badger,  night rider, 15 Badger Close 
    cow,  loves eating grass all day, 16 Moofield Aveune
    bull,  these horns need shapening, 16 Moofield Aveune
    

    Substitute a tab with another string. The tab file looks like this:

    hamsterparty	donkeyparty	horseparty	badgerparty
    

    Replace the 3rd tab with three dashes.

    $ sed 's/\t/ --- /3' tabfile 
    hamsterparty	donkeyparty	horseparty --- badgerparty
    

Log in to reply
 

© Lightnetics 2024