The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
What is the output of echo * shailja UNIX for Dummies Questions & Answers 4 06-29-2007 06:27 AM
piping output to echo A1977 Shell Programming and Scripting 3 11-01-2006 04:58 AM
Store output and Echo meyerder Shell Programming and Scripting 2 05-14-2006 06:44 AM
Weird bootlist output davew1099 AIX 4 10-12-2005 06:52 AM
Passing output of sed/echo to a variable donflamenco Shell Programming and Scripting 11 07-13-2005 07:08 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-23-2008
Registered User
 

Join Date: Mar 2008
Posts: 13
Talking weird echo output?

Code:
#!/bin/bash

INPUT=$1
if [ "$INPUT" -lt "10" ]
then
        INPUT=0$1
        TRACKNUMBER=$INPUT
fi
TRACKNUMBER=$INPUT
echo "Track Number:" $TRACKNUMBER


if [ ! -e "split-track${TRACKNUMBER}.wav" ]
then
        echo "File Does Not Exist!: split-track"${TRACKNUMBER}".wav"
        exit 0
fi

CUEFILE="$2"

CDTITLE=`less "$CUEFILE"|grep -B10 FILE|grep TITLE|sed 's/TITLE\ "//'|sed 's/^[ \t]*//'|sed '
s/"//'`
#|sed 's/ /\\ /g'`
CDPERFORMER=`less "$CUEFILE"|grep -B10 FILE|grep PERFORMER|sed 's/PERFORMER\ "//'|sed 's/^[ \
t]*//'|sed 's/"//'`
TRACKTITLE=`less "$CUEFILE" |grep -A4 TRACK\ $TRACKNUMBER|grep TITLE|sed 's/TITLE\ "//'|sed '
s/^[ \t]*//'|sed 's/"//'`
PERFORMER=`less "$CUEFILE" |grep -A4 TRACK\ $TRACKNUMBER|grep PERFORMER|sed 's/PERFORMER\ "//
'|sed 's/^[ \t]*//'|sed 's/"//'`

echo "CD Performer: $CDPERFORMER"
echo "CD Title: $CDTITLE"
echo "Track Title: $TRACKTITLE"
echo "Track Performer: $PERFORMER"


echo $CDPERFORMER $CDTITLE
echo $TRACKNUMBER $TRACKTITLE
output:
Quote:
$ ../trackname.sh 2 Santana\ -\ Abraxas.cue
Track Number: 02
CD Performer: Santana
CD Title: Abraxas
Track Title: Black Bagic Woman - Gypsy Queen
Track Performer: Santana
Abraxas
02 Black Bagic Woman - Gypsy Queen
why doesn't the second to last "echo" command work properly?

some more oddity, if I replace the last two echo commands with:
Code:
echo "\"$CDPERFORMER\""
echo "\"$CDTITLE\""
echo "\"$TRACKNUMBER\""
echo "\"$TRACKTITLE\""
I get:
Quote:
"Santana
"Abraxas
"02"
"Black Bagic Woman - Gypsy Queen
or with:
Code:
echo "\"$CDPERFORMER\" "
echo "\"$CDTITLE\" "
echo "\"$TRACKNUMBER\" "
echo "\"$TRACKTITLE\" "
I get:
Quote:
" antana
" braxas
"02"
" lack Bagic Woman - Gypsy Queen
When all I want is an output echo (or really a variable with the following so i can use it in a mv command):
"Santana - Abraxas - 02 - Black Bagic Woman - Gypsy Queen"

anyone know what I'm doing wrong? thanks!!
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-23-2008
redhead's Avatar
Registered User
 

Join Date: Feb 2002
Location: Denmark
Posts: 46
Have you tried something simple like:
Code:
echo "$CDPERFORMER - $CDTITLE - $TRACKNUMBER - $TRACKTITLE"
Reply With Quote
  #3 (permalink)  
Old 03-23-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,928
Quote:
Code:
echo "\"$CDPERFORMER\" "
echo "\"$CDTITLE\" "
echo "\"$TRACKNUMBER\" "
echo "\"$TRACKTITLE\" "
I get:
This funny board software didn't even know how to quote the output, so I take that as additional indication that the problem is this: you have DOS carriage returns in the output.

I really wonder about your use of less inside a script. Does it bring some benefit over simply reading the file?

Quote:
Code:
CDPERFORMER=`less "$CUEFILE"|grep -B10 FILE|grep PERFORMER|sed 's/PERFORMER\ "//'|sed 's/^[ \
t]*//'|sed 's/"//'`
With all due respect, you should probably read up on sed scripting. grep | sed is almost always redundant, since sed is basically a grep on stereoids.

