September 29, 2008

UNIX case study - 5

Read, change and consolidate files


MERGERECORDFILE=/root/mergerecord.$(date +%m%d%y)
RECORDFILELIST=/root/branch_records.lst

while read RECORDFILENAME
do
sed s/$/' '$(basename $RECORDFILENAME)/g $RECORDFILENAME >> $MERGERECORDFILE
# sed s/$/$'test'/g input.txt
done < $RECORDFILELIST

Read the list of files saved in "branch_records.lst" file. Append the filename to the end of each line and save the entire text output to Merge Record file. The following example does the same thing using for - in loop.

MERGERECORDFILE=/root/mergerecord.$(date +%m%d%y)
#RECORDFILELIST=/root/branch_records.lst
RECORDFILELIST=(`ls i*.txt`)

for RECORDFILENAME in ${RECORDFILELIST[@]}
do
echo $RECORDFILENAME
sed s/$/' '$(basename $RECORDFILENAME)/g $RECORDFILENAME >> $MERGERECORDFILE
done

If you want to add the field delimiter then add this...
FD=:
and change the sed command to...
sed s/$/${FD}$(basename $RECORDFILENAME)/g $RECORDFILENAME >> $MERGERECORDFILE

_____

Another way to achieve the same result is to save the standard out to file descriptor.

$outfile = '/root/testinput.txt'
$INFILE = '/root/input.txt'
> $outfile
exec 4<&1
exec 1>$OUTFILE
for LINE in `cat $INFILE`
do
echo "$LINE"
:
done
exec 1<&4
exec 4>&-

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.