我想改变格式
来自:样本.txt
Count Data
1 C0263/Pre-approved Auto Load BC
1 C0263/Pre-approved Auto Load BC Holders
3 C0268/Pre-approved Auto Loan
至:输出.txt
Data Count
C0263/Pre-approved Auto Load BC 1
C0263/Pre-approved Auto BC Holders 1
C0268/Pre-approved Auto Loan 3
答案1
awk解决方案:
awk 'NR==1{ printf "%4s%-44s%s\n",FS,$2,$1; next }
{ f1=$1; sub(/^ *[^ ]+ */,""); printf "%4s%-44s%s\n",FS,$0,f1 }' Sample.txt
输出:
Data Count
C0263/Pre-approved Auto Load BC 1
C0263/Pre-approved Auto Load BC Holders 1
C0268/Pre-approved Auto Loan 3
答案2
假设输入数据在文件中datafile.txt
并且您使用的是 POSIX shell,您可以说
sed -e 's/^\s*//' -e 's/\s\+/:/' datafile.txt | while IFS=':' read a b; do
printf '%-30.29s%s\n' "$b" "$a"
done
该sed
命令将 (1) 删除所有前导空格,然后 (2) 用冒号替换第一次出现的一个或多个空格;结果通过管道传输到一个while
循环,该循环读取由冒号分隔的两个字段,然后很好地打印它们。
如果输入数据来自管道,则删除参数datafile.txt
并将其通过管道传输到sed
.