Home > OS >  How to take output of first sed and use it as second sed input
How to take output of first sed and use it as second sed input

Time:10-25

<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>. with sed -n '/<acdb_ids>/,/<\/acdb_ids>/p' audio_platform_info.xml | sed -n 's/acdb_id=".*"/bit_width="24"/p' I'm able to get below output

    <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"/>

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

CodePudding user response:

You're in a right direction there. To run multiple sed patterns, use a ;, like so:

sed -n '/<acdb_ids>/,/<\/acdb_ids>/p ; s/acdb_id=".*"/bit_width="24"/p' audio_platform_info.xml

If you want to overwrite in-place use -i option.

sed -ni '/<acdb_ids>/,/<\/acdb_ids>/p ; s/acdb_id=".*"/bit_width="24"/p' audio_platform_info.xml

And if you want to overwrite and create a backup named audio_platform_info.xml.bak:

sed -ni.bak '/<acdb_ids>/,/<\/acdb_ids>/p ; s/acdb_id=".*"/bit_width="24"/p' audio_platform_info.xml

CodePudding user response:

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.

  • Related