用awk打印除第一个字段之外的所有内容

时间:2022-06-01 22:07:23

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
BF  Burkina Faso
BN  Brunei Darussalam

And I 'd like to invert the order, printing first everything except $1 and then $1:

我想调换一下订单,先打印所有东西,除了1美元,然后是1美元:

United Arab Emirates AE

How can I do the "everything except field 1" trick?

我怎么能做“除第一个领域外的一切”的把戏?

15 个解决方案

#1


64  

Assigning $1 works but it will leave a leading space: awk '{first = $1; $1 = ""; print $0, first; }'

分配$1可以工作,但是它会留下一个前导空间:awk '{first = $1;1美元= " ";打印0美元,第一;} '

You can also find the number of columns in NF and use that in a loop.

您还可以在NF中找到列的数量,并在循环中使用它们。

#2


76  

$1="" leaves a space as Ben Jackson mentioned, so use a for loop:

$1="" "就像Ben Jackson提到的那样,留下一个空格,所以使用for循环:

awk '{for (i=2; i<=NF; i++) print $i}' filename

So if your string was "one two three", the output will be:

所以如果你的字符串是“1 / 3”,输出将是:

two
three

两个三

If you want the result in one row, you could do as follows:

如果你想把结果放在一行,你可以这样做:

awk '{for (i=2; i<NF; i++) printf $i " "; print $NF}' filename

This will give you: "two three"

这将给你:“two three”

#3


51  

Use the cut command with the --complement option:

使用cut命令和补语选项:

$ echo a b c | cut -f 1 -d ' '
a
$ echo a b c | cut -f 1,2 -d ' '
a b
$ echo a b c | cut -f 1 -d ' ' --complement
b c

#4


13  

Maybe the most concise way:

也许是最简洁的方式:

$ awk '{$(NF+1)=$1;$1=""}sub(FS,"")' infile
United Arab Emirates AE
Antigua & Barbuda AG
Netherlands Antilles AN
American Samoa AS
Bosnia and Herzegovina BA
Burkina Faso BF
Brunei Darussalam BN

Explanation:

解释:

$(NF+1)=$1: Generator of a "new" last field.

$(NF+1)=$1:新字段的生成器。

$1="": Set the original first field to null

$1="":将原始的第一个字段设置为null

sub(FS,""): After the first two actions {$(NF+1)=$1;$1=""} get rid of the first field separator by using sub. The final print is implicit.

sub(FS,""):在前两个动作{$($(NF+1)=$1;$1=""}之后,使用sub去除第一个字段分隔符。最终打印是隐式的。

#5


9  

awk '{ saved = $1; $1 = ""; print substr($0, 2), saved }'

Setting the first field to "" leaves a single copy of OFS at the start of $0. Assuming that OFS is only a single character (by default, it's a single space), we can remove it with substr($0, 2). Then we append the saved copy of $1.

将第一个字段设置为“”将在$0开头保留一个OFS副本。假设OFS只是一个字符(默认情况下,它是一个单独的空格),我们可以使用substr($ 0,2)删除它。

#6


9  

awk '{sub($1 FS,"")}7' YourFile

Remove the first field and separator, and print the result (7 is a non zero value so printing $0).

删除第一个字段和分隔符,并打印结果(7是一个非零值,因此打印$0)。

#7


3  

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

如果您愿意使用Perl解决方案……

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

    -n循环每一行输入文件,不要自动打印每一行

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

    -l在处理之前删除新行,之后再添加

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

    -自动分割模式-将输入行分割成@F数组。默认在空格上进行分割

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

    -F autosplit修饰符,在这个例子中分割为' '(两个空格)

  • -e execute the following perl code

    -e执行以下perl代码

@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

@F是每行单词的数组,从0 $#F开始索引,是@F @F[1.. ..$#F]是元素1通过最后一个元素@F[1]的数组切片。$#F,0]是元素1通过最后一个元素加上元素0的数组切片

#8


2  

The field separator in gawk (at least) can be a string as well as a character (it can also be a regex). If your data is consistent, then this will work:

gawk中的字段分隔符(至少)可以是字符串,也可以是字符(也可以是regex)。如果你的数据是一致的,那么这将会起作用:

awk -F "  " '{print $2,$1}' inputfile

That's two spaces between the double quotes.

这是双引号之间的两个空格。

#9


2  

awk '{ tmp = $1; sub(/^[^ ]+ +/, ""); print $0, tmp }'

awk '{tmp = $1;子(/ ^ ^ + + /," ");打印0美元,tmp }’

#10


2  

Let's move all the records to the next one and set the last one as the first:

让我们把所有的记录移到下一个记录上,把最后一个作为第一个:

$ awk '{a=$1; for (i=2; i<=NF; i++) $(i-1)=$i; $NF=a}1' file
United Arab Emirates AE
Antigua & Barbuda AG
Netherlands Antilles AN
American Samoa AS
Bosnia and Herzegovina BA
Burkina Faso BF
Brunei Darussalam BN

Explanation

  • a=$1 save the first value into a temporary variable.
  • a=$1将第一个值保存到一个临时变量中。
  • for (i=2; i<=NF; i++) $(i-1)=$i save the Nth field value into the (N-1)th field.
  • (我= 2;我< = NF;$(i-1)=$i将第n个字段值保存到第N-1个字段中。
  • $NF=a save the first value ($1) into the last field.
  • $NF=a将第一个值($1)保存到最后一个字段。
  • {}1 true condition to make awk perform the default action: {print $0}.
  • {}1使awk执行默认操作的真实条件:{打印$0}。

This way, if you happen to have another field separator, the result is also good:

这样,如果您碰巧有另一个字段分隔符,结果也很好:

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

$ awk 'BEGIN{OFS=FS="-"}{a=$1; for (i=2; i<=NF; i++) $(i-1)=$i; $NF=a}1' c
United-Arab-Emirates-AE
Antigua-&-Barbuda-AG
Netherlands-Antilles-AN
American-Samoa-AS
Bosnia-and-Herzegovina-BA
Burkina-Faso-BF
Brunei-Darussalam-BN

#11


1  

A first stab at it seems to work for your particular case.

第一次尝试似乎对你的特殊情况有效。

awk '{ f = $1; i = $NF; while (i <= 0); gsub(/^[A-Z][A-Z][ ][ ]/,""); print $i, f; }'

#12


1  

Option 1

There is a solution that works with some versions of awk:

有一种解决方案可以用于awk的一些版本:

awk '{ $(NF+1)=$1;$1="";$0=$0;} NF=NF ' infile.txt

Explanation:

解释:

       $(NF+1)=$1                          # add a new field equal to field 1.
                  $1=""                    # erase the contents of field 1.
                        $0=$0;} NF=NF      # force a re-calc of fields.
                                           # and use NF to promote a print.

Result:

结果:

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

However that might fail with older versions of awk.

然而,对于较老版本的awk来说,这可能会失败。


Option 2

awk '{ $(NF+1)=$1;$1="";sub(OFS,"");}1' infile.txt

That is:

那就是:

awk '{                                      # call awk.
       $(NF+1)=$1;                          # Add one trailing field.
                  $1="";                    # Erase first field.
                        sub(OFS,"");        # remove leading OFS.
                                    }1'     # print the line.

Note that what needs to be erased is the OFS, not the FS. The line gets re-calculated when the field $1 is asigned. That changes all runs of FS to one OFS.

注意,需要删除的是OFS,而不是FS。当字段$1被赋值时,将重新计算行。这将所有的FS运行都更改为一个OFS。


But even that option still fails with several delimiters, as is clearly shown by changing the OFS:

但是,即使是这个选项也会因为有几个分隔符而失败,正如改变OFS所显示的那样:

awk -v OFS=';' '{ $(NF+1)=$1;$1="";sub(OFS,"");}1' infile.txt

That line will output:

这条线将输出:

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

That reveals that runs of FS are being changed to one OFS.
The only way to avoid that is to avoid the field re-calculation.
One function that can avoid re-calc is sub.
The first field could be captured, then removed from $0 with sub, and then both re-printed.

这表明,FS的运行被更改为一个OFS。避免这种情况的唯一方法是避免重新计算字段。一个可以避免重新计算的函数是sub.可以捕获第一个字段,然后用sub从$0中删除,然后再重新打印。

Option 3

awk '{ a=$1;sub("[^"FS"]+["FS"]+",""); print $0, a;}' infile.txt
       a=$1                                   # capture first field.
       sub( "                                 # replace: 
             [^"FS"]+                         # A run of non-FS
                     ["FS"]+                  # followed by a run of FS.
                            " , ""            # for nothing.
                                  )           # Default to $0 (the whole line.
       print $0, a                   # Print in reverse order, with OFS.


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

Even if we change the FS, the OFS and/or add more delimiters, it works.
If the input file is changed to:

即使我们更改FS、OFS和/或添加更多的分隔符,它也可以工作。如果输入文件更改为:

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

And the command changes to:

命令改为:

awk -vFS='.' -vOFS=';' '{a=$1;sub("[^"FS"]+["FS"]+",""); print $0,a;}' infile.txt

The output will be (still preserving delimiters):

输出将是(仍然保留分隔符):

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

The command could be expanded to several fields, but only with modern awks and with --re-interval option active. This command on the original file:

该命令可以扩展到多个字段,但只能使用现代awks和——reinterval选项活动。原始文件上的这个命令:

awk -vn=2 '{a=$1;b=$2;sub("([^"FS"]+["FS"]+){"n"}","");print $0,a,b;}' infile.txt

Will output this:

将输出:

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

#13


0  

There's a sed option too...

还有一个sed选项……

 sed 's/\([^ ]*\)  \(.*\)/\2 \1/' inputfile.txt

Explained...

解释……

Swap
\([^ ]*\) = Match anything until we reach a space, store in $1
\(.*\)    = Match everything else, store in $2
With
\2        = Retrieve $2
\1        = Retrieve $1

More thoroughly explained...

更彻底地解释……

s    = Swap
/    = Beginning of source pattern
\(   = start storing this value
[^ ] = text not matching the space character
*    = 0 or more of the previous pattern
\)   = stop storing this value
\(   = start storing this value
.    = any character
*    = 0 or more of the previous pattern
\)   = stop storing this value
/    = End of source pattern, beginning of replacement
\2   = Retrieve the 2nd stored value
\1   = Retrieve the 1st stored value
/    = end of replacement

#14


0  

Yet another way...

另一种方法…

...this rejoins the fields 2 thru NF with the FS and outputs one line per line of input

…这将使字段2 thru NF与FS共享,并输出每行输入的一行。

awk '{for (i=2;i<=NF;i++){printf $i; if (i < NF) {printf FS};}printf RS}'

I use this with git to see what files have been modified in my working dir:

我使用git查看我的工作目录中修改了哪些文件:

git diff| \
    grep '\-\-git'| \
    awk '{print$NF}'| \
    awk -F"/" '{for (i=2;i<=NF;i++){printf $i; if (i < NF) {printf FS};}printf RS}'

#15


0  

If you're open to another Perl solution:

如果您愿意使用另一个Perl解决方案:

perl -ple 's/^(\S+)\s+(.*)/$2 $1/' file

#1


64  

Assigning $1 works but it will leave a leading space: awk '{first = $1; $1 = ""; print $0, first; }'

分配$1可以工作,但是它会留下一个前导空间:awk '{first = $1;1美元= " ";打印0美元,第一;} '

You can also find the number of columns in NF and use that in a loop.

您还可以在NF中找到列的数量,并在循环中使用它们。

#2


76  

$1="" leaves a space as Ben Jackson mentioned, so use a for loop:

$1="" "就像Ben Jackson提到的那样,留下一个空格,所以使用for循环:

awk '{for (i=2; i<=NF; i++) print $i}' filename

So if your string was "one two three", the output will be:

所以如果你的字符串是“1 / 3”,输出将是:

two
three

两个三

If you want the result in one row, you could do as follows:

如果你想把结果放在一行,你可以这样做:

awk '{for (i=2; i<NF; i++) printf $i " "; print $NF}' filename

This will give you: "two three"

这将给你:“two three”

#3


51  

Use the cut command with the --complement option:

使用cut命令和补语选项:

$ echo a b c | cut -f 1 -d ' '
a
$ echo a b c | cut -f 1,2 -d ' '
a b
$ echo a b c | cut -f 1 -d ' ' --complement
b c

#4


13  

Maybe the most concise way:

也许是最简洁的方式:

$ awk '{$(NF+1)=$1;$1=""}sub(FS,"")' infile
United Arab Emirates AE
Antigua & Barbuda AG
Netherlands Antilles AN
American Samoa AS
Bosnia and Herzegovina BA
Burkina Faso BF
Brunei Darussalam BN

Explanation:

解释:

$(NF+1)=$1: Generator of a "new" last field.

$(NF+1)=$1:新字段的生成器。

$1="": Set the original first field to null

$1="":将原始的第一个字段设置为null

sub(FS,""): After the first two actions {$(NF+1)=$1;$1=""} get rid of the first field separator by using sub. The final print is implicit.

sub(FS,""):在前两个动作{$($(NF+1)=$1;$1=""}之后,使用sub去除第一个字段分隔符。最终打印是隐式的。

#5


9  

awk '{ saved = $1; $1 = ""; print substr($0, 2), saved }'

Setting the first field to "" leaves a single copy of OFS at the start of $0. Assuming that OFS is only a single character (by default, it's a single space), we can remove it with substr($0, 2). Then we append the saved copy of $1.

将第一个字段设置为“”将在$0开头保留一个OFS副本。假设OFS只是一个字符(默认情况下,它是一个单独的空格),我们可以使用substr($ 0,2)删除它。

#6


9  

awk '{sub($1 FS,"")}7' YourFile

Remove the first field and separator, and print the result (7 is a non zero value so printing $0).

删除第一个字段和分隔符,并打印结果(7是一个非零值,因此打印$0)。

#7


3  

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

如果您愿意使用Perl解决方案……

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

    -n循环每一行输入文件,不要自动打印每一行

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

    -l在处理之前删除新行,之后再添加

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

    -自动分割模式-将输入行分割成@F数组。默认在空格上进行分割

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

    -F autosplit修饰符,在这个例子中分割为' '(两个空格)

  • -e execute the following perl code

    -e执行以下perl代码

@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

@F是每行单词的数组,从0 $#F开始索引,是@F @F[1.. ..$#F]是元素1通过最后一个元素@F[1]的数组切片。$#F,0]是元素1通过最后一个元素加上元素0的数组切片

#8


2  

The field separator in gawk (at least) can be a string as well as a character (it can also be a regex). If your data is consistent, then this will work:

gawk中的字段分隔符(至少)可以是字符串,也可以是字符(也可以是regex)。如果你的数据是一致的,那么这将会起作用:

awk -F "  " '{print $2,$1}' inputfile

That's two spaces between the double quotes.

这是双引号之间的两个空格。

#9


2  

awk '{ tmp = $1; sub(/^[^ ]+ +/, ""); print $0, tmp }'

awk '{tmp = $1;子(/ ^ ^ + + /," ");打印0美元,tmp }’

#10


2  

Let's move all the records to the next one and set the last one as the first:

让我们把所有的记录移到下一个记录上,把最后一个作为第一个:

$ awk '{a=$1; for (i=2; i<=NF; i++) $(i-1)=$i; $NF=a}1' file
United Arab Emirates AE
Antigua & Barbuda AG
Netherlands Antilles AN
American Samoa AS
Bosnia and Herzegovina BA
Burkina Faso BF
Brunei Darussalam BN

Explanation

  • a=$1 save the first value into a temporary variable.
  • a=$1将第一个值保存到一个临时变量中。
  • for (i=2; i<=NF; i++) $(i-1)=$i save the Nth field value into the (N-1)th field.
  • (我= 2;我< = NF;$(i-1)=$i将第n个字段值保存到第N-1个字段中。
  • $NF=a save the first value ($1) into the last field.
  • $NF=a将第一个值($1)保存到最后一个字段。
  • {}1 true condition to make awk perform the default action: {print $0}.
  • {}1使awk执行默认操作的真实条件:{打印$0}。

This way, if you happen to have another field separator, the result is also good:

这样,如果您碰巧有另一个字段分隔符,结果也很好:

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

$ awk 'BEGIN{OFS=FS="-"}{a=$1; for (i=2; i<=NF; i++) $(i-1)=$i; $NF=a}1' c
United-Arab-Emirates-AE
Antigua-&-Barbuda-AG
Netherlands-Antilles-AN
American-Samoa-AS
Bosnia-and-Herzegovina-BA
Burkina-Faso-BF
Brunei-Darussalam-BN

#11


1  

A first stab at it seems to work for your particular case.

第一次尝试似乎对你的特殊情况有效。

awk '{ f = $1; i = $NF; while (i <= 0); gsub(/^[A-Z][A-Z][ ][ ]/,""); print $i, f; }'

#12


1  

Option 1

There is a solution that works with some versions of awk:

有一种解决方案可以用于awk的一些版本:

awk '{ $(NF+1)=$1;$1="";$0=$0;} NF=NF ' infile.txt

Explanation:

解释:

       $(NF+1)=$1                          # add a new field equal to field 1.
                  $1=""                    # erase the contents of field 1.
                        $0=$0;} NF=NF      # force a re-calc of fields.
                                           # and use NF to promote a print.

Result:

结果:

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

However that might fail with older versions of awk.

然而,对于较老版本的awk来说,这可能会失败。


Option 2

awk '{ $(NF+1)=$1;$1="";sub(OFS,"");}1' infile.txt

That is:

那就是:

awk '{                                      # call awk.
       $(NF+1)=$1;                          # Add one trailing field.
                  $1="";                    # Erase first field.
                        sub(OFS,"");        # remove leading OFS.
                                    }1'     # print the line.

Note that what needs to be erased is the OFS, not the FS. The line gets re-calculated when the field $1 is asigned. That changes all runs of FS to one OFS.

注意,需要删除的是OFS,而不是FS。当字段$1被赋值时,将重新计算行。这将所有的FS运行都更改为一个OFS。


But even that option still fails with several delimiters, as is clearly shown by changing the OFS:

但是,即使是这个选项也会因为有几个分隔符而失败,正如改变OFS所显示的那样:

awk -v OFS=';' '{ $(NF+1)=$1;$1="";sub(OFS,"");}1' infile.txt

That line will output:

这条线将输出:

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

That reveals that runs of FS are being changed to one OFS.
The only way to avoid that is to avoid the field re-calculation.
One function that can avoid re-calc is sub.
The first field could be captured, then removed from $0 with sub, and then both re-printed.

这表明,FS的运行被更改为一个OFS。避免这种情况的唯一方法是避免重新计算字段。一个可以避免重新计算的函数是sub.可以捕获第一个字段,然后用sub从$0中删除,然后再重新打印。

Option 3

awk '{ a=$1;sub("[^"FS"]+["FS"]+",""); print $0, a;}' infile.txt
       a=$1                                   # capture first field.
       sub( "                                 # replace: 
             [^"FS"]+                         # A run of non-FS
                     ["FS"]+                  # followed by a run of FS.
                            " , ""            # for nothing.
                                  )           # Default to $0 (the whole line.
       print $0, a                   # Print in reverse order, with OFS.


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

Even if we change the FS, the OFS and/or add more delimiters, it works.
If the input file is changed to:

即使我们更改FS、OFS和/或添加更多的分隔符,它也可以工作。如果输入文件更改为:

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

And the command changes to:

命令改为:

awk -vFS='.' -vOFS=';' '{a=$1;sub("[^"FS"]+["FS"]+",""); print $0,a;}' infile.txt

The output will be (still preserving delimiters):

输出将是(仍然保留分隔符):

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

The command could be expanded to several fields, but only with modern awks and with --re-interval option active. This command on the original file:

该命令可以扩展到多个字段,但只能使用现代awks和——reinterval选项活动。原始文件上的这个命令:

awk -vn=2 '{a=$1;b=$2;sub("([^"FS"]+["FS"]+){"n"}","");print $0,a,b;}' infile.txt

Will output this:

将输出:

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

#13


0  

There's a sed option too...

还有一个sed选项……

 sed 's/\([^ ]*\)  \(.*\)/\2 \1/' inputfile.txt

Explained...

解释……

Swap
\([^ ]*\) = Match anything until we reach a space, store in $1
\(.*\)    = Match everything else, store in $2
With
\2        = Retrieve $2
\1        = Retrieve $1

More thoroughly explained...

更彻底地解释……

s    = Swap
/    = Beginning of source pattern
\(   = start storing this value
[^ ] = text not matching the space character
*    = 0 or more of the previous pattern
\)   = stop storing this value
\(   = start storing this value
.    = any character
*    = 0 or more of the previous pattern
\)   = stop storing this value
/    = End of source pattern, beginning of replacement
\2   = Retrieve the 2nd stored value
\1   = Retrieve the 1st stored value
/    = end of replacement

#14


0  

Yet another way...

另一种方法…

...this rejoins the fields 2 thru NF with the FS and outputs one line per line of input

…这将使字段2 thru NF与FS共享,并输出每行输入的一行。

awk '{for (i=2;i<=NF;i++){printf $i; if (i < NF) {printf FS};}printf RS}'

I use this with git to see what files have been modified in my working dir:

我使用git查看我的工作目录中修改了哪些文件:

git diff| \
    grep '\-\-git'| \
    awk '{print$NF}'| \
    awk -F"/" '{for (i=2;i<=NF;i++){printf $i; if (i < NF) {printf FS};}printf RS}'

#15


0  

If you're open to another Perl solution:

如果您愿意使用另一个Perl解决方案:

perl -ple 's/^(\S+)\s+(.*)/$2 $1/' file