sh: Inserting tabs and moving text to 1 line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sh: Inserting tabs and moving text to 1 line
# 1  
Old 07-25-2007
Question sh: Inserting tabs and moving text to 1 line

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

# 2  
Old 07-25-2007
Quote:
Originally Posted by 00000008
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.

Code:
...
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
...

# 3  
Old 07-25-2007
Data lorcan please have a look

Quote:
Originally Posted by lorcan
Code:
...
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
# 4  
Old 07-25-2007
Code:
$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?

Last edited by lorcan; 07-25-2007 at 08:49 AM..
# 5  
Old 07-25-2007
Data echo and loop not working with script?

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.
# 6  
Old 07-25-2007
Sorry there is a small change if you are using bash shell.
Please proceed with this code

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

# 7  
Old 07-26-2007
Computer thanks a mil lorcan

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
}
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Echo a colored text with tabs

I have the line below to echo values with tab between them. The text is also colored, however, some\t does not work. The output of this one below will have the first two \t not working. echo "\033}\t$time\t$end\t$day\t$score\033 This one below will have all the \t working but will also... (4 Replies)
Discussion started by: erin00
4 Replies

2. Shell Programming and Scripting

[BASH] read 'line' issue with leading tabs and virtual line breaks

Heyas I'm trying to read/display a file its content and put borders around it (tui-cat / tui-cat -t(ypwriter). The typewriter-part is a 'bonus' but still has its own flaws, but thats for later. So in some way, i'm trying to rewrite cat using bash and other commands. But sadly it fails on... (2 Replies)
Discussion started by: sea
2 Replies

3. Shell Programming and Scripting

Grab line regardless of if it ends with tabs or spaces

so i have a data file that has various lines which may or may not end with spaces or tabs. data.file: , \t \t {sample} <spaces> <spaaces> several more spaces.... {"resemble"}, <nospaces> Command i'm using: sed -n 8p data.file | egrep "\],$|\],\ $" or egrep "\],$|\],\ $"... (1 Reply)
Discussion started by: SkySmart
1 Replies

4. Shell Programming and Scripting

Want to remove a line feed depending on number of tabs in a line

Hi! I have been struggling with a large file that has stray end of line characters. I am working on a Mac (Lion). I mention this only because I have been mucking around with fixing my problem using sed, and I have learned far more than I wanted to know about Unix and Mac eol characters. I... (1 Reply)
Discussion started by: user999991
1 Replies

5. Shell Programming and Scripting

Commenting a specific line and inserting a new line after commented line.

Hello All, I have following file contents cat file #line=aaaaaa #line=bbbbbb #line=cccccc #line=dddddd line=eeeeee #comment=11111 #comment=22222 #comment=33333 #comment=44444 comment=55555 Testing script Good Luck! I would like to comment line line=eeeeee and insert a new line... (19 Replies)
Discussion started by: manishdivs
19 Replies

6. UNIX for Dummies Questions & Answers

Rewriting line with tabs creating problem

Hi I have an input file which have random file in between.I have to manipulate each line and replace the character from position 5-10 with XXXXXX. But when I am writing this to on output file the tabs in between gets converted to normal space. Input file : 14207531131040896334R108 ... (4 Replies)
Discussion started by: akashtcs
4 Replies

7. Shell Programming and Scripting

Problem inserting text into file after specific line

this is utterly embarassing :( after posting here i revisited my files and found that when i used "vi" instead of a gui based editor, i suddenly found that the indentations were in fact wrong :( sorry about this :( (0 Replies)
Discussion started by: mocca
0 Replies

8. Shell Programming and Scripting

Insert Tabs / Indent text

Hi, i need replace the slash (/) with a newline (\n) and a tab (\t). With 'find -type f' in a folder i got this output: ./1999/01/file1 ./1999/01/file2 ./1999/02/file1 ./2000/04/file1 ./2000/04/file2 ./2000/04/file3 ./2000/04/file4 ./2000/06/file1 ./2000/06/file2 ./2000/06/file3... (8 Replies)
Discussion started by: Tonda
8 Replies

9. UNIX for Dummies Questions & Answers

inserting a text after a certain word in text files

I need insert a text file content in other text file after certain word like insert content of tagfav.txt in all my html files after the <head> tag. Anyone can help me? (2 Replies)
Discussion started by: ItaloAG
2 Replies

10. Shell Programming and Scripting

print pattern line +2 without tabs or brackets

This works, but its messy. Any suggestions for a elegant solution? :- me@myserver $ head zoneAttachOutfile These packages installed on the source system are inconsistent with this system: SUNWsmbac: version mismatch (11.9.0,REV=2002.03.02.00.35) ... (2 Replies)
Discussion started by: repudi8or
2 Replies
Login or Register to Ask a Question