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.
Related
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
I want to substring the File name in unix using sed command.
File name : Test_Test1_Test2_10082019_030013.csv.20191008-075740
I want the characters after the 3rd underscore or (all the characters after Test2 ) i need to be printed .
Can this be done using sed command?
I have tried this command
sed 's/^.*_\([^_]*\)$/\1/' <<< 'Test_Test1_Test2_10082019_030013.csv.20191008-075740'
but this is giving result as 030013.csv.20191008-075740
I need it from 10082019_030013.csv.20191008-075740
Thanks
Neha
To remove from the beginning up to including the 3rd underscore you can use
sed 's/^\([^_]*_\)\{3\}//' <<< 'Test_Test1_Test2_10082019_030013.csv.20191008-075740'
This removes the initial part that consists of 3 groups of (any number of non-underscore characters followed by an underscore). The result is
10082019_030013.csv.20191008-075740
If you use GNU sed you can switch it to extended regular expressions and omit the backslashes.
sed -r 's/^([^_]*_){3}//' <<< 'Test_Test1_Test2_10082019_030013.csv.20191008-075740'
Could you please try following.
sed 's/\([^_]*\)_\([^_]*\)_\([^_]*\)_\(.*\)/\4/' Input_file
Or as per Bodo's nice suggestion:
sed 's/[^_]*_[^_]*_[^_]_\(.*\)/\1/' Input_file
This might work for you (GNU sed):
sed 's/_/\n/3;s/.*\n//;t;s/Test2/\n/;s/.*\n//;t;d' file
Replace the third _ by a newline and then remove everything upto and including the first newline. If this succeeds, bail out and print the result. Otherwise, try the same method with Test2 and if this fails delete the entire line.
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.
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
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