Computing.Net > Forums > Unix > Pass environment variables to sed

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Pass environment variables to sed

Reply to Message Icon

Name: PaulDavidSumner
Date: April 26, 2006 at 07:36:34 Pacific
OS: Windows XP Build 2600
CPU/Ram: Pentium 4 2.26 GHz/256 MB
Product: Dell GX260 Optiplex
Comment:

Is it possible to pass a Unix environment variable to sed? The example script I have included shows what I am trying to do and the header explains why. I am running this script on Solaris KSh.

#################################################
# Script to merge InvoiceHeaders.txt with
# InvoiceLines.txt to make MergedFile.txt as
# follows:
#
# Print first line of InvoiceHeaders.txt
# Print first line of InvoiceLines.txt
# Print second line of InvoiceHeaders.txt
# Print second line of InvoiceLines.txt
# Print third line of InvoiceHeaders.txt
# Print third line of InvoiceLines.txt
# ...
# and so on...
# ...
# Print last line of InvoiceHeaders.txt
# Print last line of InvoiceLines.txt
# STOP
#
# Assumptions:
# 1. InvoiceHeaders.txt and InvoiceLines.txt
# contain exactly the same number of lines.
# 2. Ordering of lines within InvoiceHeaders.txt
# and InvoiceLines.txt is already correctly set
# up so this simple merge will put the right
# invoice line below its invoice header.
#################################################

clear
rm -f MergedFile.txt

#################################################
# Get the line count from one file - it will be
# the same for the other.
#################################################
lineCount=`wc -l InvoiceHeaders.txt \
| awk '{printf $1}'`
echo "lineCount = $lineCount"

counter=1

#################################################
# Print lines as described in header.
# PROBLEM - HOW TO GET SED TO RECEIVE THE VALUE
# $counter ON EACH LOOP.
#################################################
while [ `expr $counter` -le `expr $lineCount` ]
do
echo "counter = $counter"

sed -n '$counter p' InvoiceHeaders.txt \
>> MergedFile.txt
sed -n '$counter p' InvoiceLines.txt \
>> MergedFile.txt
((counter += 1))
done



Sponsored Link
Ads by Google

Response Number 1
Name: lchi2000g
Date: April 26, 2006 at 08:25:28 Pacific
Reply:

use the double quotes instead of the single quotes in sed command line


Luke Chi


0

Response Number 2
Name: PaulDavidSumner
Date: April 26, 2006 at 08:34:55 Pacific
Reply:

Yes, that works - thankyou!


0

Response Number 3
Name: lchi2000g
Date: April 26, 2006 at 09:30:48 Pacific
Reply:

Team work!

Luke Chi


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Pass environment variables to sed

replace variable to 7th field in fi www.computing.net/answers/unix/replace-variable-to-7th-field-in-fi/8142.html

replace in sed www.computing.net/answers/unix/replace-in-sed/8472.html

how to pass a variable from unix www.computing.net/answers/unix/how-to-pass-a-variable-from-unix-/7649.html