Good utilities for extracting stuff from text
tr -dc '[:print:]'tr '\n' ' '| tr -d '\r'sed 'N;s/\n//'awk '{printf $0}'sed 's/.*xx\(.*\)yy.*/\1/'sed 's/^.*\[\(.*\)\].*$/\1/'This boils down to a "substitute everything with the back reference"
so 's/everything/\1/'
or 's/the beginning (^) then some chars (.*) then the opening bracket (escaped \[)
then the chars after the opening bracket which we want printed (must be placed in parenthesis) which must also
be escaped \( .* \) then the closing bracket (escaped \]) then all after the closing
bracket (.*) up to the end ($). The \1 means the first back reference (everything in the first set
of parenthisis (there is only one set)
so here it is using spaces for clarity (which must be removed afterwards)
' s / ^ .* \[ \( .* \) \] .* $ / \1 / '
Example: Extract the WAN IP address from a Belkin F7D7302 V1 Share 300 Wireless Router
#!/bin/sh - # Get WAN IP from a Belkin F7D7302 Share N300 wireless router # The target page is status.stm # We need the line containing <space>wan_ip=" # From that line extract everything between the first quote and the space before the last quote wanip=$(curl -s 192.168.1.1/status.stm | sed -n '/wan_ip="/p' | sed 's/.*"\(.*\) ".*/\1/') echo $wanip exit 1
sed '/^$/q'sed '1,/^$/d'sed 's/^> //'sed 's/^/> /'gawk "{ print \"\042\" \$0 \"\042\" }" filesed -n '/pattern/p'sed -n 'lineno p'sed -ne 'lineno p' -e '/pattern/ p'sed -ne 'lineno1 p' -e 'lineno2 p'sed -ne '/pattern/,+1p'sed -ne '/pattern/,+1p' | sed '$!d'sed -n -e '/begin/,/^$/p'awk '{ print FNR, $0 }'awk '{ print $1, $2, $7 }'awk '/hal/{print$2,$3}'a="Harry's hat"
echo $a | awk '{ gsub(/[^[:alnum:]]/, "_");print }'A range is two addresses separated by a comma. /pattern1/ , /pattern2/
Can't use alternation with sed but you can with grep
awk will not accept parameters until after it has opened
the input file and that doesn't happen until after the BEGIN block has
finished. If you want to use awk as a calculator with no input file you
can build the command string in advance and then run it with eval:
a=79
b="awk 'BEGIN {print $a / 3}'"
c=$(eval $b)
echo $c --> 26.3333
Bash string extractions and other manipulations
if [[ "$string" =~ $substring ]]; then
String length
${#string}
or
expr length $string
Length of Matching Substring at Beginning of String
expr match "$string" '$substring'
$substring is a regular expression.
Index - Numerical position in $string of first character in $substring that matches.
expr index $string $substring
Substring Extraction - Extracts substring from $string at $position.
${string:position}
If the $string parameter is "*" or "@", then this extracts the
positional parameters, [1] starting at $position
Extracts $length characters of substring from $string at $position.
${string:position:length}
Extracts $length characters of substring from $string at $position.
Other neat stuff
![]() |
This site best viewed with a browser |
| Warning: This is a Debian centric site and MAY contain peanuts. | |
| Many thanks to Debra Lynn and Ian Murdock for making Debian possible | |
| First created Apr 22, 2008 ~ Last revised April 10, 2011 |