Today I Learned
Ask or search…
Comment on page

Line Editors in Linux, Tips and Tricks

I will log various ways through which tools like sed, cut and tr can be used.

sed 😥

  • Print specific lines from a file using line numbers.
    # print lines 12 to 22
    sed -n '12,22p' file.txt
  • Omit first line of output.
    sed -n '1!p'
  • Omit last line of file.
    sed '$d' file.txt
  • Print everything after a pattern (inclusive).
    sed -n '/pattern/,$ p' file.txt
  • Print everything before a pattern (inclusive).
    sed -n '1,/pattern/ p' file.txt
  • Print everything between two patterns
    sed -nE '/^foo/,/^bar/p' file.txt
  • Avoid printing the searched pattern
    sed -n 's/^my_string//p'
  • Insert contents of file after a certain line
    sed '5 r newfile.txt' file.txt
  • Change line containing regex/pattern match
    sed '/MATCH THIS/c\REPLACE WITH THIS' file.txt

tr ➡️

  • Translate (or convert) all () to [] in a text file.
    tr '()' '[]'
  • Translate all occurrences of multiple spaces with a single space.
    tr -s ' '
  • Remove unwanted characters from string.
    # will delete % and ;
    echo "1;00%" | tr -d "%;"

cut ✂️

  • Print every 4th word (or field) from a space separated STDIN.
    cut -d' ' -f4
    I don't know about you but this is pretty cool.


  • Don't print first line of file
    awk NR\>1 file.txt

Resources & Learning Material