FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Debian > Debian User

 
 
LinkBack Thread Tools
 
Old 09-16-2011, 04:24 PM
Tom Furie
 
Default automated editing of text files

On Fri, Sep 16, 2011 at 05:24:52PM +0200, Bonno Bloksma wrote:

> linbobo:~/sedtest# sed 's_
<pre>
_
<pre>

_' <original.txt >new.txt
> linbobo:~/sedtest# less new.txt

You want to change the regexp here. In regular expressions '^' matches
start of line and '$' matches end of line. Your sed instruction above
should be something like 's/^<pre>$/<pre>
/', or more generally
's/^(<pre>)$/1
/'.

Cheers,
Tom

--
The only problem with being a man of leisure is that you can never stop
and take a rest.
 
Old 09-16-2011, 04:25 PM
Camaleón
 
Default automated editing of text files

On Fri, 16 Sep 2011 17:24:52 +0200, Bonno Bloksma wrote:

> Hi Camaleón,
>
>>> I have a bunch of files where I need an extra line after a <pre> code.
>>> So I need to change <pre>CRLF (or <pre>LF) to <pre>CRLFCRLF.
>>>
>>> Which toool can I use for that, I can't remember which tool is a good
>>> one for this and google is no help.
>>> I am not fluent in any of the programming languages but I do write
>>> bash scripts.
>
>> I bet you can do this with a bash script for looping into the text
>> files and then replacing the text with "sed".
>>
>>Yep, something like this:
>>
>>http://www.cyberciti.biz/faq/unix-linux-replace-string-words-in-many-files/
>
> By looking at my UnixUtils list which I have on my Windows machine I had
> found sed as well. :-) Have been experimenting some with it but...
>
> linbobo:~/sedtest# sed 's_<pre>_<post>_' <original.txt >new.txt
> linbobo:~/sedtest# less new.txt
>
> And indeed I will see <post> on that line instead of <pre> but...
>
> linbobo:~/sedtest# sed 's_
<pre>
_
<pre>

_' <original.txt >new.txt
> linbobo:~/sedtest# less new.txt
>
> Will not create a new line after <pre> :-(

Hum... not sure if this what you need, but it should be something like:

sm01@stt008:~$ echo "<pre>" | sed -e "s/<pre>/<pre>
/g"
<pre>

sm01@stt008:~$

Greetings,

--
Camaleón


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: pan.2011.09.16.16.25.16@gmail.com">http://lists.debian.org/pan.2011.09.16.16.25.16@gmail.com
 
Old 09-16-2011, 04:28 PM
 
Default automated editing of text files

> I have a bunch of files where I need an extra line after a <pre> code. So
> I need to change <pre>CRLF (or <pre>LF) to <pre>CRLFCRLF.
>
> Which toool can I use for that, I can't remember which tool is a good one
> for this and google is no help.
> I am not fluent in any of the programming languages but I do write bash
> scripts.

A Perl script is a very nice way -- likely the very best way -- to do
this. You can write a script to operate automatically on every file in a
directory, searching for the <pre> code and making the insertion.

RLH


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: e2d9f06a7090d930def87db5bba9b53a.squirrel@squirrel mail.hal-pc.org">http://lists.debian.org/e2d9f06a7090d930def87db5bba9b53a.squirrel@squirrel mail.hal-pc.org
 
Old 09-16-2011, 06:58 PM
Bob Proulx
 
Default automated editing of text files

Bonno Bloksma wrote:
> Have been experimenting some with it but...
>
> linbobo:~/sedtest# sed 's_<pre>_<post>_' <original.txt >new.txt
> linbobo:~/sedtest# less new.txt
>
> And indeed I will see <post> on that line instead of <pre> but...
>
> linbobo:~/sedtest# sed 's_
<pre>
_
<pre>

_' <original.txt >new.txt
> linbobo:~/sedtest# less new.txt
>
> Will not create a new line after <pre> :-(

Using sed is a good tool for this but if you want to append then you
should use the 'a' command.

$ printf "one
two
three
four
"
one
two
three
four

$ printf "one
two
three
four
" | sed '/two/a
-> foo'
one
two
-> foo
three
four

But using Perl (or Ruby or Python or ...) is also easy.

$ printf "one
two
three
four
" | perl -lne 'print $_; print "-> foo" if $_ =~ m/two/'
one
two
-> foo
three
four

Bob
 
Old 09-16-2011, 09:14 PM
"Bonno Bloksma"
 
Default automated editing of text files

Hi Tom (and others)

>> linbobo:~/sedtest# sed 's_
<pre>
_
<pre>

_' <original.txt
>> >new.txt linbobo:~/sedtest# less new.txt

> You want to change the regexp here. In regular expressions '^' matches
start of line and '$' matches end of line.
> Your sed instruction above should be something like 's/^<pre>$/<pre>
/',
or more generally 's/^(<pre>)$/1
/'.

Ok, I've got most of it. The last part is more of a bash problem I think as
most of the files have spaces in them. Not my idea, it's how the files were
delivered to me. :-(

So the command line:
for i in `ls *.txt` ; do ./add-pre-nl.sh $i; done
does not cut it as it chops the filesnames up and issues each part to the
add-pre-nl script. :-(
If I just use
./add-pre-nl.sh file name with spaces.txt
it will produce the result I want.

Does someone know the solution or should I ask in a bash Group?

Bonno




--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: http://lists.debian.org/005501cc74b5$966cff50$c346fdf0$@bloksma@tio.nl
 
Old 09-16-2011, 09:17 PM
Tom H
 
Default automated editing of text files

On Fri, Sep 16, 2011 at 5:14 PM, Bonno Bloksma <b.bloksma@tio.nl> wrote:
>
>>> linbobo:~/sedtest# sed 's_
<pre>
_
<pre>

_' <original.txt
>>> >new.txt linbobo:~/sedtest# less new.txt
>
>> You want to change the regexp here. In regular expressions '^' matches
> start of line and '$' matches end of line.
>> Your sed instruction above should be something like 's/^<pre>$/<pre>
/',
> or more generally 's/^(<pre>)$/1
/'.
>
> Ok, I've got most of it. The last part is more of a bash problem I think as
> most of the files have spaces in them. Not my idea, it's how the files were
> delivered to me. :-(
>
> So the command line:
> *for i in `ls *.txt` ; do ./add-pre-nl.sh $i; done
> does not cut it as it chops the filesnames up and issues each part to the
> add-pre-nl script. :-(
> If I just use
> *./add-pre-nl.sh file name with spaces.txt
> it will produce the result I want.

Use "$i" for the filenames with spaces to remain whole.


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: CAOdo=SyHrd5sx1H6FtrBk46C++U8W2ZBZNDW7r+hvR0M7ku-sQ@mail.gmail.com">http://lists.debian.org/CAOdo=SyHrd5sx1H6FtrBk46C++U8W2ZBZNDW7r+hvR0M7ku-sQ@mail.gmail.com
 
Old 09-16-2011, 09:35 PM
Bob Proulx
 
Default automated editing of text files

Bonno Bloksma wrote:
> The last part is more of a bash problem I think as most of the files
> have spaces in them. Not my idea, it's how the files were delivered
> to me. :-(
>
> So the command line:
> for i in `ls *.txt` ; do ./add-pre-nl.sh $i; done

Eww.. Don't say `ls *.txt` there but just say *.txt there. No need
for ls to be there. Just let the shell do it.

> does not cut it as it chops the filesnames up and issues each part to the
> add-pre-nl script. :-(

Add double quotes around the variable to preserve the whitespace.

for i in *.txt ; do ./add-pre-nl.sh "$i"; done

If there are no *.txt files then the file glob won't be expanded,
nothing to expand it to, and then your shell script will get a literal
"*.txt" as an argument. If that minor point is important to you then
check that the file exists and simply break or continue if it does not.

for i in *.txt; do
test -f "$i" || continue
./add-pre-nl.sh "$i"
done

Bob
 
Old 09-16-2011, 09:49 PM
Aéris
 
Default automated editing of text files

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Le 16/09/2011 23:40, Bob Proulx a écrit :
> If there are no *.txt files then the file glob won't be expanded,
> nothing to expand it to, and then your shell script will get a literal
> "*.txt" as an argument. If that minor point is important to you then
> check that the file exists and simply break or continue if it does not.
>
> for i in *.txt; do
> test -f "$i" || continue
> ./add-pre-nl.sh "$i"
> done

Simplest version :
find -type f -name "*.txt" -exec add-pre-nl.sh {} ;


- --
Aeris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJOc8RvAAoJEK8zQvxDY4P9gbUH/2W5qTMILYW6ETTKKLnI+j2+
Ar5O8XhnnaQrTtLQgxzTiYfp0ErUL/pin8q/hYDh6bzDgnX4pog+6L+wJi/iV/Cx
Ob0tGvQQZjA2Y9oeTgkBRpxiiYKTohZK0cBYF8+Sf6w/+dLAF2lM3BygaRBkFcBM
4EuoOybM3GJq+jpsRyHiFCflOK+04yBzgSCyIL7awlviUchxFq VzwLpCaZe+FDSc
tSPBxL5UbP+LI+YfACiYHeJQmpnXGgeHj6Lkyxv7d1DLVRsjZR Bt4jEDYCPjgPfW
UUO4NMKbH4adv1HnFUH72vB35G2aMuFylXd36Xx0Nozv+3LMWd trCUK+Ois8pHM=
=lwDc
-----END PGP SIGNATURE-----


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 4e73c475$0$17327$426a74cc@news.free.fr">http://lists.debian.org/4e73c475$0$17327$426a74cc@news.free.fr
 
Old 09-16-2011, 10:32 PM
Bob Proulx
 
Default automated editing of text files

Aéris wrote:
> Le 16/09/2011 23:40, Bob Proulx a écrit :
> > If there are no *.txt files then the file glob won't be expanded,
> > nothing to expand it to, and then your shell script will get a literal
> > "*.txt" as an argument. If that minor point is important to you then
> > check that the file exists and simply break or continue if it does not.
> >
> > for i in *.txt; do
> > test -f "$i" || continue
> > ./add-pre-nl.sh "$i"
> > done
>
> Simplest version :
> find -type f -name "*.txt" -exec add-pre-nl.sh {} ;

Ah... Very good! An excellent suggestion! 'find' rocks! I will
note three things here however.

* One is that the find will recurse down through a possibly deep
hierarchy of directories. It isn't an identical alternative for just
looking for *.txt in the current directory. But probably that
difference isn't important here. Or perhaps that difference will be a
really desirable feature.

* Secondly if the add-pre-nl.sh script handle multiple file arguments
then instead of ; use + so that it calls it fewer times with as many
file arguments as possible. It will be more efficient that way.

* And lastly that you forgot to include the directory path to find,
probably a '.' wanted here.

find . -type f -name "*.txt" -exec add-pre-nl.sh {} +

:-)

Bob
 
Old 09-16-2011, 11:14 PM
Aéris
 
Default automated editing of text files

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Le 17/09/2011 00:40, Bob Proulx a écrit :
> Ah... Very good! An excellent suggestion! 'find' rocks! I will
> note three things here however.
>
> * One is that the find will recurse down through a possibly deep
> hierarchy of directories. It isn't an identical alternative for just
> looking for *.txt in the current directory. But probably that
> difference isn't important here. Or perhaps that difference will be a
> really desirable feature.

Use « -maxdepth 0 » to limit to the current level only

> * Secondly if the add-pre-nl.sh script handle multiple file arguments
> then instead of ; use + so that it calls it fewer times with as many
> file arguments as possible. It will be more efficient that way.

In this case, I prefere using « xargs » :
find -type f -name "*.txt" | xargs add-pre-nl.sh
And if there is space/single-quote/double-quote/new-line in some filenames :
find -type f -name "*.txt" -print0 | xargs -0 add-pre-nl.sh


> * And lastly that you forgot to include the directory path to find,
> probably a '.' wanted here.

No, « find » considers the current directory if no path is given =)

- --
Aeris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJOc9hXAAoJEK8zQvxDY4P9FJsH/2kfxAnqc10trIyJC7N6HW0I
x3Rff2Zg24+lhtRYRJPxPIXRebuFjCe1i3DVwPZ5lC9XSgX3HY PNdrxg+IDj40g/
0HsKlIEcRJen1szDGtLpyqyBHEa95kevpxd/rql9rcljJCjBCpkXnLdVLdXKjx68
QRr1a/sPAgiTK49JT+gFIdKjv9Q8ngDhMa2QhQ7Avw7wLbhd/pHrvQBi3VXteqI6
Tga6FYm9WN2DfSCD4aDPz9jdU9TSl9GGwNxm7wHqPmCDx7y8mz TD6hK/NPUo1eit
ZS9E0/OjokKlSl4WioC43kaQV/HKvebwABXKuny363KOmHazzLm/VfqxQzG8z7E=
=1UAu
-----END PGP SIGNATURE-----


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 4e73d857$0$26704$426a74cc@news.free.fr">http://lists.debian.org/4e73d857$0$26704$426a74cc@news.free.fr
 

Thread Tools




All times are GMT. The time now is 08:23 PM.

VBulletin, Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2007 - 2008, www.linux-archive.org