Quoting text with ‘sed’

0

sed is a very useful tool that makes it easy to automate text file edits from the command line. You can use other features of regular expressions to match specific instances of text, such as to “quote” text files to include them in the body of an email.

Matching a line

For example, you might need to replace text that occurs at the start of a line. With sed, you can match the beginning of a line with ^, the caret character. One way I use “start of line” in replacing text is when I need to quote a file in an email.

Let’s say I want to share my Makefile with someone via email, but I don’t want to include it as a file attachment.

.PHONY: all run clean

all: life

life: life.o
       $(CC) $(CFLAGS) -o life life.o $(LDFLAGS)

run: life
       ./life

clean:
       $(RM) *~
       $(RM) *.o
       $(RM) life

Instead, I prefer to “quote” the file in the body of an email, using > before each line. I can use the following sed command to print out an edited version to my terminal, which I can copy and paste into a new email:

$ sed -e 's/^/>/' Makefile
>.PHONY: all run clean
>
>all: life
>
>life: life.o
>       $(CC) $(CFLAGS) -o life life.o $(LDFLAGS)
>
>run: life
>       ./life
>
>clean:
>       $(RM) *~
>       $(RM) *.o
>       $(RM) life

The s/^/>/ regular expression matches the start of each line (^) and places a > there. Effectively, this starts each line with the > symbol.

Replacing tabs

The tabs might not show up correctly in an email, but I can replace all tabs in the Makefile with a few spaces by adding another regular expression:

$ sed -e 's/^/>/' -e 's/\t/  /g' Makefile
>.PHONY: all run clean
>
>all: life
>
>life: life.o
>  $(CC) $(CFLAGS) -o life life.o $(LDFLAGS)
>
>run: life
>  ./life
>
>clean:
>  $(RM) *~
>  $(RM) *.o
>  $(RM) life

The \t indicates a literal tab, so s/\t/ /g tells sed to replace all tabs in the input with two spaces in the output.

Multiple edits at once

If you need to apply lots of edits to files, you can save your -e commands in a file and use -f to tell sed to use that file as a “script.” This approach is especially useful if you need to make the same edits frequently. I might have prepared the Makefile for quoting in email using a script file called quotemail.sed:

s/^/>/
s/\t/  /g

Then I can use the script like this:

$ sed -f quotemail.sed Makefile
>.PHONY: all run clean
>
>all: life
>
>life: life.o
>  $(CC) $(CFLAGS) -o life life.o $(LDFLAGS)
>
>run: life
>  ./life
>
>clean:
>  $(RM) *~
>  $(RM) *.o
>  $(RM) life

sed is a great tool to keep in your Linux command-line toolkit. Explore the sed manual page and learn more about how to use it. Type man sed at the command line to get complete documentation about the different command line options and how to use sed to process text files.

This article is adapted from How I use the Linux sed command to automate file edits by Jim Hall, and is republished with the author’s permission.

Leave a Reply