Posted: . At: 9:24 AM. This was 5 years ago. Post ID: 13443
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.


Fun with sed on Linux.


The sed command can be used to reverse text. This is a very interesting feature.

Provide a text string, and it will be reversed.

jason@jason-Virtual-Machine:~$ sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/./Hello. /' <<< 'This is a long sentence in reverse. This is a lot of fun.'
Hello. .nuf fo tol a si sihT .esrever ni ecnetnes gnol a si sihT

Or, provide a sentence in reverse, and it will be the right way around.

jason@jason-Virtual-Machine:~$ sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/./Hello. /' <<< '.nuf fo tol a si sihT .esrever ni ecnetnes gnol a si sihT'
Hello. This is a long sentence in reverse. This is a lot of fun.

Get a string with information about the current date and then reverse it.

jason@jason-Virtual-Machine:~$ sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/./: /' <<< `calendar | head -n 1`
: 3381 ,elpoep 003 fo egalliv a sa detaroprocni ogacihC  01 guA

Another very useful example, this is a simpler way to reverse text.

jason@jason-Virtual-Machine:~$ calendar | head -n 1 | rev
3381 ,elpoep 003 fo egalliv a sa detaroprocni ogacihC    01 guA

This is a similar trick, the tac command prints a file or STDIN in reverse, the last line is printed first. This could be very useful for reversing the order of a large text file.

jason@jason-Virtual-Machine:~$ calendar | tac
Aug 11* День строителя
Aug 11* Құрылысшы күні
Aug 11  Zsuzsanna, Tiborc
Aug 11* En août et vendanges, il n'y a ni fêtes ni dimanches.
Aug 11  Aujourd'hui, c'est la St(e) Philomène.
Aug 11  Bonne fête aux Claire !
Aug 11  Oath of Baudouin, son of Leopold III, 1950
Aug 11* Parshat Re'eh
Aug 11  King Hussein's Accession to the Throne in Jordan
Aug 11  Independence Day in Chad
Aug 11  Heroes Day (2 days) in Zimbabwe
Aug 11  Perseid meteor shower (look north; three days)
Aug 11  France Ends War in Indochina, 1954
Aug 11  Dog days end
Aug 10  Lõrinc
        Y perd la graine et puis le temps.
Aug 10  Qui sème à la saint Laurent
Aug 10  N'oubliez pas les Laurent !
Aug 10  Peter Pentchev <roam@FreeBSD.org> born in Sofia, Bulgaria, 1977
Aug 10  Julio Merino <jmmv@FreeBSD.org> born in Barcelona, Spain, 1984
Aug 10  Puck Faire
Aug 10  Ian Anderson (Jethro Tull) is born in Edinburgh, Scotland, 1947
Aug 10  Independence Day in Ecuador
Aug 10  US and Panama agree to transfer the canal in the year 2000, 1977
Aug 10  Chicago incorporated as a village of 300 people, 1833

To print the text file or STDIN in randomised order for each line do it like this.

jason@jason-Virtual-Machine:~$ calendar | shuf
Aug 11  Bonne fête aux Claire !
Aug 10  Peter Pentchev <roam@FreeBSD.org> born in Sofia, Bulgaria, 1977
Aug 11* День строителя
Aug 10  N'oubliez pas les Laurent !
Aug 11  Dog days end
Aug 11* Құрылысшы күні
Aug 10  Chicago incorporated as a village of 300 people, 1833
Aug 11  Perseid meteor shower (look north; three days)
Aug 11  King Hussein's Accession to the Throne in Jordan
Aug 10  Ian Anderson (Jethro Tull) is born in Edinburgh, Scotland, 1947
Aug 11  France Ends War in Indochina, 1954
Aug 11  Heroes Day (2 days) in Zimbabwe
Aug 11  Zsuzsanna, Tiborc
Aug 10  Puck Faire
Aug 10  Qui sème à la saint Laurent
Aug 11* En août et vendanges, il n'y a ni fêtes ni dimanches.
Aug 11* Parshat Re'eh
Aug 10  Independence Day in Ecuador
Aug 11  Oath of Baudouin, son of Leopold III, 1950
Aug 11  Independence Day in Chad
Aug 10  US and Panama agree to transfer the canal in the year 2000, 1977
        Y perd la graine et puis le temps.
Aug 11  Aujourd'hui, c'est la St(e) Philomène.
Aug 10  Lõrinc
Aug 10  Julio Merino <jmmv@FreeBSD.org> born in Barcelona, Spain, 1984

These tips should be very helpful for randomizing the contents of a text file. And the rev command also works when piping a randomised text file to it.


Leave a Comment

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