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:

  1. sed -n '/ # firstmark/ (begin reading @ string)

  2. ,/# endmark/p' (stop reading @ string)

  3. client_list.formal (from file)

  4. grep -v "#" (remove commented out lines)

  5. awk -f\< '{print$2}'|awk-f\> '{print $1}' (print between < , >)

  6. xargs|sed -e 's/ /,\ /g'|sort -u (put on same line, add commas, , sort unique only)

  7. 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

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -