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
Related
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.
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