I have a text file which looks like this:
1 1 1 \n
1 1 2 \n
1 1 3 \n
1 2 1 \n
1 2 2 \n
1 2 3 \n
1 3 1 \n
1 3 2 \n
I want to save each line in a new text file. I've developed the code already but this saves each line at the beginning of the text file.
I want to save each line of the master text file separately in a new file but at a specific line and column.
For example 1 1 1 should be in a new file [a1.text] at line 5 and colmn 10. 1 1 2 should be in a new file [a2.text] at line 4 column 10.
How can edit the code? |T tried different methods, using seek, writelines, insert but I am confused because maybe I am mixing pathlib with os.
I also have tried seek, insert. but may be i am not doing it write.
from pathlib import *
z= Path ('Desktop/pythonfiles/a.DATA').read_text().splitlines ()
for i in z:
print (i , file= open ('c:/Users/Muhammad Ali/Desktop/python files/'+i+'.DATA', 'w'))
Result:
1 1 1 saving in a newfile (a1.text) at Line 5, column 20 1 1 2 saving
in a newfile (a2.text) at line 6, column 20
You can directly read the text file as liens using readlines() method then loop over the lines printing each line in a new file while formatting the file name with an incrementing counter, as follows:
data = open('source.txt','r').readlines()
file_num = 1
for line in data:
open('a{}.txt'.format(file_num),'w').write(line)
file_num+=1
Related
I have two files -
File 1:
2 923000026531
1 923000031178
2 923000050000
1 923000050278
1 923000051178
1 923000060000
File 2:
2 923000050000
3 923000050278
1 923000051178
1 923000060000
4 923000026531
1 923335980059
I want to achieve the following using awk:
1- If 2nd field is same, sum the 1st field and print it.
2- If 2nd field is not same, print the line as it is. This will have two cases.
2(a) If 2nd field is not same & record belongs to first file
2(b) If 2nd field is not same & record belongs to second file
I have achieved the following using this command:
Command: gawk 'FNR==NR{f1[$2]=$1;next}$2 in f1{print f1[$2]+$1,$2}!($2 in f1){print $0}' f1 f2
Result:
4 923000050000
4 923000050278
2 923000051178
2 923000060000
6 923000026531
1 923335980059
However, this doesn't contains the records which were in first file & whose second field didn't match that of the second file i.e. case 2(a), to be more specific, the following record is not present in the final file:
1 923000031178
I know there are multiple work around using extra commands but I am interested if this can be somehow done in the same command.
give this one-liner a try:
$ awk '{a[$2]+=$1}END{for(x in a)print a[x], x}' f1 f2
2 923000060000
2 923000051178
1 923000031178
6 923000026531
4 923000050278
4 923000050000
1 923335980059
I want to write an AppleScript that will delete the first 10 lines of a string.
set text to "Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
Line 11
Line 12
Line 13"
So the result of variable text would be:
Line 11
Line 12
Line 13
Thanks.
Editor's note: The question's massive input/output affected readability. Deleting the first 10 will suffice. You should be able to extrapolate the solution to 30 lines.
#! /usr/bin/env bash
sed '1,10d'
I have a big genome data file (.txt) in the format below. I would like to split it based on chromosome column chr1, chr2..chrX,chrY and so forth keeping the header line in all splitted files. How can I do this using unix/linux command?
genome data
variantId chromosome begin end
1 1 33223 34343
2 2 44543 46444
3 2 55566 59999
4 3 33445 55666
result
file.chr1.txt
variantId chromosome begin end
1 1 33223 34343
file.chr2.txt
variantId chromosome begin end
2 2 44543 46444
3 2 55566 59999
file.chr3.txt
variantId chromosome begin end
4 3 33445 55666
Is this data for the human genome (i.e. always 46 chromosomes)? If so, how's this:
for chr in $(seq 1 46)
do
head -n1 data.txt >chr$chr.txt
done
awk 'NR != 1 { print $0 >>("chr"$2".txt") }' data.txt
(This is a second edit, based on #Sasha's comment above.)
Note that the parens around ("chr"$2".txt") are apparently not needed on GNU awk, but they are on my OS X version of awk.
I wonder is there a way to REVERSE THE LINE ORDER for instance
from this:
Line 5
Line 4
Line 3
Line 2
Line 1
to:
Line 1
Line 2
Line 3
Line 4
Line 5
A bit late, but yes, there is: Select the amount of lines you want to reverse and click
Edit > Permute Lines > Reverse.
I've got a formatted file in this way:
1 223614 225119.537745 Engine45
2 223614 225121.082392 Engine45
3 223614 225124.440309 Engine45
4 223614 225124.763890 Engine45
5 223621 225124.933927 Engine46
6 223614 225124.934205 Engine45
7 223614 225125.354857 Engine45
8 223614 225127.603434 Engine45
.
.
.
I'm trying to make a awk/shell that takes that 1) verify columns 2 and 4 if there equal in the same line, and if this applies, 2) substract the second found value on column 2 with the first found column 1, should be like this:
1st line found by 1):
1 223614 225119.537745 Engine45
2nd line found by 1):
2 223614 225121.082392 Engine45
Output should be the answer of this:
225121.082392 - 225119.537745 = 1.544647
The next output should be :
3rd line find by 1):
3 223614 225124.440309 Engine45
4th line find by 1):
4 223614 225124.763890 Engine45
Output: 225124.763890 - 225124.440309 = 0.323581
And successively with all records in file provided.
I believe that 1) i could make it work but 2) is really getting hard for me but if someone can give a lead how to get could be very useful, i know basics from shell and awk if it helps, but i'm open to get done on another tools like perl .
Regards
If the values of two successive lines are not same and you want to skip the lines then you can do:
awk '!(NR%2) && $2==col2 && $4==col4{print $3-col3}{col2=$2;col3=$3;col4=$4}' file
1.54465
0.323581
2.24858
If you don't want to skip the lines and print the 3rd column as is then you can do:
awk '!(NR%2){print(($2==col2&&$4==col4)?$3-col3:col3 RS $3)}{col2=$2;col3=$3;col4=$4}' file
1.54465
0.323581
225124.933927
225124.934205
2.24858
You didn't specify what to do if the values are not equal. Do you want to skip the 2 lines? Or just one of them and search for a match?
Here is a Perl solution that skips both the lines:
perl -e '
while (1) {
#first = split " ", <>;
#second = split " ", <>;
print $second[2] - $first[2], "\n" if $first[1] == $second[1] and $first[3] == $second[3];
last if eof;
}' input-file