Can I use awk to specify in which column to place string? - sed

I have a tab separated text file called test.txt with multiple columns that I'm trying to make identical to another file called output.txt.
The test.txt looks as follows
t m sx sy sz rx ry rz
49.07 0 -1.00 0.00 -0.11 20.00 0.00 -2.18
49.47 0 -1.00 0.00 -0.11 22.00 0.00 -2.33
50.89 0 -1.00 0.00 -0.11 34.00 0.00 -3.21
.
:
42.06 0 29.00 0.00 -2.86 12.00 0.00 -1.44
The problem is that, no matter what type of delimiter I use, still it will not have the same form as the desired output file called output.txt
In the output.txt, all these columns have a specific location, so
t m sx sy sz rx ry rz
Ln1,col1 Ln1,col9 Ln1,col17 Ln1,col25 Ln1,col33 Ln1,col41 Ln1,col49 Ln1,col57
I'm a bit new with awk, sed and most of other unix commands. Any suggestion?

have you tried something like this?
awk -F 'BEGIN{print "t\tm\tsx\tsy\tsz\trx\try\trz"}{print $1"\t"$9"\t"$17"\t"$25"\t"$33"\t"$41"\t"$49}' test.txt
You print the header at the beginning and then for each line you print the desired columns

example for printf:
awk '{for (i=1;i<=NF;i++) printf(\"%-9s\", $i); print}' file
more examples

Related

What does `A(::2,3) = -1.0` do in Fortran?

