Macro 32 Ramblings

Mind Archive

awk/sed substr

echo “USCAGoleta9311734.5021-120.1287855805” | awk ‘{print substr($0,0,2)}’


echo “USCAGoleta9311734.5021-120.1287855805” | sed ‘s/\(^..\).*/\1/’


The awk one ought to be fairly obvious, but here’s an explanation of the sed one:

  • substitute “s/”
  • the group “()” of two of any characters “..” starting at the beginning of the line “^” and followed by any character “.” repeated zero or more times “*” (the backslashes are needed to escape some of the special characters)
  • by “/” the contents of the first (and only, in this case) group (here the backslash is a special escape referring to a matching sub-expression)
  • done “/”