LQ Newbie
Registered: Oct 2004
Distribution: Redhat Enterprise 3
Posts: 7
Rep:
|
Need help reading text file in bash script
Hi,
I'm working on a bash script that collects some data from text files, manipulates it, and saves it to a new file. My problem is that my script is not behaving the way I would expect it to. Here's a sample from the data file:
TTITLE15=Major Sample - Dutty Behaviour
TTITLE16=Frassman - Bling Bling
TTITLE17=Alley Cat, Capleton, Hawkeye, Madd Anju, Mad Cobra, Degree, E
TTITLE17=lephant Man, Lexxus, Red Rat, Kiprich, Frassman, Delon, Mr. V
TTITLE17=egas
TTITLE18=Gangster Fun With The Steady Ernest Horns/Shook Me All Night
TTITLE18=Long [live]
Note that while title 15 and title 16 each occupy one line, title 17 is split into 3 lines and title 18 is split into two lines.
My goal is to print each title on a single line. So, the above file should be processed as the following:
Major Sample - Dutty Behaviour
Frassman - Bling Bling
Alley Cat, Capleton, Hawkeye, Madd Anju, Mad Cobra, Degree, Elephant Man, Lexxus, Red Rat, Kiprich, Frassman, Delon, Mr. Vegas
Gangster Fun With The Steady Ernest Horns/Shook Me All Night Long [live]
The chunk of bash script I'm using to do this looks like:
#!/bin/bash
#
# $1 is the name of the file being parsed.
titlenum=0
# parse the lines of file $1 containing track title information.
while [ -n "$(grep "TTITLE$titlenum=" $1)" ]
do
# tan will be set to "TTITLE0, TTITLE1, TTITLE2, etc.
tan=$(grep "TTITLE$titlenum=" $1)
# Get track name and concatinate if it spills onto more than one
# line in the source file.
#
tn1=$(echo $tan | awk -F"TTITLE$titlenum=" '{print $2$3$4}')
echo $tn1 >> trackfile.txt
let titlenum=titlenum+1
done
exit 0
My problem is that this script will output the following to trackfile.txt:
Major Sample - Dutty Behaviour
Frassman - Bling Bling
Alley Cat, Capleton, Hawkeye, Madd Anju, Mad Cobra, Degree, E lephant Man, Lexxus, Red Rat, Kiprich, Frassman, Delon, Mr. V egas
Gangster Fun With The Steady Ernest Horns/Shook Me All Night Long [live]
Note the extra space in "Elephant" and "Vegas". This is happening becuase these words are split between two different lines in the source file. However, if the last character of a line in the source file is a space, such as in "Night ", the lines are combined properly without adding an extra space.
QUESTION:
How can I parse a source file like this so that titles spanning multiple lines are properly combined regardless of whether the last character in each line in a space or a letter?
Thanks,
Steve
|