process specifics line from text file - batch-file

i want to insert to text file like this: (the content of the text file is not like this, this is only example):
This Is The 1 line
This Is The 2 line
This Is The 3 line
This Is The 4 line
This Is The 5 line
This Is The 6 line
This Is The 7 line
This Is The 8 line
This Is The 9 line
This Is The 10 line
This Is The 11 line
This Is The 12 line
This Is The 13 line
This Is The 14 line
the new file will be like this:
This Is The 1 line
This Is The 2 line
This Is The 3 line
This Is The 4 line
This Is The 5 line
This Is The 6 line
This Is The 7 line
This Is The 8 line
This Is The 9 line
This Is The 10 line
NEW INPUT HERE
This Is The 11 line
This Is The 12 line
This Is The 13 line
This Is The 14 line
my question is how to process line 1-10, insert some text, and afterwords process line 11-14?

Here is one robust way to do it:
This uses a helper batch file called findrepl.bat (by aacini) - download from: https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat
Place findrepl.bat in the same folder as the batch file or on the path.
#echo off
(
type "file.txt"|findrepl /o:1:10
echo NEW INPUT HERE
type "file.txt"|findrepl /o:11
) > "newfile.txt"

Related

terminal, diff of two files, print out all lines, which are on the 2nd file a no on 1st file

On follow samples, every line can be empty or can have some characters. The characters can be other than numbers too. Every line can have line feeds and tabs too.
Follow looks partly fine, a don't work with more complex content:
file1.txt
1
2
3
5
file2.txt
1
4
5
working with simple sample above:
comm -1 -3 file1.txt file2.txt
Output, which is fine
4
More complex sample, which don't fit
file1.txt
0
2
3
4
5
6
7
8
10
file2.txt
1
4
6
7
8
9
10
wrong output (the 10 should not on output on this sample)
1
9
10
If your sort the contend your file1.txt and file2.txt on the same way, before running your sample code, your sample code works fine.
You can do it on follow way:
sort file1.txt > file1_sorted.txt
sort file2.txt > file2_sorted.txt
After than, use the files above, for your code:
comm -1 -3 file1_sorted.txt file2_sorted.txt

SWI-Prolog, Read the file and sum the numbers in the file

I am learning prolog and have the following problem:
Reads an input file, line by line. Then write the sum of each line to the output file.
Given an input.txt input file of the following form:
1 2 7 4 5
3 1 0 7 9
Each entry line are integers separated by a space.
? - calculate (‘input.txt’, ’output.txt’).
Here is the content of the output.txt file:
19
20
I have tried many ways but still not working, hope someone can help me
go :-
setup_call_cleanup(
open('output.txt', write, Out),
forall(file_line_sum('input.txt', Sum), writeln(Out, Sum)),
close(Out)
).
file_line_sum(File, Sum) :-
file_line(File, Line),
line_sum(Line, Sum).
line_sum(Line, Sum) :-
split_string(Line, " ", "", NumsStr),
maplist(string_number, NumsStr, Nums),
sum_list(Nums, Sum).
string_number(Str, Num) :-
number_string(Num, Str).
file_line(File, Line) :-
setup_call_cleanup(
open(File, read, In),
stream_line(In, Line),
close(In)
).
stream_line(In, Line) :-
repeat,
read_line_to_string(In, Line1),
(Line1 == end_of_file -> !, fail ; Line = Line1).
Contents of input.txt:
1 2 7 4 5
3 1 0 7 9
123 456 7890
Result in swi-prolog:
?- time(go).
% 115 inferences, 0.001 CPU in 0.001 seconds (90% CPU, 195568 Lips)
Generated output.txt:
19
20
8469

File format of 2d array octave

