Sed reqular expression question.

Sed reqular expression question.

am 30.10.2007 21:48:24 von mostro713

Hi.

I have a list of IP addresses (one per line) in a text file. I need
to surround them with pipe symbols and include a single space in
between the first and last number.

For example:

[before]
192.168.1.1

192.168.1.2
192.168.1.3

192.168.1.4
192.168.1.5

[after]
| 192.168.1.1 |
| 192.168.1.2 |
| 192.168.1.3 |
| 192.168.1.4 |
| 192.168.1.5 |

I tried a few different sed line that didn't work out so well

Thanks in advance....

Re: Sed reqular expression question.

am 30.10.2007 21:56:43 von Christian Schneider

Thus spake mostro713@gmail.com (mostro713@gmail.com):
> I have a list of IP addresses (one per line) in a text file. I need
> to surround them with pipe symbols and include a single space in
> between the first and last number.
>
> For example:
>
> [before]
> 192.168.1.1
>
> 192.168.1.2
> 192.168.1.3
>
> 192.168.1.4
> 192.168.1.5
>
> [after]
>| 192.168.1.1 |
>| 192.168.1.2 |
>| 192.168.1.3 |
>| 192.168.1.4 |
>| 192.168.1.5 |
>
> I tried a few different sed line that didn't work out so well

