Networking

Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.

Applications

Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.

Data

Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.

Monitoring

Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » Commands & Shells, Text Processing

Passing shell variables to awk and sed

Submitted by on December 2, 2005 – 10:23 am One Comment

By default awk and sed do not expand shell or system variables and do not pass their own variables back to the shell. To use shell variables awk and sed statements must be included in double-quotes. Here are a few examples:

1) You have a file (words.txt) containing a list of words and their replacements as shown below.

blue red
yellow green
stormy blue
dark clear

You also have a text file (text.txt) where you need to replace the words from the list above with the corresponding alternatives.

blue flowers were growing on a yellow lawn under a dark stormy sky

Here’s a shell script to do it:

#!/bin/ksh
cat words.txt | while read LINE
do
     O=$(echo "$LINE" | awk '{print $1}')
     R=$(echo "$LINE" | awk '{print $2}')

     cat text.txt | sed "s/$O/$R/g" > tmp.txt
     mv tmp.txt text.txt
done

The resulting text.txt file would read:

red flowers were growing on a green lawn under a clear blue sky
Print Friendly, PDF & Email

One Comment »

  • Joey 01 says:

    What are some real-world applications that involve a systems of linear equations? How can the graphing display calculator assist in determining the solutions of the system?

    can you gues be very brief ?

Leave a Reply

%d bloggers like this: