bash - sed contents of file appended to specific line in another file -
i have rather complicated sed
script writes contents of file single line comma separated list in new file.
i want read file , write end of specific line of file. better read original file, write comma separated list directly specified line in new file , skip middle man.
sed -n '/# firstmark/,/# endmark/p' client_list.formal|grep -v "#"|awk -f\< '{print $2}'|awk -f\> '{print $1}'|xargs|sed -e 's/ /,\ /g'|sort -u|sed -i /writes stuff end of specified line > file.txt
the break down is:
sed -n '/ # firstmark/
(begin reading @ string),/# endmark/p'
(stop reading @ string)client_list.formal
(from file)grep -v "#"
(remove commented out lines)awk -f\< '{print$2}'|awk-f\> '{print $1}'
(print between < , >)xargs|sed -e 's/ /,\ /g'|sort -u
(put on same line, add commas, , sort unique only)the final bit should write of output end of specified line in new file.
my current work writes file. sed -i file end of every line of file (that file has 1 line. there other files multiple lines , each line have it's own list , lists built 1 source file.)
so far have seen how insert file, specific line, append end of file, not end of specified line. have on thought this? feel have on thought this.
example client_list.formal
# formal client name1
contact lastname,firstname <email@address.tld>
contact lastname,firstname <email@address.tld>
# formal client name2
contact lastname,firstname <email@address.tld>
example file written to:
email@address.tld, email@address.tld, email@address.tld, email@address.tld, email@address.tld
example file insert to:
alias1: email@address.tld, email@address.tld, email@address.tld, email@address.tld
alias2:
alias3:email@address.tld, email@address.tld, email@address.tld, email@address.tld
expected file format when operation complete:
alias1: email@address.tld, email@address.tld, email@address.tld, email@address.tld
alias2: email@address.tld, email@address.tld, email@address.tld, email@address.tld,
alias3: email@address.tld, email@address.tld, email@address.tld, email@address.tld
honestly - chain of commands big enough think it's time break out programming language perl
.
the task you're trying done something this:
#!/usr/bin/env perl use strict; use warnings; #open input file open ( $email_file, '<', 'client_list') or die $!; #read lines <email@somewhere>, , join them on ',' $address_list = join ( ",", map { m/<(.*)>/ } <$email_file> ); print "inserting $address_list\n"; close ( $email_file ); #open filename open ( $file2, '<', "insert_file_name" ) or die $!; open ( $results_file, '>', "output_file" ) or die $!; #iterate file2. while ( $line = <$file2> ) { # if condition matches, replace end of line address list above; if ( $line =~ m/alias2/ ) { $line =~ s/$/$address_list/ }; print {$results_file} $line; } close ( $file2 ); close ( $results_file );
Comments
Post a Comment