Shell Practice: Introduction to the sed stream editor

Slashdot it! Delicious Share on Facebook Tweet! Digg!

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
Figure 22: Handling variables in Bash scripts.

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

  1. Project page for sed: http://sed.sourceforge.net/

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $0.99
(incl. VAT)

Buy Ubuntu User

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content