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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, 2012-11-29 at 23:33 +0000, 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@xxxxxxxxxxxxxxxxxxxxxxx
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


[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [EPEL Devel]     [Fedora Magazine]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Desktop]     [Fedora Fonts]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Fedora Sparc]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux