\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Benjy} \pdfinfo{ /Title (bash-sed.pdf) /Creator (Cheatography) /Author (Benjy) /Subject (Bash - SED Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Bash - SED Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Benjy} via \textcolor{DarkBackground}{\uline{cheatography.com/56918/cs/15071/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Benjy \\ \uline{cheatography.com/benjy} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 5th October, 2019.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Options}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# -n (default printing of pattern buffer) \newline sed 'p' \# print twice \newline sed -n 'p' \# cat \newline \newline \# -e (chain commands) \newline sed -e '1d' -e '3d' \newline \newline \# -f (script file) \newline echo -e '1d\textbackslash{}n3d' \textgreater{} cmd.txt \newline sed -f cmd.txt file.txt} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Useful recipes}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# Unix commands \newline sed '' file \# cat file \newline sed '10q' \# head \newline sed -n '\$p' \# tail \newline sed 'w file.bak' file \# cp file file.bak \newline sed -n '\$=' file \# wc -l file \newline sed 'y/a/A/' \# tr "a" "A" \newline \newline \# useful \newline sed -n '/\textasciicircum{}\$/p' \# show non-empty lines \newline sed 's/\textasciicircum{}\#.*//g' \# remove comments \newline sed '3,6 s/\textasciicircum{}/\#/' \# add comments} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{substitue -\{\{noshy\}\}{[}a1{[},a2{]}{]}s/p1/p2/{[}flags{]}}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# occurrences \newline sed 's/foo/bar/' \# only first \newline sed 's/foo/bar/2' \# second \newline sed 's/foo/bar/g \# global flag, all occurrences \newline \newline sed -n 's/foo/bar/p' \# print only changed lines \newline sed -n 's/foo/bar/w file' \# writes to file \newline sed -n 's/FOO/bar/pi' \# case insensitive \newline \newline \# uppercase - lowercase \newline sed -n 's@foo@\textbackslash{}ufoo@p' \# one letter uppercase \newline sed -n 's@foo@\textbackslash{}Ufoo@p' \# uppercase \newline sed -n 's@foo@\textbackslash{}LFOO@p' \# lowercase \newline sed -n 's@foo@\textbackslash{}Ufoo\textbackslash{}Ebar@p' \# back to normal \newline \newline \# catching char \newline \# with \& (no regex, only one char) \newline sed 's/{[}{[}:digit:{]}{]}/Number \&/' \newline \# with catching group \textbackslash{}( \textbackslash{}) \newline sed 's@\textbackslash{}(\textbackslash{}w\textbackslash{}+\textbackslash{}) \textbackslash{}(\textbackslash{}w\textbackslash{}+\textbackslash{})@\textbackslash{}2 \textbackslash{}1@' \# re-order} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{regex - need escape}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textbackslash{}? \newline \textbackslash{}+ \newline \# catching group \newline \textbackslash{}( \textbackslash{}) \newline \# occurrence \newline \textbackslash{}\{ \textbackslash{}\} \newline \# logical or \newline \textbackslash{}|} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textasciicircum{}, \$ and \textbackslash{}\textbackslash{}` (beginning of pattern space)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{print - {[}address1{[},address2{]}{]}p}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# line number \newline sed -n '3p' \newline sed -n '3,\$p' \# range (\$ is end) \newline \newline \# pattern \newline sed -n '/foo/p' \newline sed -n '/foo/,/bar/p' \# between matching lines \newline \newline \# + and \textasciitilde{} \newline sed -n '3,+2p' \# 3 and next 2 lines \newline sed -n '1\textasciitilde{}2p' \# every two lines} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{same for delete - {[}address1{[},address2{]}{]}d and write - {[}address1{[},address2{]}{]}w file} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{insert - {[}address{]}i line}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{sed '4i text' \# insert line above 4th \newline sed '/foo/i text'} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{same for append - {[}address{]}a line (below)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{change -\{\{noshy\}\}{[}address1{[},address2{]}{]}c~line}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{sed '3,6c line' \# replace by single line (note)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{consider range as single block} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{translate -\{\{noshy\}\}{[}addr1{[},addr2{]}{]}y/list1/list2/}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{echo "a b c" | sed 'y/abc/123/' \# outputs 1 2 3} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{replace char i of list1 by char i of list2} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{line numbering -\{\{noshy\}\}{[}address1{[},address2{]}{]}=}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{sed -n '/foo/=' \newline sed -n '\$=' \# wc -l} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{line number above every match} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{read - {[}address{]}r file}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{sed '3r file' \# content of file after 3rd line} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{hidden char -\{\{noshy\}\}{[}addr1{[},addr2{]}{]}l~{[}wrap{]}}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{sed -n 'l' \# displays \textbackslash{}t and \textbackslash{}n as \$ \newline sed -n 'l len' \# wraps line (\textbackslash{}) after len char} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{quit - {[}address{]}q {[}exit code{]}}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{sed '3q' \# stops execution after 3rd line} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{execute -\{\{noshy\}\}{[}address1{[},address2{]}{]}e~cmd}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{sed '3e ls' \# execute ls before 3rd line} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{loops}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# b label \newline sed -n ':label;s/\textasciicircum{}/-/;/-{}-{}-/!b label;p' \newline \# t label \newline sed -n ':label;s/\textasciicircum{}/-/;/-{}-{}-/!t label;p'} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{goto if pattern matches} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{N and P}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# N adds two lines to buffer, separated by '\textbackslash{}n' \newline sed -n 'N;s/\textbackslash{}n/,/p' \# concatenate lines 2 by 2 \newline \# P print first part of N (before '\textbackslash{}n') \newline sed -n 'N;P' \# sed -n '1\textasciitilde{}2p'} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{n and x - load / exchange buffers}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{sed 'n' \# cat \newline \newline \# exchange, load next, print \newline sed 'x;n;p' \# sed -n '2\textasciitilde{}2p' \newline \newline \# exchange, load next, exchange, print \newline sed 'x;n;x;p' \# sed -n '1\textasciitilde{}2p'} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{h and H - pattern to hold}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# {[}address1{[},address2{]}{]}h \newline \# copies pattern to hold - overwrite hold \newline \# exchange, print previous \newline sed -n '/foo/!h;/foo/\{x;p\}' \newline \newline \# {[}address1{[},address2{]}{]}H \newline \# append pattern to hold \newline \# append current, exchange, print \newline sed -n '/foo/!h;/foo/\{H;x;p\}' \# order (n-1, n)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{g and G - hold to pattern}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# {[}address1{[},address2{]}{]}g \newline \# copies hold to pattern - overwrite pattern \newline \# print, copy previous, print \newline sed -n '/foo/!h;/foo/\{p;g;p\}' \# order (n, n-1) \newline \newline \# {[}address1{[},address2{]}{]}G \newline \# append hold to pattern \newline \# print, copy previous, print \newline sed -n '/foo/!h;/foo/\{G;p\}' \# same as above} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}