Science and technology

How I take advantage of the Linux sed command to automate file edits

When I take advantage of the Linux command line, whether or not I’m writing a brand new program on my desktop pc or managing a web site on my internet server, I usually must course of textual content information. Linux supplies highly effective instruments that I leverage to get my work achieved. I continuously use sed, an editor that may modify textual content based on a sample.

sed stands for stream editor, and it edits textual content in a file and prints the outcomes. One method to make use of sed is to determine a number of occurrences of 1 string in a file and change them with a unique string. You can use sed to course of textual content information to a seemingly infinite diploma, however I’d wish to share just a few methods I take advantage of sed to assist me handle information.

Search and change textual content in a file on Linux

To use sed, it’s worthwhile to use a common expression. An everyday expression is a set of particular characters that outline a sample. My most frequent instance of utilizing sed is changing textual content in a file. The syntax for changing textual content seems to be like this: s/originaltext/newtext/. The s tells sed to carry out textual content alternative or swap occurrences of textual content. Provide the unique textual content and new textual content between slashes.

This syntax will solely change the primary prevalence of originaltext on every line. To change each prevalence, even when the unique textual content seems greater than as soon as on a line, append g to the tip of the expression. Here is an instance: s/originaltext/newtext/g.

To use this with sed, specify this common expression with the -e possibility:

$ sed -e 's/originaltext/newtext/g'

For instance, for example I’ve a Makefile for a program referred to as recreation, which simulates Conway’s Game of Life:

.PHONY: all run clear

all: recreation

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

run: recreation
        ./recreation

clear:
        $(RM) *~
        $(RM) *.o
        $(RM) recreation

The title recreation is not very descriptive, so I’d select to rename it life. Renaming the recreation.c supply file to life.c is simple sufficient, however now I want to change the Makefile to make use of the brand new title. I can use sed to vary each prevalence of recreation to life:

$ sed -e 's/recreation/life/g' Makefile
.PHONY: all run clear

all: life

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

run: life
        ./life

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

This prints the sed output to the display, which is an efficient solution to verify if the textual content alternative will do what you need. To make these adjustments to the Makefile, first, make a backup of the file, then run sed and save the output to the unique filename:

$ cp Makefile Makefile.previous
$ sed -e 's/recreation/life/g' Makefile.previous > Makefile

If you’re assured that your adjustments are precisely what you need, use the -i or --in-place choice to edit the file in place. However, I like to recommend including a backup filename suffix like --in-place=.previous to save lots of a duplicate of the unique file in case it’s worthwhile to restore it later. It seems to be like this:

$ sed --in-place=.previous -e 's/recreation/life/g' Makefile
$ ls Makefile*
Makefile  Makefile.previous

Quoting information with sed on Linux

You can use different options of normal expressions to match particular situations of textual content. For instance, you would possibly want to switch textual content that happens at first of a line. With sed, you may match the start of a line with ^, the caret character.

One method I take advantage of “start of line” in changing textual content is once I must quote a file in an e-mail. Let’s say I need to share my Makefile in an e-mail, however I do not need to embrace it as a file attachment. Instead, I desire to “quote” the file within the physique of an e-mail, utilizing > earlier than every line. I can use the next sed command to print out an edited model to my terminal, which I can copy and paste into a brand new e-mail:

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

The s/^/>/ common expression matches the beginning of every line (^) and locations a > there. Effectively, this begins every line with the > image.

The tabs won’t present up accurately in an e-mail, however I can change all tabs within the Makefile with just a few areas by including one other common expression:

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

The t signifies a literal tab, so s/t/ /g tells sed to switch all tabs within the enter with two areas within the output.

If it’s worthwhile to apply a lot of edits to information, it can save you your -e instructions in a file and use -f to inform sed to make use of that file as a “script.” This strategy is particularly helpful if it’s worthwhile to make the identical edits continuously. I might need ready the Makefile for quoting in e-mail utilizing a script file referred to as quotemail.sed:

$ cat quotemail.sed
s/^/>/
s/t/  /g
$ sed -f quotemail.sed Makefile
>.PHONY: all run clear
>
>all: life
>
>life: life.o
>  $(CC) $(CFLAGS) -o life life.o $(LDFLAGS)
>
>run: life
>  ./life
>
>clear:
>  $(RM) *~
>  $(RM) *.o
>  $(RM) life

Learn to work with sed on Linux

sed is a good device to maintain in your Linux command-line toolkit. Explore the sed guide web page and be taught extra about the way to use it. Type man sed on the command line to get full documentation in regards to the completely different command line choices and the way to use sed to course of textual content information.

Most Popular

To Top