Xen
Xen

Reputation: 41

bash append text at the start and end of ls output

I want to change the output of ls from:

1.png
2.png
3.png

Into

Start [1.png] End
Start [2.png] End
Start [3.png] End

I need to append a string at the start and end of a line at the same time. I'm not opposed to using text files to store the output however I avoid it if there is a better way.

I know I can use

ls | sed 's/^/Start [/'

and

ls | sed 's/$/] End/'

However is there a way to combine these 2 operations into 1 statement? And avoid using temporary text files?

Upvotes: 3

Views: 2094

Answers (4)

nbari
nbari

Reputation: 27005

I found easier using xargs, give a try to:

$ xargs < <(ls) -I@ echo "start [ @ ] END"

-I@ will help to use @ as a placeholder so then you could use echo "start [ @ ] END and @ will be replaced with the output from ls

Upvotes: 1

Xen
Xen

Reputation: 41

ls | sed 's/^/Start [/' | sed 's/$/] End/'

Found out myself by experimenting.

Upvotes: 1

RavinderSingh13
RavinderSingh13

Reputation: 133780

Could you please try following.

ls | awk '{print "start [" $0 "] END"}'

OR

ls | sed 's/^/start \[/;s/$/\] end/'

Upvotes: 0

P....
P....

Reputation: 18411

Using sed:

sed  's/^/start [ /;s/$/ ] end/' 
start [ 1.png ] end
start [ 2.png ] end
start [ 3.png ] end

Upvotes: 0

Related Questions