I have a file containing the following lines:
<parameter name="PortMappingEnabled" access="readWrite" type="xsd:boolean"></parameter> <parameter name="PortMappingLeaseDuration" access="readWrite" activeNotify="canDeny" type="xsd:unsignedInt"></parameter> <parameter name="RemoteHost" access="readWrite"></parameter> <parameter name="ExternalPort" access="readWrite" type="xsd:unsignedInt"></parameter> <parameter name="ExternalPortEndRange" access="readWrite" type="xsd:unsignedInt"></parameter> <parameter name="InternalPort" access="readWrite" type="xsd:unsignedInt"></parameter> <parameter name="PortMappingProtocol" access="readWrite"></parameter> <parameter name="InternalClient" access="readWrite"></parameter> <parameter name="PortMappingDescription" access="readWrite"></parameter>
I want to execute command on this file to extract only the parameter names as displayed in the following output:
$sedcommand file.txt PortMappingEnabled PortMappingLeaseDuration RemoteHost ExternalPort ExternalPortEndRange InternalPort PortMappingProtocol InternalClient PortMappingDescription
What could be this command?
The sed command is a common Linux command-line text processing utility. It's pretty convenient to process text files using this command. However, sometimes, the text we want the sed command to process is not in a file. Instead, it can be a literal string or saved in a shell variable.
Find and replace text within a file using sed command Use Stream EDitor (sed) as follows: sed -i 's/old-text/new-text/g' input.txt. The s is the substitute command of sed for find and replace. It tells sed to find all occurrences of 'old-text' and replace with 'new-text' in a file named input.txt.
grep was born to extract things:
grep -Po 'name="\K[^"]*'
test with your data:
kent$ echo '<parameter name="PortMappingEnabled" access="readWrite" type="xsd:boolean"></parameter> <parameter name="PortMappingLeaseDuration" access="readWrite" activeNotify="canDeny" type="xsd:unsignedInt"></parameter> <parameter name="RemoteHost" access="readWrite"></parameter> <parameter name="ExternalPort" access="readWrite" type="xsd:unsignedInt"></parameter> <parameter name="ExternalPortEndRange" access="readWrite" type="xsd:unsignedInt"></parameter> <parameter name="InternalPort" access="readWrite" type="xsd:unsignedInt"></parameter> <parameter name="PortMappingProtocol" access="readWrite"></parameter> <parameter name="InternalClient" access="readWrite"></parameter> <parameter name="PortMappingDescription" access="readWrite"></parameter> '|grep -Po 'name="\K[^"]*' PortMappingEnabled PortMappingLeaseDuration RemoteHost ExternalPort ExternalPortEndRange InternalPort PortMappingProtocol InternalClient PortMappingDescription
sed 's/[^"]*"\([^"]*\).*/\1/'
does the job.
explanation of the part inside ' '
basically s/search for this/replace with this/ but we're telling him to replace the whole line with just a piece of it we found earlier.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With