I have the following 2d array:
1 2 3
4 5 6
7 8 9
stored in a text file in format: [1 2 3; 4 5 6; 7 8 9;]. However when I try to load this file and save to a variable using:
a = load('data.txt'), it gives me following error:
error: load: unable to determine file format of 'data.txt'
Any suggestion on this would be nice. Thanks.
load only handles ASCII data if it's in the format shown in the first part of your post.
data.txt
1 2 3
4 5 6
7 8 9
And read it using:
data = load('data.txt', '-ascii');
If your data is stored as a formatted string rather than the ASCII matrix shown above, you'll have to read the file in a string and then use str2num to convert it to a 2D array.
fid = fopen('data.txt', 'r');
data = str2num(fread(fid, '*char').');
fclose(fid);
In the future, I would recommend storing matrices as ASCII as shown in the top part of the post

shell insert a line every n lines

I have two files and I am trying to insert a line from file2 into file1 every other 4 lines starting at the beginning of file1. So for example:
file1:
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
file2:
50
43
21
output I am trying to get:
50
line 1
line 2
line 3
line 4
43
line 5
line 6
line 7
line 8
21
line 9
line 10
The code I have:
while read line
do
sed '0~4 s/$/$line/g' < file1.txt > file2.txt
done < file1.txt
I am getting the following error:
sed: 1: "0~4 s/$/$line/g": invalid command code ~
The following steps through both files without loading either one into an array in memory:
awk '(NR-1)%4==0{getline this<"file2";print this} 1' file1
This might be preferable if your actual file2 is larger than what you want to hold in memory.
This breaks down as follows:
(NR-1)%4==0 - a condition which matches every 4th line starting at 0
getline this<"file2" - gets a line from "file2" and stores it in the variable this
print this - prints ... this.
1 - shorthand for "print the current line", which in this case comes from file1 (awk's normal input)
It is easing to do this using awk:
awk 'FNR==NR{a[i++]=$0; next} !((FNR-1) % 4){print a[j++]} 1' file2 file1
50
line 1
line 2
line 3
line 4
43
line 5
line 6
line 7
line 8
21
line 9
line 10
While processing first file in input i.e. file2, we store each line in array with key as an incrementing number starting with 0.
While processing second file in input i.e. file1, we check if current record # is divisible by 4 using modulo arithmetic and if it is then insert a line from file2 and increment the index counter.
Finally using action 1, we print lines from file1.
This might work for you (GNU sed):
sed -e 'Rfile1' -e 'Rfile1' -e 'Rfile1' -e 'Rfile1' file2
or just use cat and paste:
cat file1 | paste -d\\n file2 - - - -
another alternative with unix toolchain
$ paste file2 <(pr -4ats file1) | tr '\t' '\n'
50
line 1
line 2
line 3
line 4
43
line 5
line 6
line 7
line 8
21
line 9
line 10
Here's a goofy way to do it with paste and tr
paste file2 <(paste - - - - <file1) | tr '\t' '\n'
Assumes you don't have any actual tabs in your input files.

return value of gets command

what does the return value of gets command in following exercise means?
I tried to read file by tclsh on command line.
File
10 2 12 1 13
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4a 5
Command & Output (command-line)
% set fp [open file r]
file4
% gets $fp line
45
% gets $fp line
42
% gets $fp line
41
% gets $fp line
42
% gets $fp line
-1
% close $fp
when I got -1 output I closed the file-pointer $fp. but what does the values 45 42 41 42 meant?
Quoting from the man page of gets command
Syntax :
gets channelId ?varName?
This command reads the next line from channelId, returns everything in
the line up to (but not including) the end-of-line character(s), and
discards the end-of-line character(s).
If varName is specified and an empty string is returned in varName
because of end-of-file or because of insufficient data in nonblocking
mode, then the return count is -1.
If varName is omitted the line is returned as the result of the
command. If varName is specified then the line is placed in the
variable by that name and the return value is a count of the number of
characters returned.
As you can see from the man page, it returns the count of characters which is read by gets command in one single line.
In the first line of the file, it read 45 characters and returned 45 as a result and the string value will be stored in the variable line as per your code.
This is repeated for all lines and will return -1 once the eof reached for that file.

Resources