แก้ไขไฟล์ XML โดยใช้คำสั่งเดียวกันบน mac, windows และ linux

กำลังพยายามเปลี่ยนชื่อ = "Android" เป็น name = "Android1", name = "Android2" และอื่น ๆ ..

จะต้องเรียกใช้สคริปต์ Python ของฉันโดยใช้บรรทัดคำสั่งบน windows, macOS และ Linux

Output2.xml

<?xml version="1.0" encoding="UTF-8"?> 
    <robot generator="Robot 2.9 (Python 3.6.8 on linux)" generated="20190701 08:47:35.439"> 
    <suite id="s1" name="Android" source="/FILEPATHHERE">
    #ommitted unrelevant lines from xml file

ปัญหา

ฉันได้ลองสองวิธีที่ระบุไว้ใน Stackoverflow แล้ว แต่พบว่ามันไม่ง่ายอย่างที่คิด

เพิ่งลองใช้วิธีใช้เทอร์มินัล mac ของฉันและเมื่อฉันลองใช้สิ่งนี้:

sed -i 's/android/newtext' output2.xml 

ข้อผิดพลาด: sed: 1: "output2.xml": รหัสคำสั่งไม่ถูกต้อง o

เพิ่มใน '' แต่มีข้อผิดพลาดใหม่เกิดขึ้นอีก:

sed -i '' 's/android/newtext' output2.xml

ข้อผิดพลาด: sed: 1: "s/android/newtext": การทดแทนที่ไม่สิ้นสุดในนิพจน์ทั่วไป

ฉันสามารถใช้คำสั่ง sed ได้หรือไม่? ดูเหมือนว่าฉันทำผิดเนื่องจากมีปัญหาในการแก้ไข XML โดยใช้คำสั่ง sed

ข้อมูลอ้างอิงหลัก

https://askubuntu.com/questions/20414/find-and-replace-text-within-a-file-using-commands https://superuser.com/questions/916665/edit-xml-file-using-shell-script-command


person Mason    schedule 08.07.2019    source แหล่งที่มา


คำตอบ (1)


คุณต้องให้ตัวคั่นส่วนท้ายสำหรับรูปแบบการแทนที่ อย่างไรก็ตาม เนื่องจากคุณระบุแฟล็ก -i สำหรับไฟล์สำรองข้อมูล มันไม่ควรเป็นส่วนขยายที่มีความยาวเป็นศูนย์สำหรับสิ่งนั้น อย่างน้อยสำหรับ macOS ให้ตรวจสอบ man page เกี่ยวกับมัน

sed -i 'new' 's/android/newtext/' output2.xml
person Itachi    schedule 08.07.2019