[SLUG] treating \n like any other character

2007-10-03 Thread david
I want to edit a multi line file as if it were all one line 

In other words, treat \n like any other character, and specifically
doing global find and replace.

I know there are various hex editors, but they are all pretty clunky as
far as I can see, and none seem to be able to do that from command line.

Is there a shell script way to do it?

-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html


Re: [SLUG] treating \n like any other character

2007-10-03 Thread Amos Shapira
On 03/10/2007, david [EMAIL PROTECTED] wrote:

 I want to edit a multi line file as if it were all one line

 In other words, treat \n like any other character, and specifically
 doing global find and replace.

 I know there are various hex editors, but they are all pretty clunky as
 far as I can see, and none seem to be able to do that from command line.

 Is there a shell script way to do it?


Can you be more specific of what you are trying to achieve?

A brief look at sed(1) (GNU sed 4.1.5 on Debian Etch) shows that \n in
regular expressions will be treated as a newline, and in perl you can add
//s modifier to make perl treat strings as single line (see perlre(1)).

Also it might be worth digging the excellent vim.org web site, I wouldn't be
surprised to find something there that will tell you how to do that using
VIM.

--Amos
-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html


Re: [SLUG] treating \n like any other character

2007-10-03 Thread david
On Wed, 2007-10-03 at 18:44 +1000, Amos Shapira wrote:
 On 03/10/2007, david [EMAIL PROTECTED] wrote:
 
  I want to edit a multi line file as if it were all one line
 
  In other words, treat \n like any other character, and specifically
  doing global find and replace.
 
  I know there are various hex editors, but they are all pretty clunky as
  far as I can see, and none seem to be able to do that from command line.
 
  Is there a shell script way to do it?
 
 
 Can you be more specific of what you are trying to achieve?
 


[EMAIL PROTECTED]:~/test/testdir $ cat  test
1 
2
3
[EMAIL PROTECTED]:~/test/testdir $ sed s/1\n/1/g test 
1
2
3
[EMAIL PROTECTED]:~/test/testdir $ 

The output I would have liked would be:

12
3

but sed doesn't seem to work like that. Pity. I'm pretty sure you can't
get vim to do it either. I'm assuming vim just uses sed anyway?







 A brief look at sed(1) (GNU sed 4.1.5 on Debian Etch) shows that \n in
 regular expressions will be treated as a newline, and in perl you can add
 //s modifier to make perl treat strings as single line (see perlre(1)).
 
 Also it might be worth digging the excellent vim.org web site, I wouldn't be
 surprised to find something there that will tell you how to do that using
 VIM.
 
 --Amos

-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html


Re: [SLUG] treating \n like any other character

2007-10-03 Thread Zhasper
A quick google for sed remove newline returns, as the very first
hit, the page:

http://snow.nl/dist/htmlc/ch13s04.html - look for the header The
Pattern Buffer

There's a script there that will get you on your way.

On 03/10/2007, david [EMAIL PROTECTED] wrote:
 I want to edit a multi line file as if it were all one line

 In other words, treat \n like any other character, and specifically
 doing global find and replace.

 I know there are various hex editors, but they are all pretty clunky as
 far as I can see, and none seem to be able to do that from command line.

 Is there a shell script way to do it?

 --
 SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
 Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html




-- 
There is nothing more worthy of contempt than a man who quotes himself
- Zhasper, 2004
-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html


Re: [SLUG] treating \n like any other character

2007-10-03 Thread Robert Thorsby

On 2007.10.03 18:52 david wrote:

On Wed, 2007-10-03 at 18:44 +1000, Amos Shapira wrote:
 On 03/10/2007, david [EMAIL PROTECTED] wrote:
 Can you be more specific of what you are trying to achieve?

[EMAIL PROTECTED]:~/test/testdir $ cat  test
1
2
3
[EMAIL PROTECTED]:~/test/testdir $ sed s/1\n/1/g test
1
2
3
[EMAIL PROTECTED]:~/test/testdir $

The output I would have liked would be:

12
3

but sed doesn't seem to work like that. Pity.


I haven't got access to my references at the moment but sed **can** do 
what you want. I think the command is H (for hold pattern space). 
Anyway, sed can search for regexes that extend beyond one line -- 
particularly useful if you want to replace (say) Sydney Linux Users 
Group with SLUG and you suspect that S... L... U... G... may, or 
may not, be split over two lines.


Try googling for sed and multiple line.

Robert Thorsby
--
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html


Re: [SLUG] treating \n like any other character

2007-10-03 Thread Peter Hardy
On Wed, 2007-10-03 at 18:52 +1000, david wrote:
 [EMAIL PROTECTED]:~/test/testdir $ cat  test
 1 
 2
 3
 [EMAIL PROTECTED]:~/test/testdir $ sed s/1\n/1/g test 
 1
 2
 3
 [EMAIL PROTECTED]:~/test/testdir $ 
 
 The output I would have liked would be:
 
 12
 3
 
 but sed doesn't seem to work like that. Pity. I'm pretty sure you can't
 get vim to do it either. I'm assuming vim just uses sed anyway?

