views:

148

answers:

4

My shell has a call to 'fortune' in my .login file, to provide me with a little message of the day. However, some of the fortunes begin with one leading whitespace line, some begin with two, and some don't have any leading whitespace lines at all. This bugs me.

I sat down to wrapper fortune with my own shell script, which would remove all the leading whitespace from the input, without destroying any formatting of the actual fortune, which may intentionally have lines of whitespace.

It doesn't appear to be an easy one-liner two-minute fix, and as I read(reed) through the man pages for sed and grep, I figured I'd ask our wonderful patrons here.

A: 
# delete all leading blank lines at top of file
sed '/./,$!d'

Source: http://www.linuxhowtos.org/System/sedoneliner.htm?ref=news.rdf

Just pipe the output of fortune into it:

fortune | sed '/./,$!d'
Amber
A: 

How about:

sed "s/^ *//" < fortunefile
toby
Why was this voted down? This is how I read the OP's request.
SiegeX
+2  A: 

Using the same source as Dav:

# delete all leading blank lines at top of file
sed '/./,$!d'

Source: http://www.linuxhowtos.org/System/sedoneliner.htm?ref=news.rdf

Additionally, here's why this works:

The comma separates a "range" of operation. sed can accept regular expressions for range definitions, so /./ matches the first line with "anything" (.) on it and $ specifies the end of the file. Therefore,

  • /./,$ matches "the first not-blank line to the end of the file".
  • ! then inverts that selection, making it effectively "the blank lines at the top of the file".
  • d deletes those lines.
Travis Bradshaw
Perfect!! Thanks a bunch!! The link will prove very useful in the future too.
Maarx
It seems clearer to write: sed -n '/./,$p'
William Pursell
A: 

i am not sure about how your fortune message actually looks like, but here's an illustration

$ string="       my message of the day"
$ echo $string
my message of the day
$ echo "$string"
       my message of the day

or you could use awk

echo "${string}" | awk '{gsub(/^ +/,"")}1'
ghostdog74