LinuxQuestions.org
Register a domain and help support LQ
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-29-2004, 04:25 PM   #1
scilec
LQ Newbie
 
Registered: Oct 2004
Distribution: Redhat Enterprise 3
Posts: 7

Rep: Reputation: 0
Need help parsing text file


Hi,

I'm writing a script that parses test files and in some cases, combines several lines into one. For example, the lines:

TTITLE1=There has go
TTITLE1=t to be a better way.

Would be output as "There has got to be a better way"

This is fine. The problem I have is if the last character is a space. For example, if I have the lines:

TTITLE2=You shook me all night
TTITLE2=long.

Even though there is a space after "night" in the file, my output comes out as:

You shook me all nightlong.

QUESTION:
How do I parse a text file such that spaces at the end of lines are recognized and retained???

Thanks in advance!

-Steve
 
Old 11-29-2004, 04:28 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,386

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
probably just a case of wrapping your variable in quote marks, but as you've not even gievn us the code you're working on...
 
Old 11-29-2004, 04:29 PM   #3
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
Putting quotes around the text should work:
title="a string ending with a space "
 
Old 12-01-2004, 12:13 PM   #4
scilec
LQ Newbie
 
Registered: Oct 2004
Distribution: Redhat Enterprise 3
Posts: 7

Original Poster
Rep: Reputation: 0
My apologies for not being more clear in my initial question. In my first example, imagine a text file containing:

TTITLE1=There has go
TTITLE1=t to be a better way.

I want my script to parse this as "There has got to be a better way."

In my second example, imagine my text file contains:

TTITLE2=You shook me all night
TTITLE2=long.

Here, I'd want to parse it as "You shook me all night long."

The problem is that, depending on how I parse the file, I either end up with:

There has go t to be a better way. #unwanted space in "got"
You shook me all night long.

or

There has got to be a better way.
You shook me allnight long. # No space between "all" and "night"

Regardless of whether I use cut, sed, or awk, lines that end with a space aren't beiing handled correctly. Here's the script I'm using, where $1 is the file being read:


#!/bin/bash
#
titlenum=0

while [ -n "$(grep "TTITLE$titlenum=" $1)" ]
do
tan=$(grep "TTITLE$titlenum=" $1)
title=$(echo ${tan//"TTITLE$titlenum="/} | awk '{print $0}')
echo $title >> trackfile.txt
let titlenum=titlenum+1
done

In this case, I get the following:

There has got to be a better way.
You shook me allnight long. # No space between "all" and "night"

How do I modify my script so that it properly wraps text into a single line regardless of whether or not there's a space at the end of a line?
 
Old 12-02-2004, 03:08 AM   #5
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
Code:
/ibrc/users/tba17/daily17/soumen/tmp>cat abc |tr " " "*"
TTITLE1=There*has*go
TTITLE1=t*to*be*a*better*way.

TTITLE2=You*shook*me*all*night*
TTITLE2=long.

TTITLE3=hello,
TTITLE3=*
TTITLE3=how*are*you?*

/ibrc/users/tba17/daily17/soumen/tmp>cat b
#!/usr/bin/bash

fname=$1
tstr=TTITLE
ofile=trackfile.txt

> $ofile
for srl in $(cut -d"=" -f1 $fname | grep -v "^$" | sed -e 's/'$tstr'//g' |sort -u -n) ; do
        curstr=${tstr}${srl}
        echo "Processing $curstr ..."

        grep "^${curstr}=" $fname | \
        awk -F"=" '{arr[$1]=arr[$1]""$2} END{ for (i in arr) {printf("%s\n",arr[i])}}' | \
        grep -v "^$" >> $ofile
done
/ibrc/users/tba17/daily17/soumen/tmp>b abc
Processing TTITLE1 ...
Processing TTITLE2 ...
Processing TTITLE3 ...
/ibrc/users/tba17/daily17/soumen/tmp>cat trackfile.txt
There has got to be a better way.
You shook me all night long.
hello, how are you?
/ibrc/users/tba17/daily17/soumen/tmp>
The spaces in the input file [abc here], have been shown with "*". The script works even if the line is :

1. Ending/starting with space
2. Has only space(s)
3. Has nothing after "=" e.g. TTITLE1=

HTH.
 
Old 12-02-2004, 01:00 PM   #6
scilec
LQ Newbie
 
Registered: Oct 2004
Distribution: Redhat Enterprise 3
Posts: 7

Original Poster
Rep: Reputation: 0
Perfect

Just what I needed. Thank you so much!

-Steve
 
  


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
need help parsing text file airman99 Linux - General 2 10-08-2004 09:09 PM
Parsing large text file with perl smaida Programming 5 09-13-2004 04:33 AM
Parsing Text from a html file. Rezon Programming 6 10-18-2003 12:09 AM
Parsing a tab delimited text file jajanes Programming 9 08-08-2003 10:34 AM
Parsing a file for a string of text jamesmwlv Linux - General 2 12-02-2002 07:13 PM


All times are GMT -5. The time now is 01:30 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