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
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.
Management Strategies for the Cloud Revolution: How Cloud Computing Is Transforming Business and Why You Can't Afford to Be Left Behind by Charles Babcock(4130)
Ego Is the Enemy by Ryan Holiday(3991)
Offensive Shellcode from Scratch by Rishalin Pillay(3666)
Exploring Deepfakes by Bryan Lyon and Matt Tora(3251)
Robo-Advisor with Python by Aki Ranin(3028)
Learning C# by Developing Games with Unity 2021 by Harrison Ferrone(2871)
Speed Up Your Python with Rust by Maxwell Flitton(2849)
Liar's Poker by Michael Lewis(2811)
Agile Security Operations by Hinne Hettema(2804)
Linux Command Line and Shell Scripting Techniques by Vedran Dakic and Jasmin Redzepagic(2803)
OPNsense Beginner to Professional by Julio Cesar Bueno de Camargo(2802)
Extreme DAX by Michiel Rozema & Henk Vlootman(2790)
Essential Cryptography for JavaScript Developers by Alessandro Segala(2740)
Elevating React Web Development with Gatsby by Samuel Larsen-Disney(2729)
Python for ArcGIS Pro by Silas Toms Bill Parker(2644)
AI-Powered Commerce by Andy Pandharikar & Frederik Bussler(2642)
Cryptography Algorithms by Massimo Bertaccini(2627)
Incident Response Techniques for Ransomware Attacks by Oleg Skulkin(2626)
Distributed .NET with Microsoft Orleans by Bhupesh Guptha Muthiyalu Suneel Kumar Kunani(2625)