Show Menu
Cheatography

Tiny Helpers for awk, sed, vim and Script Cheat Sheet by

Code snippets — possibly helpful, whether used partially or combined — to speed up the process of creating simple scripts.

Introd­ucion

I know the tools and their syntax, but I don’t use them every day. When I do, I often feel like I’m starting from scratch. I hope the following snippets - partially or combined - help me get an idea and start faster in finding a solution. Maybe they’ll help you too?!
Just try them out and see 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-­away/'
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, multiple)

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, multiple)

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 "­-12­345­678­9--­-12­345­6--­-12­345­67"}'
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 \"-1­234­567­89-­---­123­456­---­123­456­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 in a 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

#!/usr­/bi­n/bash
hh=`date +%H`
echo "­Current hour:" $hh
read -p 'Hour to search for (x=Exit)?: ' 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
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)?:
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

Important note!

Please be careful with unwanted spaces and similar issues when copying and pasting!
                   
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Bash Script Colors Cheat Sheet
          [BASH] Windows Integrated Ubuntu Terminal Cheat Sheet
          Shell utilities & sytem management Cheat Sheet

          More Cheat Sheets by Rudi

          Windows Goodies (German / Deutsch) Cheat Sheet