在 Linux 上,我有文件orig-file.txt
。此文件现在包含 4 个字段,但它们可能更少或更多(此文件由其他应用程序生成)。
orig-file.txt
将其转换为类似文件的最佳选择是什么output-file.txt
(可以使用 shell 脚本或 awk 等)
原始文件.txt
CREATE_TIMESTAMP TELEPHONE_NUMBER ID TYPE
------------------- -------------------- ---------- -----------------
24-09-2009 16:17:45 33633333333 20 other_mmm_phone
24-09-2009 17:45:07 33644444444 20 other_mmm_phone
07-10-2009 10:45:49 12312312312 20 legacyphone
07-10-2009 11:46:38 59320000043 20 other_mmm_phone
输出文件.txt
CREATE_TIMESTAMP -> 24-09-2009 16:17:45
TELEPHONE_NUMBER -> 33633333333
ID -> 20
TYPE -> other_mmm_phone
---
CREATE_TIMESTAMP -> 24-09-2009 16:17:45
TELEPHONE_NUMBER -> 33633333333
ID -> 20
TYPE -> other_mmm_phone
---
来自 awk lang 的示例 -( 但它不起作用 -:(
# awk 'NR>2 {
> printf "\
> %-16s -> %s\n\
> %-16s -> %s\n\
> %-16s -> %s\n\
> %-16s -> %s\
> \n\n\n---\n\n\n",\
> "CREATE_TIMESTAMP", $1" "$2,\
> "TELEPHONE_NUMBER", $3,\
> "ID", $4,\
> "TYPE", $5}\
> ' orig-file.txt
awk: newline in string near line 2
awk: syntax error near line 3
awk: illegal statement near line 3
awk: newline in string near line 7
答案1
以下是一些简单的 ksh:
{
read t1 t2 t3 t4
maxlen=$(printf "%s\n" ${#t1} ${#t2} ${#t3} ${#t4} | sort -n | tail -1)
fmt=$(printf "%%-%ds -> %%s" $maxlen)
read line
while read date time tel id type; do
printf "$fmt\n" $t1 "$date $time" $t2 $tel $t3 $id $t4 $type
print "\n\n\n---\n\n"
done
} < orig-file.txt
更新对于灵活的字段数量:
我替换了日期时间字段中的空格,以便于解析
sed '3,$s/ /@@/' orig-file.txt |
{
read line
set -A headings $line
max=0
for head in "${headings[@]}"; do (( max < ${#head} )) && max=${#head}; done
fmt=$(printf "%%-%ds -> %%s" $max)
read line
while read line; do
set -A fields $line
i=0
while (( i < ${#headings[@]} )); do
printf "$fmt\n" ${headings[$i]} ${fields[$i]} | sed 's/@@/ /'
(( i=i+1 ))
done
print "\n\n\n---\n\n"
done
}
答案2
在这种情况下,这可以完成工作。如果添加更多字段,则需要进行一些简单的修改。
awk 'NR>2{
printf "\
%-16s -> %s\n\
%-16s -> %s\n\
%-16s -> %s\n\
%-16s -> %s\
\n\n\n---\n\n\n",\
"CREATE_TIMESTAMP", $1" "$2,\
"TELEPHONE_NUMBER", $3,\
"ID", $4,\
"TYPE", $5}\
' orig-file.txt > output-file.txt
“CREATE_TIMESTAMP” 需要两者$1
,$2
因为日期本身是用空格分隔的。
可以修改为从 header 读取字段名,但存在日期被空格分隔的问题。如果其他字段也允许包含空格,则总是需要手动修改来弥补,就像本$1" "$2
例一样。