Linux 用于格式化管道输出的bash脚本
好吧,这是你自找的-这是我的全部代码,我正在尝试使用 我需要的帮助是: postprocauto.sh:一个以可读格式显示数据的脚本 我需要对autocsv文件(下面的示例-完整文件为160k)进行一次分类,然后通过各种其他脚本对其进行管道传输,因此最终,您将拥有以下命令行:Linux 用于格式化管道输出的bash脚本,linux,bash,Linux,Bash,好吧,这是你自找的-这是我的全部代码,我正在尝试使用 我需要的帮助是: postprocauto.sh:一个以可读格式显示数据的脚本 我需要对autocsv文件(下面的示例-完整文件为160k)进行一次分类,然后通过各种其他脚本对其进行管道传输,因此最终,您将拥有以下命令行: cat autocsv | ./ prepprocauto.sh | ./BMW.sh | ./6cyl.sh | ./hwyfe.sh | ./postprocauto.sh 我的问题是,除了postprocauto.
cat autocsv | ./ prepprocauto.sh | ./BMW.sh | ./6cyl.sh | ./hwyfe.sh | ./postprocauto.sh
我的问题是,除了postprocauto.ch之外,我可以运行所有东西。我需要能够做到这一点:
Requirements for this script:
It must print header information after 20 lines of data.
The breaks must have a least one empty line between them
before printing another header.
The last line of the output should print out the number of
records that were processed.
输出的一个示例:
Year Eng. Disp. Cyl. City FE Hwy FE Model
2013 1.5 4 39 38 ILX
2013 2 4 24 35 ILX
2013 2.4 4 22 31 ILX
2013 2.4 4 22 31 TSX
2013 2.4 4 21 29 TSX
2013 3.5 6 19 28 TSX
2013 1.3 4 41 44 INSIGHT
2013 1.3 4 41 44 INSIGHT
2013 3.5 6 20 29 TL 2WD
2013 3.7 6 18 26 TL 4WD
2013 3.7 6 17 25 TL 4WD
There were 11 records processed.
不同的脚本如下-如果您想要完整大小的autocsv,请告诉我将其放置在何处-52000文件大小
prepprocauto.sh
#!/bin/bash
while read x
do
echo $x | awk -F',' ' { print $1":"$2":"$4":"$7":"$8":"$10":"$11":"$12":"$22":"$24}'
done
output=$(awk -F ':' '{print $1 "\t" $4 "\t"$5"\t" $6 "\t" $7 "\t" $3}')
echo "Year Eng. Disp Cyl City FE Hwy FE Model"
echo "$output"
宝马上海
#!/bin/bash
selection='BMW'
if [ ! $# -lt 1 ]; then
selection=$1
fi
while read y; do
model=$(echo $y | awk -F':' '{print $2 }')
if [ "$model" == "$selection" ]; then
echo $y
fi
done
6cyl.sh
#!/bin/bash
selection='6'
if [ ! $# -lt 1 ]; then
selection=$1
fi
while read y; do
model=$(echo $y | awk -F':' '{print $5 }')
if [ "$model" == "$selection" ]; then
echo $y
fi
done
hwyfe.sh
#!/bin/bash
selection='31'
if [ ! $# -lt 1 ]; then
selection=$1
fi
while read y; do
hwy=$(echo $y | awk -F':' '{print $7 }')
if [ "$hwy" -gt "$selection" ]; then
echo $y
fi
done
postprocauto.sh
#!/bin/bash
while read x
do
echo $x | awk -F',' ' { print $1":"$2":"$4":"$7":"$8":"$10":"$11":"$12":"$22":"$24}'
done
output=$(awk -F ':' '{print $1 "\t" $4 "\t"$5"\t" $6 "\t" $7 "\t" $3}')
echo "Year Eng. Disp Cyl City FE Hwy FE Model"
echo "$output"
autocsv文件的内容(大量减少)
只需引用
$output
echo "$output"
此外,您可能希望摆脱对cat
的冗余使用,并执行以下操作
awk -F ':' '{print $1 "\t" $4 "\t"$5"\t" $6 "\t" $7 "\t" $3}' ./temp.log
相反尝试使用column-t
echo $output | column -t
您可以使用模函数每隔N行显示一个标题,如下所示:
$ seq 1 12 > moo
$ awk < moo 'NR%4==0 { print "\nheading\n" } {print}
END{ print "total lines: " NR}'
1
2
3
heading
4
5
6
7
heading
8
9
10
11
heading
12
total lines: 12
$seq 112>moo
$awk
显示您的临时日志文件中的一些示例内容
规范中描述的所有内容都可以在1 awk程序中处理。但首先尝试回显“$output”
。传递给echo
的unquote变量将把所有空格减少到每个单词之间的单个空格。要获得20行标题,只需使用awk的NR(记录编号)变量,如if(NR%20){print“header}
。祝您好运……并设置OFS
使awk
更干净。