Re: Bash - an odd problem using sed or awk or for

2012-12-02 Thread John Horne
On Sat, 2012-12-01 at 23:25 -0430, Patrick O'Callaghan wrote:
> On Fri, 2012-11-30 at 21:37 -0700, Greg Woods wrote:
> > On Thu, 2012-11-29 at 23:33 +, John Horne wrote:
> > 
> > >  (the '=' are not part of the variable)
> > > abc def
> > > 
> > > hijk
> > > xyz
> > > 
> > > 
> > > So in this case what is wanted is:
> > > 
> > > 
> > > hijk
> > > xyz
> > > 
> > > 
> > > to be shown.
> > 
> > echo "$XX" | sed -e '1,/^$/d'
> 
> This will delete lines up to and including the first blank line. The
> OP's problem statement isn't very clear, but I think he wants to delete
> everything up to and including the last blank line, if any.
> 
Correct.



John.

-- 
John Horne, Plymouth University, UK
Tel: +44 (0)1752 587287Fax: +44 (0)1752 587001

-- 
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
Have a question? Ask away: http://ask.fedoraproject.org


Re: Bash - an odd problem using sed or awk or for

2012-12-01 Thread Patrick O'Callaghan
On Fri, 2012-11-30 at 21:37 -0700, Greg Woods wrote:
> On Thu, 2012-11-29 at 23:33 +, John Horne wrote:
> 
> >  (the '=' are not part of the variable)
> > abc def
> > 
> > hijk
> > xyz
> > 
> > 
> > So in this case what is wanted is:
> > 
> > 
> > hijk
> > xyz
> > 
> > 
> > to be shown.
> 
> echo "$XX" | sed -e '1,/^$/d'

This will delete lines up to and including the first blank line. The
OP's problem statement isn't very clear, but I think he wants to delete
everything up to and including the last blank line, if any.

poc

-- 
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
Have a question? Ask away: http://ask.fedoraproject.org


Re: Bash - an odd problem using sed or awk or for

2012-11-30 Thread Greg Woods
On Thu, 2012-11-29 at 23:33 +, John Horne wrote:

>  (the '=' are not part of the variable)
> abc def
> 
> hijk
> xyz
> 
> 
> So in this case what is wanted is:
> 
> 
> hijk
> xyz
> 
> 
> to be shown.

echo "$XX" | sed -e '1,/^$/d'

-- 
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
Have a question? Ask away: http://ask.fedoraproject.org


Re: Bash - an odd problem using sed or awk or for

2012-11-30 Thread inode0
On Fri, Nov 30, 2012 at 5:32 AM, John Horne  wrote:
> On Thu, 2012-11-29 at 18:56 -0600, inode0 wrote:
>
>>
>> Oh, for a simple variable this should work
>>
>> echo "${XX/*
>>
>> }"
>>
> Hello,
>
> Yes, that does seem to work :-)
>
> Although I have to admit I'm not sure why! I'll investigate further :-)

Basically it just drops the longest substring that matches the given
pattern (match anything followed by two newlines). This does
potentially miss some edge cases if you can have strings containing
only a single leading or possibly trailing blank line. But you can
remove leading and trailing blank lines if necessary or come up with
something more clever. See the "Parameter Expansion" section of the
bash man page for the details of this and other frequently useful ways
to expand/modify variables.

John
-- 
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
Have a question? Ask away: http://ask.fedoraproject.org


Re: Bash - an odd problem using sed or awk or for

2012-11-30 Thread John Horne
On Thu, 2012-11-29 at 18:56 -0600, inode0 wrote:

> 
> Oh, for a simple variable this should work
> 
> echo "${XX/*
> 
> }"
> 
Hello,

Yes, that does seem to work :-)

Although I have to admit I'm not sure why! I'll investigate further :-)



Many thanks,

John.

-- 
John Horne   Tel: +44 (0)1752 587287
Plymouth University, UK  Fax: +44 (0)1752 587001
-- 
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
Have a question? Ask away: http://ask.fedoraproject.org


Re: Bash - an odd problem using sed or awk or for

2012-11-29 Thread JD


On 11/29/2012 04:33 PM, John Horne wrote:

Hello,

I have a bash script in which a variable is set to one or more lines of
text. What I want is to remove any lines up to and including a blank
line (or alternatively to echo all the lines after the last blank line).
There may be zero or more blank lines, and the blank lines need not be
consecutive. If there is no blank line, then all the lines should be
shown. If the last line is blank, then nothing should be shown. So for
example the variable may contain:

 (the '=' are not part of the variable)
abc def

hijk
xyz


So in this case what is wanted is:


hijk
xyz


to be shown.

I tried something like:

echo "$XX" | sed -e '/./,/^$/d'

but this didn't display anything. (Where XX is the variable.)
I also tried using a 'for' loop but again this displayed nothing:

opt=""
IFS=$'\n'
for n in $XX; do test -z "$n" && opt="" || opt="$opt $n"; done

(Echoing $opt after this shows that it contains nothing.) I'm not sure
why but even using a for loop just to show it had seen a blank line
didn't work either (using something like 'test -z "$n" && echo found').
My understanding was that by setting IFS to a newline, then the 'for'
loop should see the blank line and just set '$n' to the null string. We
should then be able to test on that.

Ideally what I am looking for is a snappy one line 'sed' or 'awk'
command to handle this :-) Unfortunately at the moment I seem to be
getting nowhere though, even with the 'for' loop.



Thanks,

John.


John, this is easily accomplished, but you are not telling us
where and from what source is your variable being set.
For example, are you reading the lines in from a file
or from stdin?
If so, then I would get rid of lines containing any space(s) as follows:

while read line; do
set $line
[ $# -lt 2 ] && echo $line
done < SomeFile

If reading from stdin, then you only need
done (without the < SomeFile).

It will print only the lines that lack a space.

--
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
Have a question? Ask away: http://ask.fedoraproject.org


Re: Bash - an odd problem using sed or awk or for

2012-11-29 Thread inode0
On Thu, Nov 29, 2012 at 6:48 PM, inode0  wrote:
> On Thu, Nov 29, 2012 at 5:33 PM, John Horne  wrote:
>> Hello,
>>
>> I have a bash script in which a variable is set to one or more lines of
>> text. What I want is to remove any lines up to and including a blank
>> line (or alternatively to echo all the lines after the last blank line).
>> There may be zero or more blank lines, and the blank lines need not be
>> consecutive. If there is no blank line, then all the lines should be
>> shown. If the last line is blank, then nothing should be shown. So for
>> example the variable may contain:
>>
>>  (the '=' are not part of the variable)
>> abc def
>>
>> hijk
>> xyz
>> 
>>
>> So in this case what is wanted is:
>>
>> 
>> hijk
>> xyz
>> 
>>
>> to be shown.
>>
>> I tried something like:
>>
>>echo "$XX" | sed -e '/./,/^$/d'
>>
>> but this didn't display anything. (Where XX is the variable.)
>
> echo "${XX[*]/*
>
> }"

Oh, for a simple variable this should work

echo "${XX/*

}"

John
-- 
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
Have a question? Ask away: http://ask.fedoraproject.org


Re: Bash - an odd problem using sed or awk or for

2012-11-29 Thread inode0
On Thu, Nov 29, 2012 at 5:33 PM, John Horne  wrote:
> Hello,
>
> I have a bash script in which a variable is set to one or more lines of
> text. What I want is to remove any lines up to and including a blank
> line (or alternatively to echo all the lines after the last blank line).
> There may be zero or more blank lines, and the blank lines need not be
> consecutive. If there is no blank line, then all the lines should be
> shown. If the last line is blank, then nothing should be shown. So for
> example the variable may contain:
>
>  (the '=' are not part of the variable)
> abc def
>
> hijk
> xyz
> 
>
> So in this case what is wanted is:
>
> 
> hijk
> xyz
> 
>
> to be shown.
>
> I tried something like:
>
>echo "$XX" | sed -e '/./,/^$/d'
>
> but this didn't display anything. (Where XX is the variable.)

echo "${XX[*]/*

}"

John
-- 
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
Have a question? Ask away: http://ask.fedoraproject.org


Re: Bash - an odd problem using sed or awk or for

2012-11-29 Thread Patrick O'Callaghan
On Thu, 2012-11-29 at 23:33 +, John Horne wrote:
> Hello,
> 
> I have a bash script in which a variable is set to one or more lines of
> text. What I want is to remove any lines up to and including a blank
> line (or alternatively to echo all the lines after the last blank line).
> There may be zero or more blank lines, and the blank lines need not be
> consecutive. If there is no blank line, then all the lines should be
> shown. If the last line is blank, then nothing should be shown. So for
> example the variable may contain:
> 
>  (the '=' are not part of the variable)
> abc def
> 
> hijk
> xyz
> 
> 
> So in this case what is wanted is:
> 
> 
> hijk
> xyz
> 
> 
> to be shown.
> 
> I tried something like:
> 
>echo "$XX" | sed -e '/./,/^$/d'
> 
> but this didn't display anything. (Where XX is the variable.)
> I also tried using a 'for' loop but again this displayed nothing:
> 
>opt=""
>IFS=$'\n'
>for n in $XX; do test -z "$n" && opt="" || opt="$opt $n"; done
> 
> (Echoing $opt after this shows that it contains nothing.) I'm not sure
> why but even using a for loop just to show it had seen a blank line
> didn't work either (using something like 'test -z "$n" && echo found').
> My understanding was that by setting IFS to a newline, then the 'for'
> loop should see the blank line and just set '$n' to the null string. We
> should then be able to test on that.
> 
> Ideally what I am looking for is a snappy one line 'sed' or 'awk'
> command to handle this :-) Unfortunately at the moment I seem to be
> getting nowhere though, even with the 'for' loop.

echo $FOO | grep .

poc

-- 
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
Have a question? Ask away: http://ask.fedoraproject.org