I have a matrix A declared as real :: A(7,8) and intialised so that all entries are 0.0.
The following command does not provide any compiling errors.
A(::2,3) = -1.0
I realise that the columns affected will be only column 3.
What about the rows? Does ::2 mean rows 1 and 2? Or something else?
I printed out the matrix, but couldn't understand the pattern produced.
Here (for completeness):
do, i=1,7
write(*, "(f5.2)") ( A(i,j), j=1,8 )
enddo
0.00 i = 1
0.00
-1.00
0.00
0.00
0.00
0.00
0.00 ----
0.00 i = 2
0.00
0.00
0.00
0.00
0.00
0.00
0.00 ----
0.00 i = 3
0.00
-1.00
0.00
0.00
0.00
0.00
0.00 ----
0.00 i = 4
0.00
0.00
0.00
0.00
0.00
0.00
0.00 ----
0.00 i = 5
0.00
-1.00
0.00
0.00
0.00
0.00
0.00 ----
0.00 i = 6
0.00
0.00
0.00
0.00
0.00
0.00
0.00 ----
0.00 i = 7
0.00
-1.00
0.00
0.00
0.00
0.00
0.00
Looking at it now, it looks like it starts at i=1 and adds 2 to i until it reaches the bounds of the matrix. Is this correct?
Does this mean that ::2 is equivalent to 1:7:2 ("from 1 to 7 with a step of 2)?
Looking at the documentation, we see:
print array-expression [first-expression : last-expression : stride-expression]
where:
array-expression Expression that should evaluate to an array type.
first-expression First element in a range, also first element to be
printed. Defaults to lower bound.
last-expression Last element in a range, but might not be the last
element to be printed if stride is not equal to 1. Defaults to upper
bound.
stride-expression Length of the stride. Defaults to 1.
So if first-expression and last-expression are omitted, they default to lower bound and upper bound respectively.
Yes, that's right - it's the same as 1:7:2, as you can see from the output, it's setting the 3rd element in the sub-array to -1 for every 2nd sub array

Create table using Matlab fprintf

Suppose I have four vectors x,y,z,c
How do I get matlab to display it using fprintf in a table form with titles above each column like "title 1" and the x column below it.
Here is a short example to get you going. I suggest reading the docs about fprintf also.
clear
clc
%// Dummy data
x = .1:.1:1;
y = 2:2:20;
z = x+y;
%// Concatenate data
A = [x; y ; z];
%// Open file to write
fileID = fopen('MyTable.txt','w');
%// Select format for text and numbers
fprintf(fileID,'%6s %6s %6s\n','x','y','z');
fprintf(fileID,'%.2f \t %.2f \t %.2f\n',A);
fclose(fileID);
Checking what MyTable looks like:
type ('MyTable.txt');
x y z
0.10 2.00 2.10
0.20 4.00 4.20
0.30 6.00 6.30
0.40 8.00 8.40
0.50 10.00 10.50
0.60 12.00 12.60
0.70 14.00 14.70
0.80 16.00 16.80
0.90 18.00 18.90
1.00 20.00 21.00
Hope that helps!

insert word between lines

I have pdb (protein data base) file which has thousands of lines.
REMARK 1 PDB file generated by ptraj (set 1000)
ATOM 1 O22 DDM 1 2.800 4.419 20.868 0.00 0.00
ATOM 2 H22 DDM 1 3.427 4.096 20.216 0.00 0.00
ATOM 3 C22 DDM 1 3.351 5.588 21.698 0.00 0.00
ATOM 4 H42 DDM 1 3.456 5.274 22.736 0.00 0.00
ATOM 5 C23 DDM 1 2.530 6.846 21.639 0.00 0.00
ATOM 6 H43 DDM 1 2.347 7.159 20.611 0.00 0.00
ATOM 7 O23 DDM 1 1.313 6.498 22.334 0.00 0.00
ATOM 8 H23 DDM 1 0.903 5.837 21.771 0.00 0.00
ATOM 9 C24 DDM 1 3.073 8.109 22.266 0.00 0.00
ATOM 10 H44 DDM 1 3.139 7.837 23.319 0.00 0.00
ATOM 11 O24 DDM 1 2.218 9.278 22.007 0.00 0.00
ATOM 12 H24 DDM 1 1.278 9.184 22.179 0.00 0.00
ATOM 13 C25 DDM 1 4.494 8.317 21.764 0.00 0.00
ATOM 14 H45 DDM 1 4.391 8.452 20.687 0.00 0.00
'
I want to insert word "TER" every 81 lines in that file whcih contains more than 20,000 lines but ignoring the first line since it is a comment.
I browse through internet, seems SED can do it. But i am lost.
Can anyone guide?
Thanks in advance.
Try this:
sed -i -e '1~81 i\TER' file
I'm partial to awk myself:
awk '{if(FNR%81==0)print "TER"; print}' file
I find this is a lot easier to understand and debug than the sed equivalent. The only magic is that FNR is the line number
You might have to fiddle with the numbers in the if to get it exactly the way you want it.
The more verbose shell commands would be
{
read header
echo "$header"
i=0
while read line; do
echo "$line"
if (( ++i == 81 )); then
echo TER
i=0
fi
done
} < infile > outfile &&
mv outfile infile

matlab: change matrix

I have a code that load cell array and convert them to matrix.
now this matrix shows 4 numbers after floating point for example
0 5 15 1 51,9000 3,4000
0 5 15 1 51,9000 3,4000
0 5 15 1 51,9000 3,4000
how can I change all af the rows to just show 2 numbers after the floating point ?
please consider that I want to change the matrix not print it in command window !
If you want to see it in the command window/editor for debugging purposes, use bank format:
format bank;
Example:
A =[ 51.213123 6.132434]
format bank
disp(A);
Will result in :
A =
51.21 6.13
Also, you can use sprintf
A = [51.900 3.4000];
disp(sprintf('%2.2f ',A));
x = [0 5 15 1 51.9000 3.4000
0 5 15 1 51.9000 3.4000
0 5 15 1 51.9000 3.4000];
fprintf([repmat('%.2f ',1,size(x,2)) '\n'], x')
0.00 5.00 15.00 1.00 51.90 3.40
0.00 5.00 15.00 1.00 51.90 3.40
0.00 5.00 15.00 1.00 51.90 3.40

match a pattern and print subsequent lines

there are 200 files named File1_0.pdb,File1_60.pdb etc....it looks like:
ATOM 1 N VAL 1 8.897 -21.545 -7.276 1.00 0.00
ATOM 2 H1 VAL 1 9.692 -22.015 -6.868 1.00 0.00
ATOM 3 H2 VAL 1 9.228 -20.766 -7.827 1.00 0.00
ATOM 4 H3 VAL 1 8.289 -22.236 -7.693 1.00 0.00
TER
ATOM 5 CA VAL 1 8.124 -20.953 -6.203 1.00 0.00
ATOM 6 HA VAL 1 8.072 -19.874 -6.345 1.00 0.00
ATOM 7 CB VAL 1 6.693 -21.515 -6.176 1.00 0.00
ATOM 8 HB VAL 1 6.522 -22.024 -5.227 1.00 0.00
ATOM 9 CG1 VAL 1 5.684 -20.370 -6.330 1.00 0.00
ATOM 10 1HG1 VAL 1 5.854 -19.861 -7.279 1.00 0.00
i have to extract the part after TER and put in a different file...this has to be done on all 200 files. I did something like sed '1,/TER/d' File1_0.pdb > 1_0.pdb. But this will work for one file at a time...can there be a solution for all 200 files in one go... output file is named same only "File" is removed from the name...
for i in *.pdb; do sed '1,/TER/d' $i > ${i/File/}; done
This might work:
seq 0 200| xargs -i -n1 cp File1_{}.pdb 1_{}.pbd # backup files
sed -si '1,/TER/d' 1_{0..200}.pdb # edit files separately inline