Replace text in the line before pattern - sed

I have a text like this
5834589
MDL-BLABLABLA1
bla-bla2
bla-bla3
bla-bla4
bla-bla5
I want to replace 5834589 (number) in the line before line with the pattern MDL to EXAMPLE-RIVER-5834589 (Just add EXAMPLE-RIVER-).
Any suggestions with sed?

This might work for you (GNU sed):
sed '$!N;s/^.*\n.*MDL/EXAMPLE-RIVER-&/;P;D' file

Related

Replace new line character by comma in SED

I have the following csv data in a file.
14,95884250,ENSG00000176438,C,T,A
CCAATCAGA
14,95884250,ENSG00000176438,C,T,A
CAATCAGAG
I would like to replace alternate new line character by ',' (preferably using 'sed'). The desired output is below.
14,95884250,ENSG00000176438,C,T,A,CCAATCAGA
14,95884250,ENSG00000176438,C,T,A,CAATCAGAG
give this awk one-liner a try:
awk 'NR%2{printf "%s,",$0;next}{print}' file
This might work for you (GNU sed):
sed 'N;s/\n/,/' file
Append the next line and replace the newline by a comma.

How to replace strings in all files using sed?

I want to replace below line with next line in all the files. So what sed pattern is used for this. I have tried lot but not figured that out..
checkToken($token['token'])
checkToken($token)
This is what I have tried
sed -i -- 's/checkToken\(\$token\['token'\]\)/checkToken\(\$token\)/g' get_officers_v2.php
You just need to get your escape-characters (\) on the right place like:
sed -ie "s/\(checkToken(\$token\)\['token'\])/\1)/" get_officers_v2.php

sed: how to add to the beginning of a line on non empty lines

I have multiple lines in a text file and some are empty.
hasjdh lashd
aksl asldh l lasjdh
I want to add * to the start of all the non empty lines.
*hasjdh lashd
*aksl asldh l lasjdh
how to do it
$ sed 's/^./*&/' file
*hasjdh lashd
*aksl asldh l lasjdh
This might work for you (GNU sed):
sed '/\S/s/^/*/' file
If the line contains a non-whitespace character, insert an * before the first character of that line.

Split a string into lines of n characters each

Is it possible to split the string to lines of n characters each with sed? For example, I have a file in which the data is written in one line.
Index:0070;Done:0;Fixed:1;Index:0056;Done:1;Fixed:1;Index:0070;Done:1;Fixed:0;...
How to break a string into lines, 26 characters each?
With GNU sed:
sed -E 's/(.{26})/\1\n/g' file
Output:
Index:0070;Done:0;Fixed:1;
Index:0056;Done:1;Fixed:1;
Index:0070;Done:1;Fixed:0;
This might work for you (GNU sed):
sed 's/./&\n/26;P;D' file

sed: if line does not contain lower-case, add a blank line above and below

There are a number of questions here about sed to find lines that don't contain a string, but all of them seem to be about then deleting those lines. I want to keep mine, with a blank line added above and below.
Try doing this :
$ sed '/[[:lower:]]/!{a
i
}' file.txt
Here is an awk solution:
awk '!/[[:lower:]]/ {$0=RS$0RS}1' file
If line does not have any lower characters, add Record Selector (newline) before and after line, then print.
This might work for you (GNU sed):
sed '/[[:lower:]]/b;x;p;x;G' file