vim will do that just fine, and is pretty much my go-to when I find
myself needing to do it.

I did find a way to search/replace across newlines with sed, too, but it
made my head hurt.

-- 
Pete

-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html


Re: [SLUG] treating \n like any other character

2007-10-03 Thread Gavin Carr
On Wed, Oct 03, 2007 at 06:52:17PM +1000, david wrote:
 On Wed, 2007-10-03 at 18:44 +1000, Amos Shapira wrote:
  On 03/10/2007, david [EMAIL PROTECTED] wrote:
  
   I want to edit a multi line file as if it were all one line
  
   In other words, treat \n like any other character, and specifically
   doing global find and replace.
  
   I know there are various hex editors, but they are all pretty clunky as
   far as I can see, and none seem to be able to do that from command line.
  
   Is there a shell script way to do it?
  
  
  Can you be more specific of what you are trying to achieve?
  
 
 
 [EMAIL PROTECTED]:~/test/testdir $ cat  test
 1 
 2
 3
 [EMAIL PROTECTED]:~/test/testdir $ sed s/1\n/1/g test 
 1
 2
 3
 [EMAIL PROTECTED]:~/test/testdir $ 
 
 The output I would have liked would be:
 
 12
 3


nox:~/tmp$ cat  test
1
2
3
nox:~/tmp$ 
nox:~/tmp$ perl -i -pe 's/1\n/1/' test
nox:~/tmp$ cat test
12
3
nox:~/tmp$


Cheers,
Gavin


-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html


Re: [SLUG] treating \n like any other character

2007-10-03 Thread Michael Davies
On 03/10/2007, david [EMAIL PROTECTED] wrote:
 I want to edit a multi line file as if it were all one line

 In other words, treat \n like any other character, and specifically
 doing global find and replace.

 I know there are various hex editors, but they are all pretty clunky as
 far as I can see, and none seem to be able to do that from command line.

 Is there a shell script way to do it?

How about doing it in Perl?

use strict; # keeping
use warnings; # myself
use Perl::Critic; # honest ;)
use Carp; # croak

my $filename = /some/random/file.txt;
my $text;
{
local $/; # undef the input file sep, so we don't break on \n
open(my $fh, $filename) or croak qq(I can't even open a file, sigh);
$text = $fh; # slurp
}
$text =~ s#\n#\*#gmsx; # replace \n's with *'s.  Customise to your
heart's content :)
print $text;

Hope this helps,

Michael...
-- 
Michael Davies   Do what you think is interesting, do somthing that
[EMAIL PROTECTED]you think is fun and worthwhile, because otherwise
http://michaeldavies.org  you won't do it well anyway. -- Brian Kernighan
-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html


Re: [SLUG] treating \n like any other character

2007-10-03 Thread david
On Wed, 2007-10-03 at 19:22 +1000, Peter Hardy wrote:
 On Wed, 2007-10-03 at 18:52 +1000, david wrote:
  [EMAIL PROTECTED]:~/test/testdir $ cat  test
  1 
  2
  3
  [EMAIL PROTECTED]:~/test/testdir $ sed s/1\n/1/g test 
  1
  2
  3
  [EMAIL PROTECTED]:~/test/testdir $ 
  
  The output I would have liked would be:
  
  12
  3
  
  but sed doesn't seem to work like that. Pity. I'm pretty sure you can't
  get vim to do it either. I'm assuming vim just uses sed anyway?
 
 vim will do that just fine, and is pretty much my go-to when I find
 myself needing to do it.
 
 I did find a way to search/replace across newlines with sed, too, but it
 made my head hurt.

thanks everyone.

Looks like it's time to stop avoiding perl. (thanks Gavin). 
perl -i -pe 's/1\n/1/' test

vim does work, and I just realised that it can be scripted - I should
have checked instead of ass-uming. The things you can learn by asking.

sed pattern buffering started to make my head hurt too :-) or maybe it's
just too late at night after a hot day.
http://snow.nl/dist/htmlc/ch13s04.html

-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html


Re: [SLUG] treating \n like any other character

2007-10-03 Thread Sonia Hamilton
On Wed, 03 Oct 2007 23:51:11 +1000, david [EMAIL PROTECTED] said:
 Looks like it's time to stop avoiding perl. (thanks Gavin). 
   perl -i -pe 's/1\n/1/' test
 
 vim does work, and I just realised that it can be scripted - I should
 have checked instead of ass-uming. The things you can learn by asking.

There's a really good book that's been out for a few years called
Minimal Perl by Tim Maher. Teaches *nix people who already know a
touch of shell/grep/awk/sed how to rapidly get up to speed on Perl as a
command line tool, rather than taking the approach of a lot of books
(which is to teach Perl as a programming language).

If I had to migrate to Waziristan tomorrow and was only allowed to take
5 books, it'd be one of them (together with Vi Improved - Vim by
Oualline); see the book reviews on Amazon for other superlatives.

:-)
-- 
Sonia Hamilton

-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html