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.- Print specific lines from a file using line numbers.# print lines 12 to 22sed -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 patternssed -nE '/^foo/,/^bar/p' file.txt
- Avoid printing the searched patternsed -n 's/^my_string//p' file.md
- Insert contents of file after a certain linesed '5 r newfile.txt' file.txt
- Change line containing regex/pattern matchsed '/MATCH THIS/c\REPLACE WITH THIS' file.txt
- 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 "%;"
- Print every 4th word (or field) from a space separated STDIN.cut -d' ' -f4I don't know about you but this is pretty cool.
- Don't print first line of fileawk NR\>1 file.txt