LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-16-2005, 08:17 AM   #1
Zxarr
LQ Newbie
 
Registered: Sep 2005
Location: Ontario, Canada
Distribution: Fedora 8
Posts: 20

Rep: Reputation: 0
adding carriage returns


Hi there,

I have written a little bash script that takes output from certain backup commands. One of them gives me a list of tapes used. When I run this command seperately from the script, it gives me a tape by tape list, each on it's own line.

My script simply uses echo and the >> to redirect the output to a text file. But when it shows up in the text file, the carriage returns are missing and each tape shows up one after the other all on the same line.

The problem is that some backups use 1 tape, some use 8 tapes... It varies. The output of the command looks like this:

[NMT015L2] <Description> 84AC:3921:3394:FC44
[NMT133L2] <Description> 84AC:3921:3331:11AF

(There could be any number of tapes listed)

So I either need a way to grab each line seperately, or to figure out how many there are, then place carriage returns (or newlines, I'm not sure), after each.

Any help would be appreciated.

Oh, and I am using cygwin at the moment. I have to in this case.

Thanks.

Last edited by Zxarr; 09-16-2005 at 08:25 AM.
 
Old 09-16-2005, 11:49 AM   #2
jasonM
LQ Newbie
 
Registered: Apr 2004
Posts: 24

Rep: Reputation: 15
Try:

for line in `backup_command `
do
echo $line >> text_file
done

Note that the single quotes are the ones on the top left of the keyboard (don't know what their proper name is).

Last edited by jasonM; 09-16-2005 at 11:51 AM.
 
Old 09-16-2005, 02:16 PM   #3
Zxarr
LQ Newbie
 
Registered: Sep 2005
Location: Ontario, Canada
Distribution: Fedora 8
Posts: 20

Original Poster
Rep: Reputation: 0
Thanks, that's gotten me a little farther along....

used the for loop and here is what I ended up with in the log file:

[NMT156L2]
NMT156L2
5c1428cc:432193b6:0b24:000b

Odd, as the command's output is:

[NMT156L2] NMT156L2 5c1428cc:432193b6:0b24:000b

Which is what I want. Perhaps I need to extract each line and use sed to remove all the extra control characters? CR's, etc...

I am on the right track as when multiple tapes are used, they show up now.


Thanks.
 
Old 09-16-2005, 03:17 PM   #4
jasonM
LQ Newbie
 
Registered: Apr 2004
Posts: 24

Rep: Reputation: 15
Sorry, didn't notice the spaces in the output. Try this one:

set `backup_command` #again, special single quote

for line in "$@" # That is in double quotes
do
echo $line >> text_file
done

Actually, I just tried this myself and it didn't work. Never miind.

Last edited by jasonM; 09-16-2005 at 03:37 PM.
 
Old 09-16-2005, 03:56 PM   #5
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 31
This might not help with the original problem, but here's a little trick that might help get the full line:

Code:
IFS="
"

for line in `backup_command`; do
   echo $line
done
 
Old 09-18-2005, 06:23 AM   #6
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Instead of wasting time using a for loop, just quote the argument to echo and it'll preserve all whitespace, including carriage returns. Try
Code:
VAR=$(ls)
echo $VAR
echo "$VAR"
and see if you can spot the difference. KISS
 
Old 09-19-2005, 10:45 AM   #7
Zxarr
LQ Newbie
 
Registered: Sep 2005
Location: Ontario, Canada
Distribution: Fedora 8
Posts: 20

Original Poster
Rep: Reputation: 0
Thanks for all the tips! I think some of the issues are the fact I'm using cygwin and bash shell in combination with the backup commands, which are dos. Fun.

Anyway, from all the help I've come up with this:

./omnidb -session $session -media > ./tmp/session2.tmp

for line in `cat ./tmp/session2.tmp`
do
tape1=`cat ./tmp/session2.tmp | head -1 | cut -f1 -d" "`
id1=`cat ./tmp/session2.tmp | head -1 | cut -f2 -d" "`
echo "$tape1 $id1" >> ./tmp/$date.log
cat ./tmp/session2.tmp | sed '1d' > ./tmp/session3.tmp
cp ./tmp/session3.tmp ./tmp/session2.tmp
done

That works! I don't know why it doesn't work any other way, but if it ain't broke, don't fix it. Now, one last question that I've looked up around the net, but found no answers...

Is there a way, within this for loop, to let put each tape into it's own variable, then once the loop is done, be able to echo each tape out to a comma delimited file... How can you tell the echo command how many variables there are to echo?

Thanks.
 
Old 09-20-2005, 05:44 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,276

Rep: Reputation: 170Reputation: 170
seems a long-winded way of doing:

Code:
 
 
cat $INFILE | while read tape id rest 
do
  echo "$tape $id" 
done  > $OUTFILE
csv one long line or one record per line?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to remove carriage returns in a bunch of perl scripts? rebel Red Hat 4 04-12-2005 02:27 PM
Carriage Returns Thorkyl Linux - Software 7 06-28-2004 05:42 PM
XDrawString and carriage returns jpbarto Programming 1 03-25-2004 03:07 PM
Kword: How to remove carriage returns? RockyRed Linux - Newbie 1 07-21-2003 06:52 AM
Carriage Returns Trouble sancho5 Linux - General 2 08-29-2001 08:59 PM


All times are GMT -5. The time now is 01:03 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration