Show Menu
Cheatography

Tiny helpers for awk, sed, vim and script Cheat Sheet (DRAFT) by

Code snippets, maybe helpful partial or merged, to starting faster in creating simple scripts.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Introd­ucion

I know the tools and the syntax. But I don't use it every day and when I have to use it, I'm starting nearly at the beginning. I hope the following (partial or merged) helps me to get an idea and start faster to find a solution. Maybe you too?!

Simple try it and look what you get :)

Made with current versions in 2025 (Ubuntu LTS https:­//u­bun­tu.com; GNU utilities for Windows https:­//u­nxu­til­s.s­our­cef­org­e.net).

AWK (octal, hex)

From:               To:
yin & yang        yin and yang
 
Lnx: echo -e yin '\0046' yang | awk '{gsub­(/­\x26/, "­and­"); print $0}'
Win: echo.exe yin \046 yang | gawk.exe "­{gs­ub(­/\x26/, \"an­d\"); print $0}"

SED (octal, hex)

From:                To:
yin & yang         yin and yang
 
Lnx: echo -e yin '\0046' yang | sed 's/\x2­6/and/'
Win: echo.exe yin \046 yang | sed.exe "­s/­\x26­/an­d/"

AWK (Condi­tions)

From:        To:
1               Line 2: 2
2               Line 3: 3
3               Line 5: 5
4
5
 
Lnx: echo -e '1\n2­\n3­\n4\n5' | awk '{if($0 > 1 && $0 <= 5 && NR != 4) print "Line " NR ": " $0}'
Win: echo.exe 1\n2\n­3\n4\n5 | gawk.exe "­{if($0 > 1 && $0 <= 5 && NR != 4) print \"Line \" NR \": \" $0}"

AWK (Subst­ring)

From:                      To:
wvvw.g­oog­le.de      ­wvv­w.g­oog­le.com
                               ­google
                               ­go-away
 
Lnx: echo wvvw.g­oog­le.com | awk 'BEGIN­{FS­="."}­{print $0; print $2; print substr($2, 1, 2)"-­awa­y"}'
Win: echo.exe wvvw.g­oog­le.com | gawk.exe "­BEG­IN{­FS=­\".­\"­}{print $0; print $2; print substr($2, 1, 2)\"­-aw­ay­\"­}"

SED (Subst­ring)

From:                       To:
wvvw.g­oog­le.de       go-away
 
Lnx: echo wvvw.g­oog­le.com | sed
's/^.\.\(..\).\..*/­\1-a­way/'

Win: echo.exe wvvw.g­oog­le.com |
sed.exe "s/^.\.\(..\).\..*/­\1-a­way­/"

AWK (System call)

From:       To:
Time:        Time: hh:mm
 
Lnx: echo "­Time: " | awk '{syst­em(­"date +%R" | getline m); print $0 m}' 2>/­dev­/null
Win: echo.exe Time: | gawk.exe "­{sy­ste­m(­\"time /t\" | getline m); print $0 m}" 2>NUL

AWK (Subst­itu­tion, mult.)

From:                               To:
E4-54-­E8-­6D-­31-AA       [subst cnt] e4:54:­e8:­6d:­31:aa
 
Lnx: echo E4-54-­E8-­6D-­31-AA | awk '{print gsub("-­", "­:"); print tolowe­r($0)}'
Win: echo.exe E4-54-­E8-­6D-­31-AA | gawk.exe "­{print gsub(­\"­-\", \":­\"); print tolowe­r($­0)}­"

SED (Subst­itu­tion, mult.)

From:                               To:
E4-54-­E8-­6D-­31-AA       e4:54:­e8:­6d:­31:aa
 
Lnx: echo E4-54-­E8-­6D-­31-AA | sed 's/-/:/g; s/^\(.*­\)­$/­\L&/'
Win: echo.exe E4-54-­E8-­6D-­31-AA | sed.exe "­s/-­/:/g; s/^\(.*­\)­$/­\L&­/"

AWK (printf)

From:                                                 To:
dummy1­-dummy2                                           Six \ Two       =          3.00
                                                          123456789    ­123456       1234567
 
Lnx: echo dummy1­-dummy2 | awk 'BEGIN­{FS­="-"; equ="="; bsl="\­\"­}{V­1="S­ix"; V2="­Two­"; V3=3; printf­(" %+9s %s %-6s %s %7.2f­\n", V1, bsl, V2, equ, V3)}EN­D{print " 123456789 123456 123456­7"}'
Win: echo.exe dummy1­-dummy2 | gawk.exe "­BEG­IN{­FS=­\"-­\"; equ=\"=­\"; bsl=\"\\ \"}{­V1=­\"Si­x\"; V2=\"Tw­o\"; V3=3; printf­(\" %+9s %s %-6s %s %#7.2f­\n­\", V1, bsl, V2, equ, V3)}EN­D{print \" 123456789 123456 123456­7\"}­"

VIM (Sort file, system call)

From:                  To:
bravo                   alpha
delta                    ­bravo
alpha                   bravo
bravo                   charlie
charlie                 delta
echo                    echo
foxtrot                  echo
echo                    echo
echo                    ­foxtrot
 
Lnx/Win: :1,$ !sort

VIM (Remove duplicate lines from sorted file)

From:                  To:
alpha                   alpha
bravo                   bravo
bravo                   charlie
charlie                 delta
delta                    echo
echo                    ­foxtrot
echo
echo
foxtrot
 
Lnx/Win: :%s/^­\(.*­\)­\(\n­\1­\)\+­$/\1/

VIM (Rearr­ang­ement, assign states to regions)

From:                                To:
AMER tab Canada             AMER tab Canada
APAC tab China                 APAC tab China tab Japan
APAC tab Japan                 EMEA tab Lebanon tab Senegal tab Spain
EMEA tab Lebanon
EMEA tab Senegal
EMEA tab Spain
 
Lnx/Win: :%s/^\(..\)\t\(..$\)\(­\n\1­\)­\+\t­\(..*$­\)/­\1­\t\2­\t\4/
               ­[Repeat until "­Pattern not found"]

Some Bash script

#!/bin­/bash
hh=`date +%H`
echo "­Current hour:" $hh
read -p 'Hour to search for (x=Exit)?: ' hour
# \< \> to supress hits of a single digit in a two digit hour
echo $hh | grep "­\<$­hou­r\>­" >/d­ev/null
RetCode=$?
if [ $hour == "­x" ]; then
    exit
elif [ $RetCode -eq 0 ]; then
    echo "$hh eq $hour Hit"
elif [ $RetCode -eq 1 ]; then
    echo "$hh neq $hour noHit"
fi
a=`expr $hour + 1`
echo -e "­$hour + 1 =" $a "­\n"

Some Batch script

@echo off
REM Some echo. commands would make it nicer ...
cls
set hh=%ti­me:­~-11,2%
if %hh% LSS 10 set hh=0%t­ime­:~-­10,1%
echo Current hour: %hh%
set /P hour=Hour to search for (x=Exit)?:
:: Brackets to identify blank input.
if [%hour­%]==[x] goto :EOF
echo %hh% | find "­%ho­ur%­" >NUL
if errorlevel 1 echo %hh% neq %hour% noHit
if not errorlevel 1 echo %hh% equ %hour% Hit
for /f %%d in ('date /T') do set dat=%%d
echo %dat%
if %dat% NEQ "­01.0­1.1­97­0" set /a sum=1+1
echo 1+1=%sum%
pause

Some Vim script

" Execute: vim -S this.s­cript
:function! IN()
:     call inputs­ave()
:     let @i = input(­'Hour to search for?: ')
:     call inputr­est­ore()
:endfu­nction
:let hh=str­fti­me(­"­%H")
:echo "­Current hour:" hh
:call IN()
:let hour = @i
:if hh == hour
:     echo "­\n" hh "­equ­" hour "­Hit­"
:elseif hh != hour
:     echo "­\n" hh "­neq­" hour "­noH­it"
:endif
:let a = hour + 1
:echo hour "+ 1 =" a