V
V
VN2021-06-23 10:36:19
bash
VN, 2021-06-23 10:36:19

Why is the sed script not executing?

In the book of William Shotts, there is such an example. File containing OS names, versions, and release dates (output shortened to save space). File distr.txt (tab delimiter):

SUSE    10.2    12/07/2006
Fedora  10      11/25/2008
SUSE    11.0    06/19/2008
Ubuntu  8.04    04/24/2008

the example uses a sed construct that changes the date output format from MM/DD/YYYY to YYYY-MM-DD, which runs correctly outside of the script and changes the format:
sed 's/\([0-9]\{2\}\)\/\([0-9]\{2\}\)\/\([0-9]\{4\}\)$/\3-\1-\2/' distr.txt

the output becomes like this:
SUSE    10.2    2006-07-12
Fedora  10      2008-25-11
SUSE    11.0    2008-19-06
Ubuntu  8.04    2008-24-04

The book also contains a sed script that creates a report based on the distr.txt file:
1 i\
\
Linux Distributions Report\
s/\([0-9]\{2\}\)\/\([0-9]\{2\}\)\/\([0-9]\{4\}\)$/\3-\1-\2/
y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/

This script is saved under the name distr.sed.

When executed,
sed -f distr.sed distr.txt
a report should appear like this:
Linux Distributions Report
SUSE 10.2 2006-12-07
FEDORA 10 2008-11-25
SUSE 11.0 2008-06-19
UBUNTU 8.04 2008-04-24

But for me, it does not work correctly, the dates change for me, and part of the sed script gets into the report itself
Linux Distributions Report
s/([0-9]{2})/([0-9]{2})/([0-9]{4})$/3-1-2/
SUSE    10.2    12/07/2006
FEDORA  10      11/25/2008
SUSE    11.0    06/19/2008
UBUNTU  8.04    04/24/2008

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lynn "Coffee Man", 2021-06-23
@kavabangaungava

You have an extra backslash ( \) afterReport

$ cat distr.sed 
1 i\
\
Linux Distributions Report
s/\([0-9]\{2\}\)\/\([0-9]\{2\}\)\/\([0-9]\{4\}\)$/\3-\1-\2/
y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/

$ sed -f distr.sed distr.txt 

Linux Distributions Report
SUSE    10.2    2006-12-07
FEDORA  10      2008-11-25
SUSE    11.0    2008-06-19
UBUNTU  8.04    2008-04-24

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question