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 11-24-2004, 01:18 PM   #1
scilec
LQ Newbie
 
Registered: Oct 2004
Distribution: Redhat Enterprise 3
Posts: 7

Rep: Reputation: 0
Question 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
 
Old 11-24-2004, 08:53 PM   #2
wapcaplet
Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
I don't know if this will help removing the extra space, but 'sed' would probably be better for removing the TTITLEXX stuff. Try this instead of awk:

Code:
sed -e "s/TTITLE$titlenum=//g"
sed is useful for doing all sorts of regular expression search-and-replace. Also, using the '-n' option to 'echo' in that same line may help suppress the extra space (-n tells echo not to print a newline at the end).
 
Old 11-25-2004, 04:14 PM   #3
LasseW
Member
 
Registered: Oct 2004
Distribution: Fedora 7, OpenSuse 10.2
Posts: 108

Rep: Reputation: 15
When standard output is redirected to a variable, linefeeds are converted to spaces. So you need to remove the linefeeds before assigning the string to a variable. Here's one solution:

tn1=$(grep "TTITLE$titlenum=" $1 | cut -d\= -f2 | tr -d "\n")
 
Old 11-25-2004, 06:44 PM   #4
krock923
Member
 
Registered: Jul 2004
Posts: 171

Rep: Reputation: 30
Hello, that looks a lot like the result from a cddb query. Are you making an encoding script? If so, I've been working on one for a few months and i'd be honored if you wanted to try it out.
 
  


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
Saving a text file as a variable and reading it every second in java script mrobertson Programming 4 03-26-2007 08:25 PM
BASH script Ė reading and writing variables to a separate file morrolan Programming 10 09-20-2005 07:45 AM
Reading a conf file from a BASH script dinolinux Programming 5 08-03-2005 04:18 AM
Bash script - reading from text file twantrd Programming 4 11-24-2004 12:38 AM
reading file, bash script marri Programming 3 11-15-2004 09:13 AM


All times are GMT -5. The time now is 09:22 PM.

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