Posted: . At: 10:22 AM. This was 1 year ago. Post ID: 17605
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


More very useful sed tips for the command line on Linux.


The sed utility is very useful on a Linux machine. It can do numerous things to manipulate text easily.

This example center aligns all text in the terminal. This is very useful.

[john@localhost ~]$ ls -hula | sed  -e :a -e 's/^.\{1,77\}$/ &/;ta' -e 's/\( *\)\1/\1/'
                                   total 36K
                 drwx------. 16 john john 4.0K Feb 13 09:21 .
                 drwxr-xr-x.  3 root root   18 Feb  8 06:49 ..
           -rw-------.  1 john john  576 Feb 13 09:07 .bash_history
            -rw-r--r--.  1 john john   18 Nov  2 03:47 .bash_logout
           -rw-r--r--.  1 john john  141 Feb 13 09:07 .bash_profile
              -rw-r--r--.  1 john john  492 Feb 13 09:07 .bashrc
               drwxr-xr-x. 20 john john 4.0K Feb  6 13:30 .cache
              drwx------. 18 john john 4.0K Feb  6 13:30 .config
              drwxr-xr-x.  2 john john    6 Feb 13 09:21 Desktop
             drwxr-xr-x.  2 john john   99 Feb 13 09:21 Documents
             drwxr-xr-x.  2 john john    6 Feb 13 09:21 Downloads
               drwx------.  4 john john   32 Feb  6 13:30 .local
              drwxr-xr-x.  5 john john   54 Oct 30 11:36 .mozilla
               drwxr-xr-x.  2 john john    6 Feb 13 09:21 Music
              drwxr-xr-x.  2 john john  101 Feb 13 09:21 Pictures
               drwxr-xr-x.  2 john john    6 Feb 13 09:21 Public
          -rw-------.  1 john john  541 Feb  6 16:47 .python_history
                drwx------.  4 john john   33 Feb 13 09:21 snap
                drwx------.  2 john john   25 Feb  6 15:41 .ssh
             drwxr-xr-x.  2 john john    6 Feb 13 09:12 Templates
               drwxr-xr-x.  2 john john    6 Feb 13 09:21 Videos
               -rw-r--r--.  1 john john  658 May 15  2022 .zshrc

Print the last 10 lines of a text file with sed. Using this one-liner. sed -e :a -e ‘$q;N;11,$D;ba’

[john@localhost ~]$ curl -s https://www.gutenberg.org/cache/epub/2147/pg2147.txt | sed -e :a -e '$q;N;11,$D;ba'
edition.
 
Most people start at our website which has the main PG search
facility: www.gutenberg.org
 
This website includes information about Project Gutenberg-tm,
including how to make donations to the Project Gutenberg Literary
Archive Foundation, how to help produce our new eBooks, and how to
subscribe to our email newsletter to hear about new eBooks.

Print section of a text file between two regular expressions (inclusive). This could be a very useful sed tip indeed.

[john@localhost ~]$ curl -s https://www.gutenberg.org/cache/epub/2147/pg2147.txt | sed -n '/waited in vain/,/gloom/p'
      We waited in vain for the arrival of the sixth day—that day to me
      has not yet arrived—to the Swede, never did arrive. Thenceforward
      we were enshrouded in patchy darkness, so that we could not have
      seen an object at twenty paces from the ship. Eternal night
      continued to envelop us, all unrelieved by the phosphoric
      sea-brilliancy to which we had been accustomed in the tropics. We
      observed too, that, although the tempest continued to rage with
      unabated violence, there was no longer to be discovered the usual
      appearance of surf, or foam, which had hitherto attended us. All
      around were horror, and thick gloom, and a black sweltering

Reverse all text in a file using sed. This is easy. This sed regex does this easily: sed ‘/\n/!G;s/(.)(.*\n)/&\2.

[john@localhost ~]$ curl -s https://www.abc.net.au/news/feed/8055316/rss.xml | xmlstarlet sel -T -t -v '/rss/channel/item/title' | sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'
daeha yal segnellahc nraw tub 'elbativeni' s'ti timda sgniK ?esihcnarf LBN yendyS dnoces A
elbat neerg eht no tseb eht tsniaga pu gniog ydaerla s'eh tub ,bup eht ta reeb a evah t'nac nedyaJ
yas srenwo ,hguone raf og t'nod seitrap rojam yb ocsaif srewoT tocsaM rof sexiF :'eramthgin siht dnE'
dnamed gniteem si egnellahc tseggib rieht tub ,evird ot nrael elpoep gnipleh si margorp sihT
yriuqni licnuoC erihS slliH ot ecnedive evig ot srehtorb s'reimerp gniriuqer fo pets 'suoires yrev' sekat yriuqni yratnemailrap WSN
yrujni gnitoohs htiw yab ycnegreme latipsoh ta tfel namow retfa degrahc naM
sreyalp LFN 'deraef tsom eht fo eno' emaceb ataliaM nadroJ woH :lwoB repuS eht ot nwotsknaB morF
sisirc latner dima ailartsuA ot nruter ot tes stneduts lanoitanretni fo sdnasuohT
ytud dehcaerb rediaR arrebnaC detserra ohw ecilop syas etartsigaM :'detluassa gnieb moT naht rehto gnihtoN'
yrujni niarb citamuart htiw yob oy5 tfel tluassa kcitsmoorb retfa sraey 01 rof deliaj naM
tuoyap m1$ rof tes erif no reh gnittes yb efiw gniredrum fo ytliug ton dnuof naM
 yriuqni tnorf ot senilced srewot yendyS laisrevortnoc dniheb repoleveD
sreganeet evif gnillik ,eert otni etu dehsarc ohw revird rof dedargpu segrahC
thginrevo srentrap ssenisub dna seitseb emaceb anneJ dna nadnerB snaidemoc woH
?tenalp eht evas teid nagev a lliW
dellecnac neeb sah pihsnezitic sih tub ,nailartsuA sa flesmih nees syawla sah ttaM :'ma I ohw fo trap eroc A'
demees ti tahw t'nsaw tI .bulc gninim llams a morf noitanod 000,441$ a deralced slarebiL ehT
enoz retsasid ot levart ekauqhtrae airyS-eyikrüT ni daed deraef nam yendyS fo ylimaF
yendyS fo steerts no denodnaba sekib erahs fo sderdnuh rof noitulover weN
'gniweps' tfel yrewerb lavir sa elttab reeb truoc niw snaidemoC
gnitoohs retfa 'niaga klaw ot ylekilnu' nam yretsym yb latipsoh yendyS ta depmud namoW
sgge reh ezeerf ot dediced letnahC yhW :'pu-kaerb a retfa snoitpo ym gniredisnoc saw I'
rekrow elamef yb noitneted htuoy ni esuba lauxes gnigella drawrof emoc neM
'edargpu latot' rof tnarg m71$ yb devas knir eci yendyS sa 'feiler eguH'
naidrauG s'nerdlihC wen rof hcraes ni srennurtnorf elamef revo dessap retsinim WSN

Align all text to the right, using 160 columns of width.

[john@localhost ~]$ curl -s https://www.abc.net.au/news/feed/8055316/rss.xml | xmlstarlet sel -T -t -v '/rss/channel/item/title' | sed -e :a -e 's/^.\{1,160\}$/ &/;ta' ; echo
                                                                       A second Sydney NBL franchise? Kings admit it's 'inevitable' but warn challenges lay ahead
                                                               Jayden can't have a beer at the pub, but he's already going up against the best on the green table
                                                            'End this nightmare': Fixes for Mascot Towers fiasco by major parties don't go far enough, owners say
                                                                     This program is helping people learn to drive, but their biggest challenge is meeting demand
                              NSW parliamentary inquiry takes 'very serious' step of requiring premier's brothers to give evidence to Hills Shire Council inquiry
                                                                                      Man charged after woman left at hospital emergency bay with shooting injury
                                                                 From Bankstown to the Super Bowl: How Jordan Mailata became 'one of the most feared' NFL players
                                                                                Thousands of international students set to return to Australia amid rental crisis
                                                      'Nothing other than Tom being assaulted': Magistrate says police who arrested Canberra Raider breached duty
                                                                        Man jailed for 10 years after broomstick assault left 5yo boy with traumatic brain injury
                                                                                 Man found not guilty of murdering wife by setting her on fire set for $1m payout
                                                                                          Developer behind controversial Sydney towers declines to front inquiry 
                                                                                    Charges upgraded for driver who crashed ute into tree, killing five teenagers
                                                                                   How comedians Brendan and Jenna became besties and business partners overnight
                                                                                                                               Will a vegan diet save the planet?
                                                    'A core part of who I am': Matt has always seen himself as Australian, but his citizenship has been cancelled
                                                                     The Liberals declared a $144,000 donation from a small mining club. It wasn't what it seemed
                                                                             Family of Sydney man feared dead in Türkiye-Syria earthquake travel to disaster zone
                                                                                        New revolution for hundreds of share bikes abandoned on streets of Sydney
                                                                                                  Comedians win court beer battle as rival brewery left 'spewing'
                                                                           Woman dumped at Sydney hospital by mystery man 'unlikely to walk again' after shooting
                                                                          'I was considering my options after a break-up': Why Chantel decided to freeze her eggs
                                                                                       Men come forward alleging sexual abuse in youth detention by female worker
                                                                                         'Huge relief' as Sydney ice rink saved by $17m grant for 'total upgrade'
                                                                               NSW minister passed over female frontrunners in search for new Children's Guardian

This is using this sed statement. sed -e :a -e ‘s/^.{1,160}$/ &/;ta’ ; echo. Very simple to do.

Finally, how to print all information about your Internet-facing IP address and strip out all white space and blank lines.

[john@localhost ~]$ curl -s ifconfig.me/all | sed 's/.*://;/^[[:space:]]*$/d' ; echo
 203.241.10.128
 unavailable
 curl/7.76.1
 54586
 GET
 */*
 1.1 google
 203.241.10.128, 34.160.111.145,35.191.10.228

This does the trick.

This statement, sed ‘s/.://;/^[[:space:]]$/d’. This is how to have multiple expressions in one sed one-liner. The semicolon separates the multiple expressions.

Just like this: ‘s/.:// ; /^[[:space:]]$/d’. This is not too hard to do, this makes more sense than using a far too complicated sed statement.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.