출력을 테이블 형식으로 나열

출력을 테이블 형식으로 나열

나는 다음과 같은 결과를 얻었습니다.

Student Name: abc
Roll Num: 123

Student Name: xyz
Roll Num: 124

아래 형식으로 인쇄해야 합니다.

Student Name     Roll Num
abc              123
xyz              124

누군가 간단한 리눅스 명령으로 나를 도와줄 수 있나요?

답변1

awk물론 도구 상자에 있는 유일한 도구는 아닙니다. 여기밀러실제로:

%mlr --ixtab --ips : --opprint 고양이 << END
학생 이름: abc
롤 번호: 123

학생 이름: xyz
롤 번호: 124

학생 이름 명부
 알파벳 123
 xyz 124
%

-ixtabXTAB 형식( )에서 PPRINT 형식( )으로 변환을 수행 중입니다 -opprint.

답변2

골라보세요:

$ awk -v RS= -F': |\n' -v OFS='\t' 'NR==1{print $1, $3} {print $2, $4}' file
Student Name    Roll Num
abc     123
xyz     124

$ awk -v RS= -F': |\n' -v OFS='\t' 'NR==1{print $1, $3} {print $2, $4}' file | column -s$'\t' -t
Student Name  Roll Num
abc           123
xyz           124

$ awk -v RS= -F': |\n' -v fmt='%-13s %-13s\n' 'NR==1{printf fmt, $1, $3} {printf fmt, $2, $4}' file
Student Name  Roll Num
abc           123
xyz           124

답변3

Method1

awk 'BEGIN{print "Student Name";RS="Student Name:"}{print $1}' p.txt| awk '$0 !~ /^$/' >student.txt
awk   'RS="Roll Num"{print $2}' p.txt|sed '/Name/,/^$/d'| awk 'BEGIN {print "Roll Num"}{print $0}' > roll.txt


paste student.txt  roll.txt


output

Student Name     Roll Num
abc              123
xyz              124



 Method2

awk 'BEGIN{print "Student Name"}{if($1 ~ /Student/){print $3}}' p.txt > student.txt

awk 'BEGIN{print "Roll Num"}{if($1 ~ /Roll/){print $3}}' p.txt > roll.txt

paste student.txt  roll.txt


output

Student Name     Roll Num
abc              123
xyz              124

관련 정보