How to use sed to extract substring
You want awk
.
This would be a quick and dirty hack:
awk -F "\"" '{print $2}' /tmp/file.txt
PortMappingEnabled
PortMappingLeaseDuration
RemoteHost
ExternalPort
ExternalPortEndRange
InternalPort
PortMappingProtocol
InternalClient
PortMappingDescription
extract substring with SED
When you want to use sed
, you can choose between solutions like
# Double processing
echo "$input1" | sed 's/[^-]*-//;s/-.*//'
# Normal approach
echo "$input1" | sed -r 's/^[^-]*-([^-]*)|-.*)/\1/g'
# Funny alternative
echo "$input1" | sed -r 's/(^[^-]*-|-.*)//g'
The obvious "external" tool would be cut
. You can also look at a Bash builtin solution like
[[ ${input1} =~ ([^-]*)-([^-]*) ]] && printf %s "${BASH_REMATCH[2]}"
How to use sed or awk to extract substring
You can get this value by using multiple delimiters in awk
:
awk -F':|]' '{print $2}' $file
Extract substring with sed command
You should use a proper json parser like jq for this but if you cannot use jq for some reason, you can use sed:
sed -rn 's/(^.*My Jabber ID: )(.*)(".*$)/\2/p' file
Enable regular expressions with -r and then split the line into three sections using regular expressions, substituting the line for just the second section and printing.
How to use sed to extract the specific substring?
Assumptions:
- input is nicely formatted as per the sample provided by OP so we can use some 'simple' pattern matching
Modifying OP's current awk
code:
# use split() function to break line using dual delimiters ">" and "&"; print 2nd array entry
awk '/<p class="myforecast-current-lrg">/{ n=split($0,arr,"[>&]");print arr[2]}'
# define dual input field delimiter as ">" and "&"; print 2nd field in line that matches search string
awk -F'[>&]' ' /<p class="myforecast-current-lrg">/{print $2}'
Both of these generate:
64
One sed
idea:
sed -En 's/.*<p class="myforecast-current-lrg">([^&]+)°.*/\1/p'
This generates:
64
Extract substring from string with sed
With awk:
awk -F[:.] '{print $3}'
(define :
and .
as field delimiters and display the 3rd field)
with sed (Gnu):
sed 's/^[^:]*::\|\.0.*//g'
(replace with the empty string all that isn't a :
followed by ::
at the start of the line or the first .0
and following characters until the end of the line)
How to use sed to extract a string using a string as search parameter
Replace everything up to the last /
and everything from ?
with empty strings.
grep -m 1 'https://rancher.website.com/version1/projects/' grep.txt |
sed -e 's#.*/##' -e 's#\?.*##'
When the pattern includes /
, use a different character as the s
delimiter to avoid having to escape it; I've used #
above.
How to use SED to extract string
Using jq
is a better solution, but by sed
you should apply this:
echo '{ "requestId": "aee4f0cd-4b15-4df5-ab6b-62874963105a" }' | sed -r 's/^.*: "(.+)".*$/\1/'
How to extract text from a string using sed?
The pattern \d
might not be supported by your sed
. Try [0-9]
or [[:digit:]]
instead.
To only print the actual match (not the entire matching line), use a substitution.
sed -n 's/.*\([0-9][0-9]*G[0-9][0-9]*\).*/\1/p'
How to use sed/grep to extract text between two words?
sed -e 's/Here\(.*\)String/\1/'
Related Topics
How to Debug the Linux Kernel With Gdb and Qemu
How to Print a Number in Assembly Nasm
How to Preserve Quotes in Printing a Bash Script'S Arguments
What Happens If There Is No Exit System Call in an Assembly Program
Simulate Delayed and Dropped Packets on Linux
Using Awk to Print All Columns from the Nth to the Last
How to Install Latest Version of Git on Centos 8.X/7.X/6.X
The 'Eval' Command in Bash and Its Typical Uses
Static Link of Shared Library Function in Gcc
"Unable to Find Remote Helper For 'Https'" During Git Clone
How to Parse Xml Using Shellscript
Better Way to Rename Files Based on Multiple Patterns
Docker Can't Connect to Docker Daemon
Adding a New Entry to the Path Variable in Zsh
How to Join Multiple Lines of File Names into One With Custom Delimiter