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

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.

Related

sed replace # with empty line

How do you replace a line that only has # with an empty line using sed?
I have tried to find on google but I haven't gotten anything.
File Content:
#test
another test
#
another test2
Expected result:
#test
another test
another test2
So, under expected result, after another test the line should be blank without the #.
Any help is greatly appreciated.
With regular expressions you can match for the beginning of a line with ^ and the end of a line with $. The s/regexp/replacement/ command will replace text that matches regexp with replacement.
This sed command gives the desired output:
sed 's/^#$//' < input.txt
On each line, sed looks for the start of a line, a # character, and then the end of a line, and replaces it with nothing. The newline character remains, however, so you are left with a blank line.
sed '/^#$//'
Anchor to beginning (^) and end ($) of line to match a whole line exactly.
Using sed
$ sed '/[[:alnum:]]/ ! s/#//' file
#test
another test
another test2
This might work for you (GNU sed):
sed '/^#$/g' file
If a line contains only #, replace it by a blank line.
Alternatives:
sed 's/^#$//' file
or
sed '/^#$/c\\' file

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.

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 to delete lines containg n charcters

I have many lines in a file which only contain '--' on each line which i want to rmeove. But there are many other lines in the file that contain 'SOMETEXT--SOMETEXT'.
sed -i "/--/d" will remove all instances of '--' but I only want to remove all lines that contain only '--'.
You can use ^ and $ to indicate beginning and end of line
sed -i '/^--$/d'
A line containing only -- would match the regex ^--$
If you want to include lines with leading/trailing whitespaces, it could be extended to
^\s*--\s*$
sed -i '/^--$/' file
The ^ and $ chars "anchor" the search to the beginning and end of the line, respectively.
OR if there can be spaces at the front or back AND assuming an modernish sed
sed -i '/^[[:space:]]*--[[:space:]]*$/' file
where [:space:] will find space chars and tabs.
ELSE a total retro sed should handle
sed '/^[ ]*--[ ]*$/' file > newFile && mv newFile file
and if there could be tabs, then just include a tab char along with the space char, i.e.\
[<Space><TAB>]
but not spelled, out, just typing a space char and a tab char will do it.
IHTH

replace last character in a line with sed

I am trying to replace the last character in a line with the same character plus a quotation mark '
This is the sed code
sed "s/\([A-Za-z]\)$/\1'/g" file1.txt > file2.txt
but does not work. Where is the error?
try:
sed "s/\([a-zA-Z]\)\s*$/\1\'/" file
This will replace the last character in the line followed by none or many spaces.
HTH Chris
It seems pointless to replace a character with itself, so try this: for lines ending with a letter, add a quote to the end:
sed "/[a-zA-Z]$/s/$/'/"
This does what you ask for:
sed "s/\(.\)$/\1'/" file1.txt > file2.txt
Your line only matches a line with a single character. Note that the s operation only takes effect if the line matches, not if only a subset of the line matches the regex.