johan
johan

Reputation: 45

script shell : copy part of line in a file and put it at the beginning

I have a file looking like that

blabla1/blabla2/blabla3.ex
blabla4/blabla5/blabla6.ex
blabla7/blabla8/blabla9.ex

and with a script shell linux, I would like to change it to

blabla3 : blabla1/blabla2/blabla3.ex
blabla6 : blabla4/blabla5/blabla6.ex
blabla9 : blabla7/blabla8/blabla9.ex

I didnt succeed that with a sed command. My problem is copying text existing in a line at the beginning of each line.

Does anybody have a solution? Thanks

Upvotes: 0

Views: 198

Answers (3)

Kent
Kent

Reputation: 195289

for fun:

paste file file|sed 's@^\S*/@@;s/\.[^.]*\s/ : /'

Upvotes: 0

sat
sat

Reputation: 14979

You can use this sed,

sed 's#^.*/\([^/]*\).ex#\1 : &#' yourfile

Upvotes: 1

fedorqui
fedorqui

Reputation: 290525

awk loves these things:

$ awk -F[/.] '{print $(NF-1),$0}' OFS=" : " file
blabla3 : blabla1/blabla2/blabla3.ex
blabla6 : blabla4/blabla5/blabla6.ex
blabla9 : blabla7/blabla8/blabla9.ex

Explanation

  • -F[/.] set field separators to / or ..
  • print $(NF-1),$0 print the penultimate field and then the full line.
  • OFS=" : " set output field separator to space + : + space.

Upvotes: 4

Related Questions