Code:
CDPERFORMER=`grep -B10 FILE "$CUEFILE" |
    tr -d '\015' |
    sed -n 's/^[ 	][ 	]*PERFORMER[ 	][ 	]*"\([^"]*\)*"/\1/p'`
... assuming that your repeated sed invocations were meant to remove any PERFORMER tag, remove any leading whitespace before it, and trim the double quotes around the value (and throwing in the removal of any DOS carriage returns as a bonus). Probably the first grep could also be avoided if you familiarize yourself with sed some more, but this is how far I get without even reading the manual page. (Granted, the real magic is in being able to write a proper regular expression for any given problem.) ... That's a tab and a space between the square brackets, by the way.
Reply With Quote
  #4 (permalink)  
Old 03-24-2008
Registered User
 

Join Date: Mar 2008
Posts: 13
Quote:
Originally Posted by redhead View Post
Have you tried something simple like:
Code:
echo "$CDPERFORMER - $CDTITLE - $TRACKNUMBER - $TRACKTITLE"
yes, sorry, forgot to show what that output, which was basically the tracknumber and the tracktitle, ie:
Quote:
02 - Black Bagic Woman - Gypsy Queen
(from memory, i believe there were some spaces before the 02, but nothing else if anything).
Reply With Quote
  #5 (permalink)  
Old 03-24-2008
Registered User
 

Join Date: Mar 2008
Posts: 13
Post

Quote:
Originally Posted by era View Post
This funny board software didn't even know how to quote the output, so I take that as additional indication that the problem is this: you have DOS carriage returns in the output.
I'm running linux, why would I have DOS carriage returns?

Quote:
I really wonder about your use of less inside a script. Does it bring some benefit over simply reading the file?

With all due respect, you should probably read up on sed scripting. grep | sed is almost always redundant, since sed is basically a grep on stereoids.
yea, except I don't really know how to use sed outside of how I've used it.

Quote:
Code:
CDPERFORMER=`grep -B10 FILE "$CUEFILE" |
    tr -d '\015' |
    sed -n 's/^[ 	][ 	]*PERFORMER[ 	][ 	]*"\([^"]*\)*"/\1/p'`
... assuming that your repeated sed invocations were meant to remove any PERFORMER tag, remove any leading whitespace before it, and trim the double quotes around the value (and throwing in the removal of any DOS carriage returns as a bonus). Probably the first grep could also be avoided if you familiarize yourself with sed some more, but this is how far I get without even reading the manual page. (Granted, the real magic is in being able to write a proper regular expression for any given problem.) ... That's a tab and a space between the square brackets, by the way.
repeated sed was indeed to do basically all that.
yea, the first grep could probably be avoided, but I don't know how exactly.

still doesn't explain why my desired output is not working though.
of course, any help with sed would be appreciated. I've tried reading some of the online FAQ's, etc. but they all seem to assume some knowledge which I am not privy to...
Reply With Quote
  #6 (permalink)  
Old 03-24-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,928
Did you try trimming the assumed carriage returns from the file? If something is fetching data from the Internet then the protocol is probably using carriage returns, and they might survive into your files (depending on the client which fetches them).

There's a book about awk & sed from O'Reilly, it's very old but also very much worth reading. Also most Unix intro books have a few chapters dedicated to regular expression tools in general, and grep and sed in particular, then moving on to awk. The manual page is not particularly gruesome, either, although of course it's primarily intended as a reference, not a tutorial.
Reply With Quote
  #7 (permalink)  
Old 03-24-2008
Registered User
 

Join Date: Mar 2008
Posts: 13
Quote:
Originally Posted by era View Post
Did you try trimming the assumed carriage returns from the file? If something is fetching data from the Internet then the protocol is probably using carriage returns, and they might survive into your files (depending on the client which fetches them).
well that was it, the original source of the information was from a dos file. I ran dos2unix on it and ran the script and it worked fine.


Quote:
Originally Posted by era View Post
There's a book about awk & sed from O'Reilly, it's very old but also very much worth reading. Also most Unix intro books have a few chapters dedicated to regular expression tools in general, and grep and sed in particular, then moving on to awk. The manual page is not particularly gruesome, either, although of course it's primarily intended as a reference, not a tutorial.
I'll have look into it, thanks for the help!
Reply With Quote
Google UNIX.COM
Reply

Tags
linux

Thread Tools
Display Modes




All times are GMT -7. The time now is 09:49 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0