改变行中单词的顺序

改变行中单词的顺序

我正在尝试打印包含学生姓名的文件。

muhaz:x:6541:113:AhmmedMuhaz Mohammed Kunhi Mulleria:/home/students/muhaz:/bin/bash

我需要我的输出是这样的:

muhaz:x:6541:113:Mulleria Mohammed Kunhi AhmmedMuhaz:/home/students/muhaz:/bin/bash

这是我尝试过的,但它根本不起作用。

cat /etc/passwd | tail -n 20 | sed -r 's/.*[[:alpha:]]+([[:blank:]][[:alpha:]]+)*.*/\3\2\1/'

该命令应该是什么样子,有什么问题?

入口文件:

rovshan:x:6532:113:Rovshan Ahmadli:/home/students/rovshan:/bin/bash
smith_63:x:6533:113:Smith Pataraprasitpon:/home/students/smith_63:/bin/bash
rashad:x:6534:113:Rashad Mahmudov:/home/students/rashad:/bin/bash
dilgamsh:x:6535:113:Dilgam Sharifov:/home/students/dilgamsh:/bin/bash
nlk_ns:x:6536:113:Nurlan Zhangali:/home/students/nlk_ns:/bin/bash
karpwiol:x:6537:113:WK:/home/students/karpwiol:/bin/bash
yigit123:x:6538:113:Mustafa Yigit Ersoz:/home/students/yigit123:/bin/bash
orxan123:x:6539:113:Orkhan Abiyev:/home/students/orxan123:/bin/bash
valiyev:x:6540:113:rufat valiyev:/home/students/valiyev:/bin/bash
muhaz:x:6541:113:AhmmedMuhaz Mohammed Kunhi Mulleria:/home/students/muhaz:/bin/bash
fatihebr:x:6542:113:Fatih Ocal:/home/students/fatihebr:/bin/bash
mrgo_g:x:6543:113:margarita gagaeva:/home/students/mrgo_g:/bin/bash
chrisd48:x:6544:113:Christian Hibestu:/home/students/chrisd48:/bin/bash
qwertinx:x:6545:113:Jakub Kaczmarczyk:/home/students/qwertinx:/bin/bash
grrronek:x:6546:113:Katarzyna Gro▒ska:/home/students/grrronek:/bin/bash
shahin:x:6547:113:Shahin Mehrabov:/home/students/shahin:/bin/bash
soufroud:x:6548:113:sofiane bouzroud:/home/students/soufroud:/bin/bash
jtrojak0:x:6549:113:Joanna Trojak:/home/students/jtrojak0:/bin/bash
jtrojak1:x:6550:113:Joanna Trojak:/home/students/jtrojak1:/bin/bash
markam:x:6551:113:Marcel Kami▒ski:/home/students/markam:/bin/bash

我想要实现的目标是:

shahin:x:6547:113:Mehrabov Shahin:/home/students/shahin:/bin/bash
jtrojak1:x:6550:113:Trojak Joanna:/home/students/jtrojak1:/bin/bash
yigit123:x:6538:113:Ersoz Yigit Mustafa:/home/students/yigit123:/bin/bash

答案1

您可以使用 GNU 尝试此操作sed,假设该字符串是唯一的,如果不是,请对其进行注释:

sed -E 's/:(\w+) (\w+) (\w+) (\w+):/:\4 \2 \3 \1:/' file

输出:

muhaz:x:6541:113:Mulleria Mohammed Kunhi AhmmedMuhaz:/home/students/muhaz:/bin/bash

答案2

对于结构化数据,您可能会发现 Awk 等工具更适合,例如。

$ echo 'muhaz:x:6541:113:AhmmedMuhaz Mohammed Kunhi Mulleria:/home/students/muhaz:/bin/bash' | awk -F: '
    BEGIN {OFS=FS} 
    {n=split($5,a,/ /); $5 = a[n]; for(i=2;i<n;i++) $5 = $5 " " a[i]; $5 = $5 " " a[1]}
    {print}
  '
muhaz:x:6541:113:Mulleria Mohammed Kunhi AhmmedMuhaz:/home/students/muhaz:/bin/bash

答案3

使用 GNU sed 我们可以这样做:

$ sed -r '
    s/:/\n/4;s//\n/4
    s/\n(\S+)(.*\s)(\S+)\n/:\3\2\1:/
    y/\n/:/
' file

通过保证不会被发现的唯一标记来标记第五个字段。

然后在两侧标记所围住的部分中,翻转第一个和最后一个单词。

如果上述步骤没有完成,请说它只有一个名字,例如中亚人的名字,然后删除这些标记。

sed 默认打印模式空间。

答案4

使用 GNU awk 执行 gensub():

