I trying to extract certain text from a csv file and then placing it into another csv file, but having problems getting the data to placed in one line with tab separated fields.
Basically would like to have text sent to interfaces.csv in one line seperated by tabs. As it currently places files in <new_line> with no tabs.
Some help would be great.
example of script::
------------------------------------------------------------------
#!/bin/sh
#
# <24/07/2007>
#
#
#
#
DIR=/home/files/reports
export DIR
LOGDIR=/home/me
export LOGDIR
rm $LOGDIR/interfaces.csv
rm $LOGDIR/log
for FILE in $DIR/*
do
cat $FILE | head -2 | tail -1 | cut -d'"' -f2 >> $LOGDIR/interfaces.csv
cat $FILE | head -6 | tail -1 | cut -d"," -f3,4 >> $LOGDIR/interfaces.csv
if [ $? -ne 0 ]
then
echo "$FILE FAILED!!" >> log
else
echo "$FILE COMPLETE!!" >> log
fi
done
lorcan
July 25, 2007, 5:02am
2
...
for FILE in $DIR/*
do
cat $FILE | head -2 | tail -1 | cut -d'"' -f2 | read tmpLine1
cat $FILE | head -6 | tail -1 | cut -d"," -f3,4 | read tmpLine2
echo $tmpLine1,$tmpLine2 | tr ',' '\t' >> $LOGDIR/interfaces.csv
...
lorcan:
...
for FILE in $DIR/*
do
cat $FILE | head -2 | tail -1 | cut -d'"' -f2 | read tmpLine1
cat $FILE | head -6 | tail -1 | cut -d"," -f3,4 | read tmpLine2
echo $tmpLine1,$tmpLine2 | tr ',' '\t' >> $LOGDIR/interfaces.csv
...
1) When I adjusted script as per above, my interfaces.csv file is empty.
2) The file format of the text for the one file "tmpLine2" is separated with commas (,) but the the first file is just a name and no spaces, as per below.
Thanks
as per below
"tmpLine1"
DEVICE_NAME1.ATM3/0.101-pvc-1-101-A
DEVICE_NAME2.ATM3/0.102-pvc-1-101-A
DEVICE_NAME3.ATM3/0.103-pvc-1-101-A
"tmpLine2"
81.69795990,30.23038101
181.69795990,130.23038101
281.69795990,230.23038101
lorcan
July 25, 2007, 7:16am
4
$echo DEVICE_NAME1.ATM3/0.101-pvc-1-101-A,81.69795990,30.23038101 | tr ',' '\t'
DEVICE_NAME1.ATM3/0.101-pvc-1-101-A 81.69795990 30.23038101
It works for me. Try removing the redirection to the interfaces.csv and see if the lines are printed in your screen/Standard output.
In your DIR path do you have directories?
I can use the echo from command line and it works, but does not work within script, files are empty. I have attached files with script for you to see.
Seems to loose the stored variables when running the loop.
lorcan
July 25, 2007, 9:52am
6
Sorry there is a small change if you are using bash shell.
Please proceed with this code
#!/bin/sh # # <24/07/2007>
#
# Script to make my life easier for reports
#
#
# This will copy all the network interfaces with bandwidth in and bandwidth out values into CSV file
DIR=/home/e/reports
export DIR
LOGDIR=/home/e/files
export LOGDIR
rm $LOGDIR/interfaces.csv
rm $LOGDIR/log
for FILE in $DIR/*
do
tmpLine1=`cat $FILE | head -2 | tail -1 | cut -d'"' -f2 `
tmpLine2=`cat $FILE | head -6 | tail -1 | cut -d',' -f3,4`
# cat $FILE | head -2 | tail -1 | cut -d'"' -f2 | read tmpLine1
# cat $FILE | head -6 | tail -1 | cut -d',' -f3,4 | read tmpLine2
echo $tmpLine1,$tmpLine2 | tr ',' '\t' >> $LOGDIR/interfaces.csv
echo $tmpLine1 > $LOGDIR/t1
echo $tmpLine2 > $LOGDIR/t2
if [ $? -ne 0 ]
then
echo "$FILE FAILED!!" >> log
else
echo "$FILE COMPLETE!!" >> log
fi
done
I was having problems with the quotations (bash), so I added the brackets.
Thanks lorcan for your assistance
#!/bin/sh
# <26-07-2007>
LOGDIR=/home/e/files
DIR=/home/e/reports
export LOGDIR DIR
rm $LOGDIR/interfaces.csv 2> /dev/null
rm $LOGDIR/log 2> /dev/null
for FILE in $DIR/D*
{
tmpLine1=`cat $FILE | head -2 | tail -1 | cut -d'"' -f2`
tmpLine2=`cat $FILE | head -6 | tail -1 | cut -d',' -f3,4`
echo $tmpLine1,$tmpLine2 | tr ',' '\t' >> $LOGDIR/interfaces.csv
if [ $? -ne 0 ]
then
echo "$FILE FAILED !!" >> $LOGDIR/log
else
echo "$FILE COMPLETE !!" >> $LOGDIR/log
fi
}