Shell Practice: Introduction to the sed stream editor
|
Shell Variables
If a shell variable needs to be resolved, you need to enclose the statements in double quotes (" ) instead of single quotes (' ). The little shell script in Listing 3 shows how to handle variables. It searches through the sample file and outputs the matching lines. Figure 22 shows the result.
Listing 3
searchString.sh
01 #! /bin/sh 02 echo -n "Enter search string: ";read sstring 03 cat textdata.txt | sed -n "/$sstring/"p
Conclusion
With sed, you can execute complex text manipulation without user intervention. Its cryptic syntax might seem cumbersome at first, which is why building scripts bit by bit is a great option.
Infos
- Project page for sed: http://sed.sourceforge.net/
Buy this article as PDF
Express-Checkout as PDF
Pages: 6
Price $0.99
(incl. VAT)
(incl. VAT)