Printing everything except the first field with awk

前端 未结 16 2667
北恋
北恋 2020-12-04 07:04

I have a file that looks like this:

AE  United Arab Emirates
AG  Antigua & Barbuda
AN  Netherlands Antilles
AS  American Samoa
BA  Bosnia and Herzegovina         


        
16条回答
  •  甜味超标
    2020-12-04 07:42

    If you're open to a Perl solution...

    perl -lane 'print join " ",@F[1..$#F,0]' file
    

    is a simple solution with an input/output separator of one space, which produces:

    United Arab Emirates AE
    Antigua & Barbuda AG
    Netherlands Antilles AN
    American Samoa AS
    Bosnia and Herzegovina BA
    Burkina Faso BF
    Brunei Darussalam BN
    

    This next one is slightly more complex

    perl -F`  ` -lane 'print join "  ",@F[1..$#F,0]' file
    

    and assumes that the input/output separator is two spaces:

    United Arab Emirates  AE
    Antigua & Barbuda  AG
    Netherlands Antilles  AN
    American Samoa  AS
    Bosnia and Herzegovina  BA
    Burkina Faso  BF
    Brunei Darussalam  BN
    

    These command-line options are used:

    • -n loop around every line of the input file, do not automatically print every line

    • -l removes newlines before processing, and adds them back in afterwards

    • -a autosplit mode – split input lines into the @F array. Defaults to splitting on whitespace

    • -F autosplit modifier, in this example splits on ' ' (two spaces)

    • -e execute the following perl code

    @F is the array of words in each line, indexed starting with 0
    $#F is the number of words in @F
    @F[1..$#F] is an array slice of element 1 through the last element
    @F[1..$#F,0] is an array slice of element 1 through the last element plus element 0

提交回复
热议问题