Answer the question
In order to leave comments, you need to log in
How to append multiple lines from another file to a string..?
Hello! The task is next. There is a line. And there is a certain .txt file that lies in / home.
It is necessary, in a certain place of this line, to start writing line by line data from the .txt file, and at the end create a file in which it will be ... well, something like this.
I'll try to explain clearly.
there is a line: 45788-4rbf-77458-ik-90-98
and there is a
.txt file that contains:
@96
@97
@98
@99
@100
... and so on.
It is necessary: that the data from the .txt file be written in a line, well, for example, in this place:
45788-4rbf-77458-ik-90-98
-------------^
and upon completion of the operation, get a file that would contain this:
45788-4rbf @96-77458-ik-90-98
45788-4rbf @97 -77458-ik-90-98
45788-4rbf @98 -77458-ik-90-98
45788-4rbf @99 -77458-ik-90-98
45788-4rbf @100 -77458-ik-90-98
and so on.
Thank you very much in advance!
If possible, please, on pearl. I prefer this particular language.
Answer the question
In order to leave comments, you need to log in
$ str="45788-4rbf-77458-ik-90-98"
$ cat IN.txt
@96
@97
@98
@99
@100
$ str1=$(echo $str|awk -F- '{print $1"-"$2}')
$ str2=$(echo $str|awk -F- '{print $3"-"$4"-"$5"-"$6}')
$ awk '{print "'$str1'"$0"-'$str2'"}' IN.txt > OUT.txt
$ cat OUT.txt
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
$ awk '{print "45788-4rbf"$0"-77458-ik-90-98"}' IN.txt > OUT.txt
in vim - e , launched on your txt file - quite simply:
Sitting in the first line of the txt file, we set the command:
:.,$s/^/45788-4rbf//gp
is a replacement ( from substitute, a . , -- this is the location -- from the current location to the end of the file) at the beginning of the line,
then up and repeat, but already replacing at the end of each line -- :
1G
:.,$s/$/-77458-ik-90-98/ /gp
is from a school course on regular expressions and the vim editor
Python:
with open('D:\dest.txt', 'a') as dest:
with open('D:\list.txt', 'r') as file:
for line in file.readlines():
line = line.replace('\n','')
dest.write('45788-4rbf{}-77458-ik-90-98\n'.format(line))
use Modern::Perl;
use Readonly;
use File::Slurper qw( read_text write_text );
Readonly my $STR => "45788-4rbf-77458-ik-90-98";
Readonly my $OFFSET => 10;
Readonly my $INFILE => "/home/in.txt";
Readonly my $OUTFILE => "/home/out.txt";
my @insertions = split( m/\n/, read_text(INFILE) );
my $result = '';
for my $insertion ( @insertions ) {
$result .= substr($STR, 0, $OFFSET). $insertion. substr($STR, $OFFSET). "\n";
}
# а можно так
my $result = join("\n", map {
substr($STR, 0, $OFFSET). $_. substr($STR, $OFFSET)
} @insertions;
write_text(OUTFILE, $result);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question