[ale] multi-file string swap

Richard Bronosky Richard at Bronosky.com
Tue Aug 4 17:44:20 EDT 2009


Don't have much time so I'll outline for now.
*use awk for most of this, and a little vimming. Don't get too fancy
with overly complex | and $() nests
1 generate an awk script that will delete the unwanted lines in one pass. Use it
2 generate an sed script _file_ that do the replacements. Use the
script file (-f I believe) instead of trying to get it included as a
script string.

If you don't have it by 8pm I'll give more detail.

On 8/4/09, Jim Kinney <jim.kinney at gmail.com> wrote:
> OK. Last technique hunt for the day. really.
>
> 2 files, /etc/hosts and /home/foo/.ssh/known_hosts (that makes for
> long lines and different location delimeters)
>
> hosts has :
>
> ip name1 name2
>
> known_hosts has:
> name1,ip ssh-rsa <long string>
>
> I need to change name1 in known_hosts to name2 from /etc/hosts
>
> I'm looking at creating a string from each line in /etc/hosts as
> name1,ip and doing a sed substitution for name2,ip in known_hosts.
>
> A fun thing is not all hosts files have name2. Those I don't want to
> tinker with anywhere.
>
> So I have the following which outputs a space separated
> pattern/substitution set: (yay!)
>
> grep -v "^127" </etc/hosts | grep "^[0-9]" | awk '{$2=tolower($2); if
> ($3)  print $2","$1,$3","$1 }'
>
> Next step is to actually do the substitution. I tried adding more to
> the above line as:
>
> | awk '{ system(" sed -i \"s/$1/$2/\" known_hosts ") }'
>
> but no joy:
>
> sed: -e expression #1, char 0: no previous regular expression
>
> So I've poked and fiddled with the " and ' and added backslashes until
> it looks like perl code with no solution yet.
>
> So then I tried just echo'ing the strings thinking they will show up
> as positional parameters $1 and $2.
>
> | echo "$1 $2" gave nothing. I'm rather puzzled and expecting it's
> something simple yet subtle I'm missing.
>
> Ideas?
>
> --
> --
> James P. Kinney III
> Actively in pursuit of Life, Liberty and Happiness
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://mail.ale.org/mailman/listinfo/ale
>

-- 
Sent from my mobile device

.!# RichardBronosky #!.


More information about the Ale mailing list