Replace value if just first character of line matches in sed command - sed

I am trying to replace the value with N.A if the first column is less than or equal to 5 means between 0 and 5 using this command :
sed -e '/^[0-5]/ s/2/N.A/g' snp.example.1 > result2
For instance input line,
4 2 0 0 2 0 0 2 0 2 0
Converted to:
4 N.A 0 0 N.A 0 0 N.A 0 N.A 0
But instead of just checking for the first character it also looks for the second character and replaces the values with N.A.
For instance input line should not be change as it's first column contains value (33) which is greater than 5:
33 2 2 2 2 2 2 2 2 2 2
But its also get converted:
33 N.A N.A N.A N.A N.A N.A N.A N.A N.A N.A
Your kind help will be highly appreciated.

You can do this with sed, but awk seems a better choice for making numerical comparisons:
awk '$1 < 6{gsub("2","N.A")}1' input

To restrict the changes to lines whose first number is 5 or less, try:
sed -e '/^[0-5] / s/2/N.A/g' example
Note the space afer [0-5].
For example, consider this input file:
$ cat example
4 2 0 0 2 0 0 2 0 2 0
33 2 2 2 2 2 2 2 2 2 2
Our command produces:
$ sed -e '/^[0-5] / s/2/N.A/g' example
4 N.A 0 0 N.A 0 0 N.A 0 N.A 0
33 2 2 2 2 2 2 2 2 2 2

Related

Why does sed (insert line) output spaces between each character?

I have split a larger data file into individual 2-column files for each field. This results in something like this:
0.00 3.02211e+07
1.00 3.02211e+07
2.00 3.02211e+07
3.00 3.02211e+07
4.00 3.02211e+07
5.00 3.01295e+07
6.00 3.00608e+07
7.00 2.99768e+07
When I try to add a row via sed,
sed -i '1i pressure-prof' myfile.txt the output has a space character between each character (including existing spaces). If I look in notepad++, the extra spaces appear as the ASCII "NULL". In the terminal it looks like this:
pressure-prof
0 . 0 0 3 . 0 2 2 1 1 e + 0 7
1 . 0 0 3 . 0 2 2 1 1 e + 0 7
2 . 0 0 3 . 0 2 2 1 1 e + 0 7
3 . 0 0 3 . 0 2 2 1 1 e + 0 7
4 . 0 0 3 . 0 2 2 1 1 e + 0 7
5 . 0 0 3 . 0 1 2 9 5 e + 0 7
6 . 0 0 3 . 0 0 6 0 8 e + 0 7
7 . 0 0 2 . 9 9 7 6 8 e + 0 7
This is on Windows, and I think sed is being provided by cygwin or msys2. I don't know if that has anything to do with the output format issues.
Yes, I can resort to opening up files in a text editor and just adding that way. I would like to be able to utilize sed in the future though.
Thanks for any thoughts and assistance.
cat myfile.txt | tr -d ' ' | sed 's/./0 /4' | sed '1s/0 //' > mf2 && mv mf2 myfile.txt
Run that after you've finished adding your rows. Using tr initially wipes all the spaces, and then sed counts to the fourth character and re-adds a space.

Convert Group of Picture structure to the appropriate one for HEVC HM encoder

I'm using HM-16 and Scalable HM 12.3.
I have this GOP order as seen below. As you can see I have QP value for each slice type.
Encode Order Type POC QP
0 I-SLICE 0 23
1 P-SLICE 3 26
2 B-SLICE 2 27
3 b-SLICE 1 28
4 P-SLICE 6 26
5 B-SLICE 5 27
6 b-SLICE 4 28
And I want to convert it to a code like the following where I must define QPoffset CbQPoffset CrQPoffset QPfactor tcOffsetDiv2 betaOffsetDiv2 temporal_id #ref_pics_active #ref_pics reference pictures predict deltaRPS #ref_idcs reference idcs.
# Type POC QPoffset CbQPoffset CrQPoffset QPfactor tcOffsetDiv2 betaOffsetDiv2 temporal_id #ref_pics_active #ref_pics reference pictures predict deltaRPS #ref_idcs reference idcs
Frame1: P 16 1 0 0 0.6 0 0 0 2 3 -16 -24 -32 0
Frame2: B 8 2 0 0 0.2 0 0 1 2 3 -8 -16 8 1 8 4 1 1 0 1
Can you please help me to convert it?
Is there any other way to define the number of B-frames or b-frames in a GOP?
I found the solution considering HM reference manual.
I reorder the frames starting from a B-frame considering only one reference frame. All B frames reference previous I-frame or P-frames.
The new GOP structure is the one below.
It's important to mention here, that a B-frame can't have a P-frame that is encoded later. More explicitly, a B-frame with POC number e.g. 1 can't reference a P-frame with a grater value of POC number e.g. 3.
# Type POC QPoffset CbQPoffset CrQPoffset QPfactor tcOffsetDiv2 betaOffsetDiv2 temporal_id #ref_pics_active #ref_pics reference pictures predict deltaRPS #ref_idcs reference idcs
Frame1: B 1 2 0 0 0.4624 0 0 0 1 1 -1 0
Frame2: B 2 1 0 0 0.4624 0 0 0 1 1 -2 2 1
Frame3: P 3 0 0 0 0.4624 0 0 0 1 1 -3 2 2
Frame4: B 4 2 0 0 0.4624 0 0 0 1 1 -1 2 2
Frame5: B 5 1 0 0 0.4624 0 0 0 1 1 -2 2 3
Frame6: P 6 0 0 0 0.4624 0 0 0 1 1 -3 2 3

How to normalize matrix setting 0 for minimum values and 1 for maximum values?

I need to transform a neural network output matrix with size 2 X N in zeros and ones, where 0 will represent the minimum value of the column and 1 contrariwise. This will be necessary in order to calculate the confusion matrix.
For example, consider this matrix 2 X 8:
2 33 4 5 6 7 8 9
1 44 5 4 7 5 2 1
I need to get this result:
1 0 0 1 0 1 1 1
0 1 1 0 1 0 0 0
How can I do this in MATLAB without for loops? Thanks in advance.
>> d = [ 2 33 4 5 6 7 8 9;
1 44 5 4 7 5 2 1];
>> bsxfun(#rdivide, bsxfun(#minus, d, min(d)), max(d) - min(d))
ans =
1 0 0 1 0 1 1 1
0 1 1 0 1 0 0 0
The bsxfun function is necessary to broadcast the minus and division operations to matrices of different dimensions (min and max have only 1 row each).
Other solution is the following (works only for 2 rows):
>> [d(1,:) > d(2,:); d(1,:) < d(2,:)]
ans =
1 0 0 1 0 1 1 1
0 1 1 0 1 0 0 0
If it's just 2xN, then this will work:
floor(A./[max(A); max(A)])
In general:
floor(A./repmat(max(A),size(A,1),1))

Circuit that accept 4-bit number and generate its triple, what does that mean? Can someone give me an example of it?

I have to design a combinational circuit that accepts a 4-bit number and generate its triple, what does it mean? Can someone please give me an example of a specific input and its output so I can understand the question?
If you could give me also any hint for designing this circuit I would be very grateful.
Thank you.
You'll want to make a truth table and then derive the combinational logic from either boolean algebra (sum of truths), or from a k-map.
If, for example, the input is 0100 which is decimal 4, then the triple would be 12, or 1100. Since the highest number is 1111 (15), then your output has to be able to represent 45, or 101101 (6 bits).
Hence, you'll have something like:
Input | Output
-----------------
abcd uvwxyz
0000 | 000000
0001 | 000011
0010 | 000110
0011 | 001001
0100 | 001100
0101 | 001111
0110 | 010010
0111 | 010101
1000 | 011000
1001 | 011011
1010 | 011110
1011 | 100001
1100 | 100100
1101 | 100111
1110 | 101010
1111 | 101101
From that you can build a k-map for each output bit and find the minimum combinational logic required per output bit.
For example, to find the combinational logic for bit u then you would use the following k-map:
AB
00 01 11 10
CD 00 0 0 1 0
01 0 0 1 0
11 0 0 1 1
10 0 0 1 0
Which reduces to ACD + AB
Repeat for the other 5 bits (v-z) and you'll have the full combinational logic needed to implement the solution.
Start with a truth table:
IN OUT
0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 1 3 0 0 0 0 1 1
2 0 0 1 0 6 0 0 0 1 1 0
3 0 0 1 1 9 0 0 1 0 0 1
4 0 1 0 0 C 0 0 1 1 0 0
5 0 1 0 1 F 0 0 1 1 1 1
6 0 1 1 0 12 0 1 0 0 1 0
7 0 1 1 1 15 0 1 0 1 0 1
8 1 0 0 0 18 0 1 1 0 0 0
9 1 0 0 1 1B 0 1 1 0 1 1
A 1 0 1 0 1E 0 1 1 1 1 0
B 1 0 1 1 21 1 0 0 0 0 1
C 1 1 0 0 24 1 0 0 1 0 0
D 1 1 0 1 27 1 0 0 1 1 1
E 1 1 1 0 2A 1 0 1 0 1 0
F 1 1 1 1 2D 1 0 1 1 0 1
Then use a standard technique such as a Karnaugh Map to deduce the input/output expressions.

Pattern multiply - sed?

I have these kind of rows
0 1 1
I would like to multiply it by let's say 2 or 4 to get this pattern
0 0 0 0 1 1 1 1 1 1 1 1
Now, I have some piece of old code, which basically does this in the case of multiplying by 5.
But I cannot convert this script to do it for example 2 or 4 times...
Can anyone help me to figure it out?
Here is the code:
sed -e 's/\([01]\)/\1\1\1\1/7g ; s/\([01]\{2,\}\)/\1\1\1/g ; s/\b\([01]\)\b/\1\1\1\1\1/g ; s/\([01]\)\B/\1 /g'
$ echo '0 1 1' | sed -r 's/\S/& & & & &/g'
0 0 0 0 0 1 1 1 1 1 1 1 1 1 1
using sed repeat 4 times:
kent$ echo "0 1 1
1 1 0"|sed 's/[01]/& & & &/g'
0 0 0 0 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 0 0 0 0
with awk, you can give the times you want to repeat as parameter: e.g. say repeat 5 times:
kent$ echo "0 1 1
dquote> 1 1 0"|awk -v t=5 '{f=1;while(f<=NF){ n=1;while(n<=t){printf "%s ",$f;n++;}f++;} print "";}'
0 0 0 0 0 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 0 0 0 0 0
This might work for you:
echo -e '0 1 1\n1 1 1 0 0' | sed "s/\S/$(echo {1..4}| sed 's/\S*/\&/g')/g"
0 0 0 0 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0
This provides an OTT solution but it is programmable i.e. change 4 to any value you wish to multiply by.