Skip to content

Instantly share code, notes, and snippets.

@gkeramidas
Created February 21, 2014 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gkeramidas/9130579 to your computer and use it in GitHub Desktop.
Save gkeramidas/9130579 to your computer and use it in GitHub Desktop.
AWK or sed for removing the first 'field' of a line
# This should print 'second:third'
echo first:second:third | awk -F: '{ print substr($0, index($0, FS) + 1) }'
# Same here, but shorter code.
echo first:second:third | sed -e 's/^[^:]*://'
# Slightly more verbose awk version for 'second:third' output
echo first:second:third | awk -F: '{ for (k = 2; k < NF; k++) { printf $k FS } ; print $NF }'
# The verbose version can be generalized to remove any other field too,
# e.g. the second field, and print 'first:third'
echo first:second:third | awk -F: '{ for (k = 1; k < NF; k++) { if (k != 2) printf $k FS } ; print $NF }'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment