\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{{[}deleted{]}} \pdfinfo{ /Title (sed.pdf) /Creator (Cheatography) /Author ({[}deleted{]}) /Subject (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{Sed Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{{[}deleted{]}} via \textcolor{DarkBackground}{\uline{cheatography.com/30394/cs/9098/}}} \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}{[}deleted{]} \\ \uline{cheatography.com/deleted-30394} \\ \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 3rd October, 2016.\\ 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{tabularx}{17.67cm}{x{3.7994 cm} x{13.4706 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{regexp}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{{\bf{wild characters}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} {\emph{char}} & a single character, if not special, is matched against text \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} . & matches any character \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} * & matches a sequence of 0 or more repetitions of previous character/grouped regexp/class \tn % Row Count 7 (+ 3) % Row 4 \SetRowColor{LightBackground} .* & match all characters on every line (including empty ones) \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} \textbackslash{}+ & as *, but matches 1 or more \tn % Row Count 10 (+ 1) % Row 6 \SetRowColor{LightBackground} .\textbackslash{}+ & match all characters on every non-empty line \tn % Row Count 12 (+ 2) % Row 7 \SetRowColor{white} \textbackslash{}? & as *, but only matches 0 or 1 character \tn % Row Count 14 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{{\bf{special characters}}} \tn % Row Count 15 (+ 1) % Row 9 \SetRowColor{white} \textbackslash{}n & new line \tn % Row Count 16 (+ 1) % Row 10 \SetRowColor{LightBackground} \textbackslash{}t & tab \tn % Row Count 17 (+ 1) % Row 11 \SetRowColor{white} \textbackslash{}s & whitespace \tn % Row Count 18 (+ 1) % Row 12 \SetRowColor{LightBackground} \textbackslash{}S & any non-whitespace character \tn % Row Count 19 (+ 1) % Row 13 \SetRowColor{white} \textbackslash{}w & any word character (letter, digit, underscore) \tn % Row Count 21 (+ 2) % Row 14 \SetRowColor{LightBackground} \textbackslash{}W & any non-word character \tn % Row Count 22 (+ 1) % Row 15 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{{\bf{line beginning}}} \tn % Row Count 23 (+ 1) % Row 16 \SetRowColor{LightBackground} \textasciicircum{} & matches the null string at the beginning of the line. What appears after \textasciicircum{} must appear at the beginning of the line \tn % Row Count 27 (+ 4) % Row 17 \SetRowColor{white} \textasciicircum{}\# & match every line beginning with a \# character \tn % Row Count 29 (+ 2) % Row 18 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{{\bf{line ending}}} \tn % Row Count 30 (+ 1) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.7994 cm} x{13.4706 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{regexp (cont)}} \tn % Row 19 \SetRowColor{LightBackground} \$ & same as \textasciicircum{}, but refers to the end of line \tn % Row Count 2 (+ 2) % Row 20 \SetRowColor{white} \textbackslash{}\$ & dollar sign is escaped, so this matches lines ending with a single dollar \tn % Row Count 5 (+ 3) % Row 21 \SetRowColor{LightBackground} \textbackslash{}\textbackslash{}\$ & backslash is escaped, so this matches lines ending with a single backslash \tn % Row Count 8 (+ 3) % Row 22 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{{\bf{number of sequences}}} \tn % Row Count 9 (+ 1) % Row 23 \SetRowColor{LightBackground} \textbackslash{}\{i\textbackslash{}\} & as *, but matches exactly {\emph{i}} number of sequences \tn % Row Count 11 (+ 2) % Row 24 \SetRowColor{white} \textbackslash{}\{i,\textbackslash{}\} & matches more than or equal to {\emph{i}} sequences \tn % Row Count 13 (+ 2) % Row 25 \SetRowColor{LightBackground} \textbackslash{}\{i,j\textbackslash{}\} & matches between {\emph{i}} and {\emph{j}} sequences, inclusive \tn % Row Count 15 (+ 2) % Row 26 \SetRowColor{white} .\textbackslash{}\{9\textbackslash{}\}A\$ & matches an {\emph{A}} that is the last character on line, with at least 9 preceding characters \tn % Row Count 18 (+ 3) % Row 27 \SetRowColor{LightBackground} \textasciicircum{}.\textbackslash{}\{15\textbackslash{}\}A & matches an {\emph{A}} that is the 16th character on a line \tn % Row Count 20 (+ 2) % Row 28 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{{\bf{groups and lists}}} \tn % Row Count 21 (+ 1) % Row 29 \SetRowColor{LightBackground} {[}{\emph{list}}{]} & matches any single character in list. Dashes indicate inclusive sequences. \tn % Row Count 24 (+ 3) % Row 30 \SetRowColor{white} {[}a-zA-Z0-9{]} & matches any letters or digits \tn % Row Count 26 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{6.7353 cm} x{10.5347 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{printing}} \tn % Row 0 \SetRowColor{LightBackground} sed '' {\emph{file}} & auto print file contents to command line (essentially `cat`) \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} sed -n 'p' {\emph{file}} & `-n` surpresses auto printing of each line; `p` prints each line (same result as above) \tn % Row Count 7 (+ 4) % Row 2 \SetRowColor{LightBackground} sed -n '1p' {\emph{file}} & print only 1st line \tn % Row Count 9 (+ 2) % Row 3 \SetRowColor{white} sed -n '1,5p' {\emph{file}} & print 1st through 5th lines \tn % Row Count 11 (+ 2) % Row 4 \SetRowColor{LightBackground} sed -n '1,+4p' {\emph{file}} & print 1st line and next 4 lines (same output as above) \tn % Row Count 14 (+ 3) % Row 5 \SetRowColor{white} sed -n '1\textasciitilde{}2p' {\emph{file}} & print every 2nd line beginning with the 1st \tn % Row Count 16 (+ 2) % Row 6 \SetRowColor{LightBackground} sed -n '/keyword/p' {\emph{file}} & prints every line that contains {\emph{keyword}} \tn % Row Count 18 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{6.3899 cm} x{10.8801 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{deleting}} \tn % Row 0 \SetRowColor{LightBackground} sed '1\textasciitilde{}2d' {\emph{file}} & delete every 2nd line beginning with the 1st (without `-n` option will also print what remains) \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} sed '/\textasciicircum{}\$/d' {\emph{file}} & matches any blank lines and passes them to the delete command \tn % Row Count 7 (+ 3) % Row 2 \SetRowColor{LightBackground} sed '/\textasciicircum{}\$/!d' {\emph{file}} & delete any line that is not blank (! inverts the address) \tn % Row Count 10 (+ 3) % Row 3 \SetRowColor{white} sed 's/{[}0-9{]}//g' {\emph{file}} & delete all digits in all lines \tn % Row Count 12 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{7.2534 cm} x{10.0166 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{interact with files}} \tn % Row 0 \SetRowColor{LightBackground} sed '1\textasciitilde{}2d' {\emph{file}} \textgreater{} {\emph{newfile}} & delete every 2nd line from {\emph{file}}, print remaining lines to {\emph{newfile}} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} sed -i '1\textasciitilde{}2d' {\emph{file}} & delete every 2nd line "in-place", changes original file \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} sed -i.bak '1\textasciitilde{}2d' {\emph{file}} & create backup file with .bak extension, edit the regular file in place \tn % Row Count 10 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{Note that source file is not affected by the basic commands; the edits are directed only to the command line unless explicitly directed to a file.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{substitute}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{{\bf{basic substitution}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} sed \seqsplit{'s/oldword/newword/'} & change 1st instance of {\emph{oldword}} in each line to {\emph{newword}} \tn % Row Count 4 (+ 3) % Row 2 \SetRowColor{LightBackground} sed \seqsplit{'s|dir1/oldword|dir2/newword|'} & if string includes forward slash, other valid delimiters include pipe, underscore \tn % Row Count 9 (+ 5) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{{\bf{start options}}} \tn % Row Count 10 (+ 1) % Row 4 \SetRowColor{LightBackground} sed \seqsplit{'keyword/s/oldword/newword/'} {\emph{file}} & replace first instance of {\emph{oldword}} with {\emph{newword}} in any line that includes {\emph{keyword}} \tn % Row Count 15 (+ 5) % Row 5 \SetRowColor{white} sed '1,3s/.*/newword/' {\emph{file}} & substitute everything in 1st, 2nd, and 3rd lines with {\emph{newword}} \tn % Row Count 19 (+ 4) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{{\bf{end flags}}} \tn % Row Count 20 (+ 1) % Row 7 \SetRowColor{white} sed 's/oldword/newword/{\emph{flag}}' {\emph{file}} & {\emph{flag}} can be any of the below \tn % Row Count 22 (+ 2) % Row 8 \SetRowColor{LightBackground} g & substitute every instance of oldword instead of just the first on each line (default behavior) \tn % Row Count 27 (+ 5) % Row 9 \SetRowColor{white} 2 & substitute only {\emph{number}}th instance of oldword on each line \tn % Row Count 30 (+ 3) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{substitute (cont)}} \tn % Row 10 \SetRowColor{LightBackground} p & print new pattern space for all lines where substitution was made \tn % Row Count 4 (+ 4) % Row 11 \SetRowColor{white} i & ignore case \tn % Row Count 5 (+ 1) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{{\bf{other commands}}} \tn % Row Count 6 (+ 1) % Row 13 \SetRowColor{white} sed 's/word/(\&)/' {\emph{file}} & `\&` holds matched pattern ({\emph{word}}) and puts parentheses around it \tn % Row Count 10 (+ 4) % Row 14 \SetRowColor{LightBackground} sed \seqsplit{'s/old/new/';s/first/second/'} {\emph{file}} & semicolon strings together distinct commands \tn % Row Count 13 (+ 3) % Row 15 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{{\bf{examples}}} \tn % Row Count 14 (+ 1) % Row 16 \SetRowColor{LightBackground} sed -n \seqsplit{'s/oldword/newword/2p'} {\emph{file}} & prints the lines where substitution took place \tn % Row Count 17 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{7.4261 cm} x{9.8439 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{random tricks}} \tn % Row 0 \SetRowColor{LightBackground} sed 's/.*/\textbackslash{}"\&\textbackslash{}"/g' & add double quotes to line \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} sed \seqsplit{"s|.*|\$dirname|"} {\emph{file}} & use double quotes to expand variables within replacement. Use a different delimiter if variable contains slashes (e.g. directory path) \tn % Row Count 9 (+ 7) % Row 2 \SetRowColor{LightBackground} {\emph{var}}=\$(sed -n "\$\{i\}p" {\emph{file}}) & set variable equal to line in {\emph{file}}, where line number {\emph{i}} is also a variable \tn % Row Count 13 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{Also remember `sed` reads and operates line by line. Some commands modify the output stream directly (so can't use results for more editing unless pipe it to another `sed` command).} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \end{document}