Means "" a empty line? If so use
sed '/^$/d;s/^/| /;s/$/ |/' file
Otherwise use
sed 's/^/| /;s/$/ |/' file
--
{ \|/ ______ \|/ Access denieded | Christian 'strcat' Schneider }
{ "@' / , . \ `@" Nah Nah Nah :p | http://www.strcat.de/ }
{ /__| \____/ |__\ | GnuPG-Key-ID: 47E322CE }
{ \___U__/ | http://strcat.de/chris.gpg }

Re: Sed reqular expression question.

am 30.10.2007 22:10:38 von Ed Morton

On 10/30/2007 3:48 PM, mostro713@gmail.com wrote:
> Hi.
>
> I have a list of IP addresses (one per line) in a text file. I need
> to surround them with pipe symbols and include a single space in
> between the first and last number.
>
> For example:
>
> [before]
> 192.168.1.1
>
> 192.168.1.2
> 192.168.1.3
>
> 192.168.1.4
> 192.168.1.5
>
> [after]
> | 192.168.1.1 |
> | 192.168.1.2 |
> | 192.168.1.3 |
> | 192.168.1.4 |
> | 192.168.1.5 |
>
> I tried a few different sed line that didn't work out so well
>
> Thanks in advance....
>

sed 's/\(.*\)/| \1 |/' file

Re: Sed reqular expression question.

am 30.10.2007 22:14:33 von Stephane CHAZELAS

2007-10-30, 21:56(+01), Christian Schneider:
> Thus spake mostro713@gmail.com (mostro713@gmail.com):
>> I have a list of IP addresses (one per line) in a text file. I need
>> to surround them with pipe symbols and include a single space in
>> between the first and last number.
>>
>> For example:
>>
>> [before]
>> 192.168.1.1
>>
>> 192.168.1.2
>> 192.168.1.3
>>
>> 192.168.1.4
>> 192.168.1.5
>>
>> [after]
>>| 192.168.1.1 |
>>| 192.168.1.2 |
>>| 192.168.1.3 |
>>| 192.168.1.4 |
>>| 192.168.1.5 |
>>
>> I tried a few different sed line that didn't work out so well
>
> Means "" a empty line? If so use
> sed '/^$/d;s/^/| /;s/$/ |/' file

Or
sed -n 's/..*/| & |/p' file

> Otherwise use
> sed 's/^/| /;s/$/ |/' file

Or
sed 's/.*/| & |/' file

or

paste -d '| |' - - file - - < /dev/null

--
Stéphane

Re: Sed reqular expression question.

am 31.10.2007 01:41:23 von mostro713

On Oct 30, 4:56 pm, Christian Schneider wrote:
> Thus spake mostro...@gmail.com (mostro...@gmail.com):
>
>
>
> > I have a list of IP addresses (one per line) in a text file. I need
> > to surround them with pipe symbols and include a single space in
> > between the first and last number.
>
> > For example:
>
> > [before]
> > 192.168.1.1
> >
> > 192.168.1.2
> > 192.168.1.3
> >
> > 192.168.1.4
> > 192.168.1.5
>
> > [after]
> >| 192.168.1.1 |
> >| 192.168.1.2 |
> >| 192.168.1.3 |
> >| 192.168.1.4 |
> >| 192.168.1.5 |
>
> > I tried a few different sed line that didn't work out so well
>
> Means "" a empty line? If so use
> sed '/^$/d;s/^/| /;s/$/ |/' file
> Otherwise use
> sed 's/^/| /;s/$/ |/' file
> --
> { \|/ ______ \|/ Access denieded | Christian 'strcat' Schneider }
> { "@' / , . \ `@" Nah Nah Nah :p | http://www.strcat.de/ }
> { /__| \____/ |__\ | GnuPG-Key-ID: 47E322CE }
> { \___U__/ | http://strcat.de/chris.gpg }



sed '/^$/d;s/^/| /;s/$/ |/' worked for me.. Now I have to read the
man page to figure it out. :)

Thank you.

Re: Sed reqular expression question.

am 31.10.2007 02:10:27 von Christian Schneider

Thus spake mostro713@gmail.com (mostro713@gmail.com):
> On Oct 30, 4:56 pm, Christian Schneider wrote:
>> Thus spake mostro...@gmail.com (mostro...@gmail.com):
>>
>> > I have a list of IP addresses (one per line) in a text file. I need
>> > to surround them with pipe symbols and include a single space in
>> > between the first and last number.
[...]
>> Means "" a empty line? If so use
>> sed '/^$/d;s/^/| /;s/$/ |/' file
>> Otherwise use
>> sed 's/^/| /;s/$/ |/' file
[...]
> sed '/^$/d;s/^/| /;s/$/ |/' worked for me.. Now I have to read the
> man page to figure it out. :)

/^$/d deletes all blank lines from a file. ^ matching the null string at
the beginning of a line and $ the null string at the end of a line. "d"
is "delete".
The semicolon is a "seperator" for the next directive "s/^/| /" which
substitutes ("s") the beginning of each line with "| " ("s/$/ |/" for
the end of each line).
btw. "info sed" is more detailed as the manpage.
--
{ \|/ ______ \|/ Access denieded | Christian 'strcat' Schneider }
{ "@' / , . \ `@" Nah Nah Nah :p | http://www.strcat.de/ }
{ /__| \____/ |__\ | GnuPG-Key-ID: 47E322CE }
{ \___U__/ | http://strcat.de/chris.gpg }

Re: Sed reqular expression question.

am 31.10.2007 10:53:50 von mostro713

On Oct 30, 9:10 pm, Christian Schneider wrote:
> Thus spake mostro...@gmail.com (mostro...@gmail.com):
>
> > On Oct 30, 4:56 pm, Christian Schneider wrote:
> >> Thus spake mostro...@gmail.com (mostro...@gmail.com):
>
> >> > I have a list of IP addresses (one per line) in a text file. I need
> >> > to surround them with pipe symbols and include a single space in
> >> > between the first and last number.
> [...]
> >> Means "" a empty line? If so use
> >> sed '/^$/d;s/^/| /;s/$/ |/' file
> >> Otherwise use
> >> sed 's/^/| /;s/$/ |/' file
> [...]
> > sed '/^$/d;s/^/| /;s/$/ |/' worked for me.. Now I have to read the
> > man page to figure it out. :)
>
> /^$/d deletes all blank lines from a file. ^ matching the null string at
> the beginning of a line and $ the null string at the end of a line. "d"
> is "delete".
> The semicolon is a "seperator" for the next directive "s/^/| /" which
> substitutes ("s") the beginning of each line with "| " ("s/$/ |/" for
> the end of each line).
> btw. "info sed" is more detailed as the manpage.
> --
> { \|/ ______ \|/ Access denieded | Christian 'strcat' Schneider }
> { "@' / , . \ `@" Nah Nah Nah :p | http://www.strcat.de/ }
> { /__| \____/ |__\ | GnuPG-Key-ID: 47E322CE }
> { \___U__/ | http://strcat.de/chris.gpg }

Thanks for the great explanation. Yes, the complete manual is where I
ended up although your paragraphs above is a much better explanation
to an example..