
Eu tenho uma saída de
Student Name: abc
Roll Num: 123
Student Name: xyz
Roll Num: 124
e preciso imprimir no formato abaixo
Student Name Roll Num
abc 123
xyz 124
alguém pode me ajudar com comandos simples do Linux
Responder1
awk
não é a única ferramenta na caixa de ferramentas, é claro. Aqui estáMoleiroem ação:
%mlr --ixtab --ips : --opprint cat << FIM Nome do Aluno: ABC Número do rolo: 123 Nome do aluno: xyz Número do rolo: 124 FIM Número da lista de nomes do aluno abc 123 xiz 124 %
Você está fazendo uma conversão do formato XTAB ( -ixtab
) para o formato PPRINT ( -opprint
).
Responder2
Faça sua escolha:
$ 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
Responder3
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