0
<acdb_ids>
    <device name="SND_DEVICE_OUT_SPEAKER" acdb_id="15"/>
    <device name="SND_DEVICE_OUT_SPEAKER_REVERSE" acdb_id="15"/>
    <!-- add by lifei for A2DP Dirac audio effect 20170413-->
    <device name="SND_DEVICE_OUT_BT_A2DP" acdb_id="10"/>
    <!-- add by lifei end-->
    <device name="SND_DEVICE_OUT_SPEAKER_PROTECTED" acdb_id="124"/>
    <device name="SND_DEVICE_IN_VOICE_REC_QMIC_FLUENCE" acdb_id="131"/>
    <device name="SND_DEVICE_IN_VOICE_REC_TMIC" acdb_id="131"/>
    <device name="SND_DEVICE_IN_VOICE_REC_DMIC_FLUENCE" acdb_id="132"/>
    <device name="SND_DEVICE_OUT_VOICE_SPEAKER_2_PROTECTED" acdb_id="150"/>
    <device name="SND_DEVICE_OUT_VOICE_SPEAKER_2_PROTECTED_VBAT" acdb_id="150"/>
    <device name="SND_DEVICE_IN_CAPTURE_VI_FEEDBACK_MONO_1" acdb_id="151"/>
    <device name="SND_DEVICE_IN_CAPTURE_VI_FEEDBACK_MONO_2" acdb_id="152"/>
    <device name="SND_DEVICE_IN_UNPROCESSED_USB_HEADSET_MIC" acdb_id="133"/>
    <device name="SND_DEVICE_IN_UNPROCESSED_MIC" acdb_id="143"/>
    <device name="SND_DEVICE_IN_UNPROCESSED_STEREO_MIC" acdb_id="144"/>
    <device name="SND_DEVICE_IN_UNPROCESSED_THREE_MIC" acdb_id="145"/>
    <device name="SND_DEVICE_IN_UNPROCESSED_QUAD_MIC" acdb_id="146"/>
    <device name="SND_DEVICE_IN_UNPROCESSED_HEADSET_MIC" acdb_id="147"/>
    <!-- #ifdef VENDOR_EDIT -->
    <!-- aditya.gunda@Multimedia, 2019/10/16, EIDQ-5951, fix low volume issue -->
    <device name="SND_DEVICE_IN_HANDSET_MIC_REC_SINGLE" acdb_id="4"/>
    <!-- #endif -->
</acdb_ids>

<bit_width_configs>
        <device name="SND_DEVICE_OUT_SPEAKER_PROTECTED" bit_width="24"/>
</bit_width_configs>

These line are in audio_platform_info.xml, so what I want to do is read all device names from <acdb_ids> and append those onto <bit_width_configs>. Below represents the expected output after the script has been executed.

<acdb_ids>
    <device name="SND_DEVICE_OUT_SPEAKER" acdb_id="15"/>
    <device name="SND_DEVICE_OUT_SPEAKER_REVERSE" acdb_id="15"/>
    <!-- add by lifei for A2DP Dirac audio effect 20170413-->
    <device name="SND_DEVICE_OUT_BT_A2DP" acdb_id="10"/>
    <!-- add by lifei end-->
    <device name="SND_DEVICE_OUT_SPEAKER_PROTECTED" acdb_id="124"/>
    <device name="SND_DEVICE_IN_VOICE_REC_QMIC_FLUENCE" acdb_id="131"/>
    <device name="SND_DEVICE_IN_VOICE_REC_TMIC" acdb_id="131"/>
    <device name="SND_DEVICE_IN_VOICE_REC_DMIC_FLUENCE" acdb_id="132"/>
    <device name="SND_DEVICE_OUT_VOICE_SPEAKER_2_PROTECTED" acdb_id="150"/>
    <device name="SND_DEVICE_OUT_VOICE_SPEAKER_2_PROTECTED_VBAT" acdb_id="150"/>
    <device name="SND_DEVICE_IN_CAPTURE_VI_FEEDBACK_MONO_1" acdb_id="151"/>
    <device name="SND_DEVICE_IN_CAPTURE_VI_FEEDBACK_MONO_2" acdb_id="152"/>
    <device name="SND_DEVICE_IN_UNPROCESSED_USB_HEADSET_MIC" acdb_id="133"/>
    <device name="SND_DEVICE_IN_UNPROCESSED_MIC" acdb_id="143"/>
    <device name="SND_DEVICE_IN_UNPROCESSED_STEREO_MIC" acdb_id="144"/>
    <device name="SND_DEVICE_IN_UNPROCESSED_THREE_MIC" acdb_id="145"/>
    <device name="SND_DEVICE_IN_UNPROCESSED_QUAD_MIC" acdb_id="146"/>
    <device name="SND_DEVICE_IN_UNPROCESSED_HEADSET_MIC" acdb_id="147"/>
    <!-- #ifdef VENDOR_EDIT -->
    <!-- aditya.gunda@Multimedia, 2019/10/16, EIDQ-5951, fix low volume issue -->
    <device name="SND_DEVICE_IN_HANDSET_MIC_REC_SINGLE" acdb_id="4"/>
    <!-- #endif -->
</acdb_ids>

<bit_width_configs>
    <device name="SND_DEVICE_OUT_SPEAKER" bit_width="24"/>
    <device name="SND_DEVICE_OUT_SPEAKER_REVERSE" bit_width="24"/>
    <device name="SND_DEVICE_OUT_BT_A2DP" bit_width="24"/>
    <device name="SND_DEVICE_OUT_SPEAKER_PROTECTED" bit_width="24"/>
    <device name="SND_DEVICE_IN_VOICE_REC_QMIC_FLUENCE" bit_width="24"/>
    <device name="SND_DEVICE_IN_VOICE_REC_TMIC" bit_width="24"/>
    <device name="SND_DEVICE_IN_VOICE_REC_DMIC_FLUENCE" bit_width="24"/>
    <device name="SND_DEVICE_OUT_VOICE_SPEAKER_2_PROTECTED" bit_width="24"/>
    <device name="SND_DEVICE_OUT_VOICE_SPEAKER_2_PROTECTED_VBAT" bit_width="24"/>
    <device name="SND_DEVICE_IN_CAPTURE_VI_FEEDBACK_MONO_1" bit_width="24"/>
    <device name="SND_DEVICE_IN_CAPTURE_VI_FEEDBACK_MONO_2" bit_width="24"/>
    <device name="SND_DEVICE_IN_UNPROCESSED_USB_HEADSET_MIC" bit_width="24"/>
    <device name="SND_DEVICE_IN_UNPROCESSED_MIC" bit_width="24"/>
    <device name="SND_DEVICE_IN_UNPROCESSED_STEREO_MIC" bit_width="24"/>
    <device name="SND_DEVICE_IN_UNPROCESSED_THREE_MIC" bit_width="24"/>
    <device name="SND_DEVICE_IN_UNPROCESSED_QUAD_MIC" bit_width="24"/>
    <device name="SND_DEVICE_IN_UNPROCESSED_HEADSET_MIC" bit_width="24"/>
    <device name="SND_DEVICE_IN_HANDSET_MIC_REC_SINGLE" bit_width="24"/>
</bit_width_configs>

So my question is how to append these output from first sed command to onto <bit_width_configs>.

  • 5
    [Don't Parse XML/HTML With Regex.](https://stackoverflow.com/a/1732454/3776858) I suggest to use an XML/HTML parser (xmlstarlet, xmllint ...). – Cyrus Oct 23 '21 at 15:54

2 Answers2

0

This might work for you (GNU sed):

sed -En '/<acdb_ids>/{:a;n;/<device/{s/ acdb.*//;H};/<\/acdb_ids/!ba;H;x;x}
         /<bit_width_configs/!b;p;n
         s/.* //;G;:b;s/^([^\n]*)(.*)"\n/\1\2 \1\n/;tb
         s/^.*\n\n(.*)\n.*/\1/p;n;p;q' file

In overview: save the device information, print the opening tag of bit_width_configs, append the bit_width details to the detail lines, print them and the closing tag and quit.

Turn on extended regexps and off implicit printing -En.

Match opening acdb_ids tag and filter device info (less acdb_id tag) into the hold space.

Match opening bit_width_configs tag, print it.

Fetch the next line and append the details info.

Append the bit_width to each detail line.

Remove any artefacts and print all the details.

Fetch and print the closing bit_width_configs tag.

Quit processing.

potong
  • 55,640
  • 6
  • 51
  • 83
0
sed -Ei '/<acdb_ids>/{:a;n;p;/<device/{s/ acdb.*//;H};/<\/acdb_ids/!ba;H;x;x}
     /<bit_width_configs/!b;n;p
     s/.* //;G;:b;s/^([^\n]*)(.*)"\n/\1\2" \1\n/;tb
     s/^.*\n\n(.*)\n.*/\1/p' audio_platform_info.xml
sed -Ei '/<bit_width_configs>/,/<\/bit_width_configs>/{G;/^([^\n]*). (\n.*)*\n\1(\n|$)/!{P;h};d}' audio_platform_info.xml
sed -i 's/<device name=".*"$/\
/g;/^$/!P;D;g' audio_platform_info.xml
sed -i '/^[[:space:]]*$/d' audio_platform_info.xml

For now this is my workaround for this problem, until me or somebody comes with a better solution. where, sed -Ei '/<bit_width_configs>/,/<\/bit_width_configs>/{G;/^([^\n]*)(\n.*)*\n\1(\n|$)/!{P;h};d}' audio_platform_info.xml weirds out due to sed being different in busybox.

  • You can include that information in the question using "edit" feature. It is better to not create answers for additional information – Amaimersion Oct 30 '21 at 12:19