Created
February 21, 2014 08:17
-
-
Save gkeramidas/9130579 to your computer and use it in GitHub Desktop.
AWK or sed for removing the first 'field' of a line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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