Linux Command Line and Shell Scripting Techniques by Vedran Dakic and Jasmin Redzepagic

Linux Command Line and Shell Scripting Techniques by Vedran Dakic and Jasmin Redzepagic

Author:Vedran Dakic and Jasmin Redzepagic
Language: eng
Format: epub
Publisher: Packt Publishing Ltd.
Published: 2022-02-15T00:00:00+00:00


How it works…

As a command-line text replacement utility, sed requires us to explain what we want to do to it. That's the reason why the structure of sed commands seems a bit descriptive – that's just because it is. It also has a lot of options and switches, which add to the overall usability and possible usage scenarios.

The basic command structure is usually something like this:

sed (-i) 's/something/tosomething/g' filename.txt

Or, it might be like this:

sed -someoption filename.txt

Of course, sed is often used in scripts, either standalone or as a part of a serial pipeline, something like this:

command1 |( command2 |) sed …..

Whichever way we use sed, it's essential to learn at least some of its switches and settings, starting with the most commonly used ones – s and g inside sed expressions and -i as a command-line parameter.

Say we have a command such as the following:

sed (-i) 's/something/tosomething/g' filename.txt

Obviously, this has multiple important options. The -i option, as we mentioned, is all about interactive change that's going to implement our search-and-replace criteria in the original file. Without it, we are going to get results from our sed command to the screen, basically results written to the console. Options inside the quotes – s and g – are the most-used sed options, and they mean search and globally replace, that is, in the whole file.

We could do the same thing without the -i option, by doing this:

sed 's/something/tosomething/g' inputfile.txt > outputfile.txt

But, as you might imagine, this requires more typing and is generally more complicated.

The sed command-line option -n can be used to suppress output to the terminal, and that's the reason why it's used often. If we have a large text file that we're modifying and we aren't using the -i option, this might be the go-to option to use if we don't want our console filled with text data.

One more very useful option is the -f option, as it allows us to use an input sed file with replacement definitions. Say we run the following command:

sed -f seddefinitionfile.sed inputfile.txt > outputfile.txt

We create a seddefinitionfile.sed file that contains this:

#!/usr/bin/sed -f

s/something/tosomething/g

s/somethingelse/tosomethingelse/g

We can use these options to do multiple sed transformations in one command. We just need to create sed definitions in the file and use it.

The next chapter in this book is going to introduce us to the world of shell scripting – and the whole second half of the book is about shell scripting. We will get to use all the tools that we discussed up to now there, and combine them to create shell scripts, some of the most used programming-based administration tools ever. Take a short break and get ready to shell script!



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.