通过从xml文件中获取元素并使用元素值替换来在文本文件中搜索的Sed或Awk

时间:2017-08-21 14:00:43

标签: xml awk sed

第一个文件XML

<div class="form-group col-md-2" >
     <select ng-model="singleSelect" ng-init="singleSelect = 1" class="form-control" required>
             <option value="2">All</option>
             <option value="1" >Male</option>
             <option value="0">Disapproved</option>
     </select>                  
</div>

第二个文件TXT

<html>
<body>
    <form method = "post">
        Student ID <input type="text" name = "student_id"> <br>
        OR NO <input type = "text" name = "or_no"> <br>
        Requests
        <input type = "checkbox" name = "ac_description[]" value = "Replacement_of_Registration"> Replacement_of_Registration <br>
        <input type = "checkbox" name = "ac_description[]" value = "Good Moral Certificate">Good Moral Certificate <br>
        <input type = "checkbox" name = "ac_description[]" value = "Honorable Dismissal ">Honorable Dismissal <br>
        <input type = "checkbox" name = "ac_description[]" value = "Transcript of Record">Transcript of Record <br>
        <input type = "checkbox" name = "ac_description[]" value = "Diploma">Diploma<br>
        <input type = "checkbox" name = "ac_description[]" value = "CUE Request"> CUE Request <br>
        <input type = "checkbox" name = "ac_description[]" value = "CMI Request">CMI Request <br>
        <input type = "checkbox" name = "ac_description[]" value = "Entrance Exam">Entrance Exam <br>
        <input type = "checkbox" name = "ac_description[]" value = "School fees-Medical/Dental Laboratory ">School fees-Medical/Dental Laboratory <br>
        <input type = "checkbox" name = "ac_description[]" value = "School fees-Transcript/Honorable">School fees-Transcript/Honorable<br>
        <input type = "checkbox" name = "ac_description[]" value = "School fees-Library">School fees-Library <br>
        <input type = "checkbox" name = "ac_description[]" value = "Affiliation Fees">Affiliation Fees <br>
        <input type = "submit" name = "submit" value = "Save"> 
    </form>
</body>
</html>

<?php
    if ($_POST["submit"]=="Save")
    {
        $student_id = $_POST["student_id"];
        $or_no = $_POST["or_no"];
        $ac_description = $_POST["ac_description"];
        print_r ($ac_description);
        $status1="processing";
        $qty=1;
        $col_credit = 80;
        $dep_credit = 80;
        $col_debit = 0;
        $dep_debit = 0;
        $db = mysqli_connect ("localhost", "root", "turtledove", "accounting");
        if (!$db)
        {
            die ("ERROR!!!!!!>>>");
        }
        if ($ac_description)
        {
                foreach ($ac_description as $ac)
                { 
                mysqli_query($db, "insert into or_header (detail_no,or_ no, ac_description, student_id, date1, status1, qty, 
                col_credit, col_debit, dep_credit, dep_debit)
                values (".$or_no.", '".mysqli_real_escape_string($db,$ac)."',".$student_id.",curdate(),".$status1.",
                ".$qty.",".$col_credit.",".$col_debit.",".$dep_credit.",".$dep_debit.")");

                }


        }
    }
?>

输出:

xml文件<channel id="Number One">1:02B18:1900:FBFF:820000:0:0:0:</channel> <!-- Number One HD --> <channel id="Number Two">1:02B65:19C8:FBFF:820000:0:0:0:</channel> <!-- Number Two HD --> <channel id="Number Three">1:0:19:2B16:1900:FBFF:82000 0:0:0:0:</channel> <!-- Number Three HD -->

中的每个“text”

查看第二个文件并在DESCRIPTION

中搜索“text”(可以包含,不区分大小写)

如果找到,则在上面的行#SERVICE中替换为x:x:x:x:x:x:x:x:在channel元素中但保留http://www.google.ts

使用上述两个文件的输出示例:

#SERVICE 1:0:19:106C:2008:FBFF:820000:0:0:0:http//www.google.ts
#DESCRIPTION Hello Number One HD
#SERVICE 1:0:0:106C:2008:FBFF:820000:0:0:0:http//www.amazon.ts
#DESCRIPTION bye Number two hd
#SERVICE 1:0:2:3:2008:FBFF:820000:0:0:0:http//www.linux.ts
#DESCRIPTION number three hd exit
#SERVICE 1:0:2:3:2008:FBFF:820000:0:0:0:http//www.goodlooking.ts
#DESCRIPTION Number Three HD
#SERVICE 1:0:19:106C:2008:FBFF:820000:0:0:0:http//www.shame.ts
#DESCRIPTION number One hd bye

1 个答案:

答案 0 :(得分:0)

请您试试,请告诉我这是否对您有所帮助?

awk '
FNR==NR{
  a[tolower($3)]=$5;
  next
}
/SERVICE/{
  prev=$0;
  next
}
/DESCRIPTION/{
  val=tolower($0);
  sub(/.*[Nn]umber/,"number",val);
  split(val, array," ");
}
((array[1] FS array[2]) in a){
  sub(/ .*:/,FS a[array[1] FS array[2]],prev);
  print prev RS $0
}
' FS='["><]' first_xml   FS=" " second_xml

上面只会查找匹配的内容,如果您还有其他条件,请告诉我。