sed delete unmatched lines between two lines with bash variable -
i need understanding weird problem sed, bash , while loop.
my data looks this:
-file 1- csv account,hostnames,status,ipaddress,port,user,pass
-file 2- xml - sample record set 2 entries under 1 account
<accountname="account"> <cname="fqdn or simple name goes here"> <field="hostname">ahostname or ipv4 goes here</field> <protocol>aprotocol</protocol> <field="port">aportnumber</field> <field="username">ausername</field> <field="password">apassword</field> </cname> <cname="fqdn or simple name goes here"> <field="hostname">ahostname or ipv4 goes here</field> <protocol>aprotocol</protocol> <field="port">aportnumber</field> <field="username">ausername</field> <field="password">apassword</field> </cname> </accountname>
so far, can add records in between respective account holder file1 file2. but, if need remove records no longer exists not work efficiently since wipes other records different accounts, ie not delete between matched accountname.
i import file 1 file 2 while loop in bash program:
-bash program excerpts- //read file in f// cat file 2 | while read f //extract fields f variables _vmname="$(echo $f |grep 'cname'| sed 's/<cname="//g' |sed 's/.\{2\}$//g')" _account="$(echo $f | grep 'accountname' | sed 's/accountname="//g' |sed 's/.\{2\}$//g')" // compare file1 , stale records still in file2 if grep "$_vmname" file1 ;then continue else // if not matched, delete between respective accountname sed -i '/'"$_account"'/,/<\/accountname>/ {/'"$_vmname"'/,/<\/cname>/d}' file2
if manually declare _vmname , _account , run
sed -i '/'"$_account"'/,/<\/accountname>/ {/'"$_vmname"'/,/<\/cname>/d}' file2
it removes stale records file2. when let bash script run, not.
i think have 3 problems:
- reading variables _vmname , _account name inside loop makes read numerous times. better way appreciated.
- i not think sed statement matching these 2 patterns , delete works want inside while loop.
- i may have logic problem thought chain.
any pointers, , please no awk, perl, lxml or python one.
thanks!
and please no awk
i appreciate want keep things simple, , suppose awk seems more complicated you're doing. i'd point out have far 3 grep , 4 sed invocations per line in file, process another file n times, once per line. that's o(mn) using slowest method on planet read file (a while loop). , doesn't work.
i may have logic problem thought chain.
i'm afraid must allow possibility!
the right advice tackle xml xml parser, because xml not regular language , can't parsed regular expressions. , that's really need here, because program processes whole xml document. you're not plucking out bits , depending on incidental formatting artifacts; want add records aren't there , remove "no longer exist". apparently there information in xml document need preserve, else produce csv. parser spoon-feed you.
the second-best advice use awk. suppose might try approach like:
- process csv , produce xml inserted.
- in awk, first read new input xml array keyed cname, process xml target once. every cname, consult array; if find match, insert pre-constructed xml replacement (or modify "paragraph" accordingly).
- i'm not sure delete criteria are, don't know if can done in same pass step #2. if not, extract salient information somehow. maybe print list of keys each of 2 files, , use comm(1) produce list of to-be-deleted. then, similar step #2, read in list, , process xml file 1 more time. write delete stderr can keep track of went missing, lines.
any pointers
whenever find processing same file n times n inputs, know you're headed trouble. 1 of 2 inputs smaller, , 1 can put in kind of array. cat file | while read
warning signal, telling use awk or of dozen obvious utilities understand lines of text.
you posted question on 2 weeks ago. suspect no 1 answered because warned them away: preemptively saying, in effect, don't tell me use tools. i'm here suggest you'll more comfortable after take off straightjacket. better tools, in case, right answer.
Comments
Post a Comment