$ gawk 'BEGIN{FS=OFS=":"} {$5=gensub(/^(\S+)((\s+\S+)*\s+)(\S+)$/,"\\4\\2\\1",1,$5)} 1' file
rovshan:x:6532:113:Ahmadli Rovshan:/home/students/rovshan:/bin/bash
smith_63:x:6533:113:Pataraprasitpon Smith:/home/students/smith_63:/bin/bash
rashad:x:6534:113:Mahmudov Rashad:/home/students/rashad:/bin/bash
dilgamsh:x:6535:113:Sharifov Dilgam:/home/students/dilgamsh:/bin/bash
nlk_ns:x:6536:113:Zhangali Nurlan:/home/students/nlk_ns:/bin/bash
karpwiol:x:6537:113:WK:/home/students/karpwiol:/bin/bash
yigit123:x:6538:113:Ersoz Yigit Mustafa:/home/students/yigit123:/bin/bash
orxan123:x:6539:113:Abiyev Orkhan:/home/students/orxan123:/bin/bash
valiyev:x:6540:113:valiyev rufat:/home/students/valiyev:/bin/bash
muhaz:x:6541:113:Mulleria Mohammed Kunhi AhmmedMuhaz:/home/students/muhaz:/bin/bash
fatihebr:x:6542:113:Ocal Fatih:/home/students/fatihebr:/bin/bash
mrgo_g:x:6543:113:gagaeva margarita:/home/students/mrgo_g:/bin/bash
chrisd48:x:6544:113:Hibestu Christian:/home/students/chrisd48:/bin/bash
qwertinx:x:6545:113:Kaczmarczyk Jakub:/home/students/qwertinx:/bin/bash
grrronek:x:6546:113:Gro▒ska Katarzyna:/home/students/grrronek:/bin/bash
shahin:x:6547:113:Mehrabov Shahin:/home/students/shahin:/bin/bash
soufroud:x:6548:113:bouzroud sofiane:/home/students/soufroud:/bin/bash
jtrojak0:x:6549:113:Trojak Joanna:/home/students/jtrojak0:/bin/bash
jtrojak1:x:6550:113:Trojak Joanna:/home/students/jtrojak1:/bin/bash
markam:x:6551:113:Kami▒ski Marcel:/home/students/markam:/bin/bash

使用带有参数-E来支持 ERE 的 sed(例如 GNU 或 OSX/BSD sed):

$ sed -E 's/(([^:]*:){4})([^[:space:]:]+)(([[:space:]]+[^[:space:]:]+)*[[:space:]]+)([^[:space:]:]+)/\1\6\4\3/' file
rovshan:x:6532:113:Ahmadli Rovshan:/home/students/rovshan:/bin/bash
smith_63:x:6533:113:Pataraprasitpon Smith:/home/students/smith_63:/bin/bash
rashad:x:6534:113:Mahmudov Rashad:/home/students/rashad:/bin/bash
dilgamsh:x:6535:113:Sharifov Dilgam:/home/students/dilgamsh:/bin/bash
nlk_ns:x:6536:113:Zhangali Nurlan:/home/students/nlk_ns:/bin/bash
karpwiol:x:6537:113:WK:/home/students/karpwiol:/bin/bash
yigit123:x:6538:113:Ersoz Yigit Mustafa:/home/students/yigit123:/bin/bash
orxan123:x:6539:113:Abiyev Orkhan:/home/students/orxan123:/bin/bash
valiyev:x:6540:113:valiyev rufat:/home/students/valiyev:/bin/bash
muhaz:x:6541:113:Mulleria Mohammed Kunhi AhmmedMuhaz:/home/students/muhaz:/bin/bash
fatihebr:x:6542:113:Ocal Fatih:/home/students/fatihebr:/bin/bash
mrgo_g:x:6543:113:gagaeva margarita:/home/students/mrgo_g:/bin/bash
chrisd48:x:6544:113:Hibestu Christian:/home/students/chrisd48:/bin/bash
qwertinx:x:6545:113:Kaczmarczyk Jakub:/home/students/qwertinx:/bin/bash
grrronek:x:6546:113:Gro▒ska Katarzyna:/home/students/grrronek:/bin/bash
shahin:x:6547:113:Mehrabov Shahin:/home/students/shahin:/bin/bash
soufroud:x:6548:113:bouzroud sofiane:/home/students/soufroud:/bin/bash
jtrojak0:x:6549:113:Trojak Joanna:/home/students/jtrojak0:/bin/bash
jtrojak1:x:6550:113:Trojak Joanna:/home/students/jtrojak1:/bin/bash
markam:x:6551:113:Kami▒ski Marcel:/home/students/markam:/bin/bash

相关内容