LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-03-2003, 04:06 AM   #1
ganninu
Member
 
Registered: Jul 2003
Distribution: RH 7.3/8.0/9.0, Debian Stable 3.0, FreeBSD 5.2, Solaris 8/9/10,HP-UX
Posts: 340

Rep: Reputation: 30
Converting a paragraph into a one-line string


Suppose I have 2 paragraphs of text such as:
"hello world 1
hello world 2
hello world 3"

linux rules 1
linux rules 2
linux rules 3"


and i want to convert each paragraph into a one line string such as "hello world 1, hello world 2, hello world 3" and "linux rules 1, linux rules 2, linux rules 3" (delimited by commas). I did it using a C program by detecting "/n" characters. But, honestly, I'd like to do it using a simple bash script - i know dat it can be done cos i've made it once, but i just cannot remember how i've done it.

Moreover, how can i control the size of a file, in a similar way as logrotate does? - i have this file here which i continusouly append data to it, but only the 50 recent entries are needed... Or for assurance, the last 100 entries, to be on d safe side that all previous entries have been processed.

thanks a lot, ganninu

Last edited by ganninu; 12-03-2003 at 04:12 AM.
 
Old 12-03-2003, 04:45 AM   #2
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
With awk, you could do it like this...

awk 'NR == 1 {printf $0} NR > 1 {printf ", %s",$0} END {printf "\n"}' FILENAME

I'm sure you could use sed as well.

For your other question, I don't know. How about a crontab that uses tail -100
and overwrites the file?

John

Last edited by jkobrien; 12-03-2003 at 04:47 AM.
 
Old 12-03-2003, 04:58 AM   #3
ganninu
Member
 
Registered: Jul 2003
Distribution: RH 7.3/8.0/9.0, Debian Stable 3.0, FreeBSD 5.2, Solaris 8/9/10,HP-UX
Posts: 340

Original Poster
Rep: Reputation: 30
Thanks jkobrien,

it worked for that particular example of having two paragraphs.

Now i hope to get it work as a general case since i simplified my problem - I have a big log file (i.e. with more than two paragraphs) where each record is displayed as a paragraph. The paragraphs are seperated by a blank line. In order to process the data, I need to convert the file such that each paragraph is displayed as a line...
 
Old 12-03-2003, 06:32 AM   #4
ganninu
Member
 
Registered: Jul 2003
Distribution: RH 7.3/8.0/9.0, Debian Stable 3.0, FreeBSD 5.2, Solaris 8/9/10,HP-UX
Posts: 340

Original Poster
Rep: Reputation: 30
I have done it in bash, and this is the way i've used:


while read line

do

if [ "$line" = "" ] #this is used to seperate the lines...
then echo
fi

echo -n "$line" # Output the line itself.

done

exit 0

Last edited by ganninu; 12-03-2003 at 06:34 AM.
 
Old 12-03-2003, 07:50 AM   #5
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
Well if you don't care about having a comma at the end of each line then the following will work...

awk '$0 != "" {printf "%s, ",$0} $0 == "" {printf "\n"}' FILENAME

You can always pass through the file again with awk or sed to get rid of the final comma.

John
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
search for paragraph marks or line ends in openoffice? lugoteehalt Linux - Software 3 12-06-2005 12:46 PM
converting a int to string irfanhab Programming 6 07-30-2005 09:40 PM
converting string to asterisks pantera Programming 2 09-13-2004 01:27 PM
Converting a string from /etc/passwd liguorir Linux - Software 3 04-13-2004 02:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:05 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration