학생 이름이 포함된 파일을 인쇄하려고 합니다.
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(예: GNU 또는 OSX/BSD sed)를 지원하려면 인수와 함께 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