You are not logged in.

#1 2009-02-05 16:16:25

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,896
Website

Simple bash script to add a '-' [Solved]

I need to write a small bash script to add a '-' to each line in a file before displaying via conky!

Todo

- Get Milk
- Buy Food
- Pay Bills

Currently I use

TEXT
Todo
${hr}
${head /home/mrgreen/.stuffigottado.txt 30 20}

In .conkyrc but have to add '-' each time I edit .stuffigottado.txt

Thanks in advance....


Mr Green

Offline

#2 2009-02-05 16:35:49

rson451
Member
From: Annapolis, MD USA
Registered: 2007-04-15
Posts: 1,233
Website

Re: Simple bash script to add a '-' [Solved]

#!/bin/bash
cat ~/.stuffigottado.txt | sed -r "s/(.*)/- \1/" > filetobereadbyconky.txt

archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson

Offline

#3 2009-02-05 16:38:09

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,896
Website

Re: Simple bash script to add a '-' [Solved]

Wow! ...... thanks \o/


Mr Green

Offline

#4 2009-02-05 18:20:05

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: Simple bash script to add a '-' [Solved]

#TEXT
TODO
${hr}
${exec awk '{ print "- ", $_ }' stuffigottado.txt}

should work too

(edit cat | awk to just awk)

Last edited by brisbin33 (2009-02-05 18:20:58)

Offline

#5 2009-02-05 20:27:31

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,896
Website

Re: Simple bash script to add a '-' [Solved]

Yes that works too, removed extra space as its not required

though some sort of filter may be needed if line is empty ie /n

Its much better now thanks for your help :-)


Mr Green

Offline

#6 2009-02-05 21:04:29

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: Simple bash script to add a '-' [Solved]

To filter out blank lines, you could just modify the awk command:

${exec  awk '!/^$/ { print "-", $_ }' stuffigottado.txt}

Offline

#7 2009-02-05 21:50:34

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: Simple bash script to add a '-' [Solved]

Cerebral wrote:

To filter out blank lines, you could just modify the awk command:

${exec  awk '!/^$/ { print "-", $_ }' stuffigottado.txt}

very nice; awk and grep: two commands that never cease to amaze me.

Offline

#8 2009-02-06 07:04:05

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,896
Website

Re: Simple bash script to add a '-' [Solved]

That is awesome! will have to google what $_ means got an idea on some of it ...


Mr Green

Offline

#9 2009-02-06 12:40:27

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: Simple bash script to add a '-' [Solved]

awk runs commands on each "record" - usually a record is a line of input.   From what I gather, $_ is the entire line of input (although most documentation I can find uses $0 for the same thing and doesn't even mention $_)

Offline

#10 2009-02-06 14:42:25

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: Simple bash script to add a '-' [Solved]

i found $_ just by trial and error so it was by chance that i didn't land on $0 first.  documentation ftw!

Offline

Board footer

Powered by FluxBB