LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 04-20-2007, 01:38 PM   #1
armandino101
LQ Newbie
 
Registered: Oct 2006
Distribution: Ubuntu
Posts: 14

Rep: Reputation: 0
Bash question


Hi,

say you have a file like this:

Code:
-- comment 1
This
is
the first
line

-- comment 2
This is the
second line

-- comment 3
And this is the third
and you want to generate this


Code:
-- comment 1
This is the first line

-- comment 2
This is the second line

-- comment 3
And this is the third
What's the easiest/shortest way to do this?

Thanks advance,
A.
 
Old 04-20-2007, 02:27 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Don't know if this is easy or even short (enough), but it works:
Code:
#!/usr/bin/perl
# splitter.pl
# Usage: splitter.pl <infile>

use strict;
use warnings;

my $inFile="$ARGV[0]";

open(FH,$inFile);
while (<FH>) {
  chomp;
  print "$_\n" if ( /^-- comment/ );
  print "$_ "  if ( !/^-- comment/ );
  print "\n\n" if ( /^$/ );
}

close FH;
I could not come up with a one-liner.........

Hope this helps.
 
Old 04-20-2007, 03:03 PM   #3
drawde83
Member
 
Registered: May 2006
Location: New Zealand
Distribution: ubuntu, gentoo at uni
Posts: 31

Rep: Reputation: 15
ok this almost works

Code:
cat comment.txt|sed '/^[[:alpha:]]/N;s/\n/ /'

Last edited by drawde83; 04-20-2007 at 07:33 PM. Reason: idn't use code tags before, sorry
 
Old 04-20-2007, 03:57 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi again,

Here's a (semi) one-liner:
Code:
cat infile | tr "\n" " " | sed -e 's/\(-- comment [1-9][0-9]* \)/\1\
/g' -e 's/  -/\
\
/g'
I like the perl script a lot better, but this one can be executed from the command line......
 
Old 04-20-2007, 03:58 PM   #5
drawde83
Member
 
Registered: May 2006
Location: New Zealand
Distribution: ubuntu, gentoo at uni
Posts: 31

Rep: Reputation: 15
It's not pretty and it can probably be made simpler but it's all on one line and it works.
Code:
cat comment.txt|tr '\n' ' '| sed -e 's/--/\n\n--/g' -e "s/\([[:digit:]]\)/\1\n/g"
 
Old 04-20-2007, 04:02 PM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
ROFL

Almost the same solution within the minute

drawde83's version using \n in the sed statement could fail on certain platforms, that's why I choose the 'multi-line' one-liner....

If it works on your platform, use that one.
 
Old 04-24-2007, 12:18 PM   #7
armandino101
LQ Newbie
 
Registered: Oct 2006
Distribution: Ubuntu
Posts: 14

Original Poster
Rep: Reputation: 0
Thanks for the replies!

drawde, your first script almost works like you said..

druuna, I should have also mentioned that comment 1, 2, 3 can be anything and doesn't have to end with a digit. Basically if a string starts with '--' it's a comment.

The file actually represents a long sql script where i need to strip off the formatting, ie the new lines. If you have any other ideas please post!
 
  


Reply

Tags
perl, sed



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
A question about BASH operand Linux - Newbie 2 11-27-2005 09:49 PM
a bash question thanhVic Linux - General 2 02-05-2005 04:39 AM
bash question gsgleason Linux - Newbie 5 01-26-2005 03:52 PM
little bash question gplacek Linux - General 2 09-01-2004 12:10 PM
Bash question J_Szucs Programming 9 01-13-2003 03:19 AM

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

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