Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

![]() |
![]() |
![]() |

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