Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding text using SED

Tags:

linux

unix

sed

i am working with a task right now, manipulating text in Unix, but i have no idea how i will do it. I am planning to use sed. this is wat i am trying to do.

I have this text.

BS111

xxxxx
yyyyy
zzzzz

BS112

xxxxx
yyyyy
zzzzz

BS113

xxxxx
yyyyy
zzzzz

so on.. with this kind of format

and i want it to be like:

BS111 xxxxx
BS111 yyyyy
BS111 zzzzz



BS112 xxxxx
BS112 yyyyy
BS112 zzzzz



BS113 xxxxx
BS113 yyyyy
BS113 zzzzz

so on.. with this kind of format

the BS* and its data occurs almost hundred times. so i think the best way to do it is to use a script. thank you in advance for your help.

like image 256
jonas Avatar asked Jun 02 '26 06:06

jonas


2 Answers

With awk :

awk '/^BS/{v=$0;next} {print (/^$/) ? $0 : v, $0}' file.txt

BS111 xxxxx
BS111 yyyyy
BS111 zzzzz


BS112 xxxxx
BS112 yyyyy
BS112 zzzzz


BS113 xxxxx
BS113 yyyyy
BS113 zzzzz
like image 52
Gilles Quenot Avatar answered Jun 03 '26 22:06

Gilles Quenot


This might work for you (GNU sed):

sed -r '/^BS/{h;d};/./!b;G;s/(.*)\n(.*)/\2 \1/' file
like image 45
potong Avatar answered Jun 04 '26 00:06

potong



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!