linux - 用于格式化管道输出的 bash 脚本

标签 linux bash

好的,这是您自找的 - 这是我尝试使用的全部代码。

我需要的帮助是: postprocauto.sh:一个以易于阅读的格式呈现数据的脚本。

我应该获取 autocsv 文件的一部分(下面的示例 - 完整文件为 160k),然后通过各种其他脚本将其通过管道传输,所以最后,您将拥有以下命令行:

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,告诉我把它放在哪里 - 520k 文件大小

prepprocauto.sh

#!/bin/bash

while read x
do
    echo $x | awk -F',' ' { print $1":"$2":"$4":"$7":"$8":"$10":"$11":"$12":"$22":"$24}'

done

宝马.sh

#!/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

后处理.sh

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文件的内容(删减了很多)

2013,Audi,Audi,TT Roadster quattro,ADX,67,2,4,Auto(AM-S6),22,31,26,28.4068,42.25
79,33.3217,22.407,31.1674,25.6515,,TC,Turbocharged,AMS,Automated Manual- Selecta
ble (e.g. Automated Manual with paddles),6,Y,N,A,10,GP,Gasoline (Premium Unleade
d Recommended),MPG,N,,,,,,,2200,2200,2,2,1,Two Seaters,car,Vehicle Specific 5-cy
cle label,6/18/12,12113,,N,N,,,N,N,ENGINE CODE CDMA ONLY.,N,,Y,CONTINUOUS VARIAB
LE VALVE TIMING,N,,,,,,,,,,,,,,,,,,,,,,,,,,,GDI,Spark Ignition Direct Injection,
N,N,5W40,33.3,7,7,,DADXV02.03UA,5,600,
2013,BMW,BMW,Z4 sDrive28i,BMX,428,2,4,Auto(A8),22,33,26,27.9499,46.8923,34.1594,
21.9803,33.2305,25.9308,,TC,Turbocharged,A,Automatic,8,Y,N,R,10,GP,Gasoline (Pre
mium Unleaded Recommended),MPG,N,,,,,,,2200,2200,2,2,1,Two Seaters,car,Derived 5
-cycle label,7/24/12,11033,,N,N,,,N,N,,N,,Y,variable valve timing at inlet and o
utlet valves,Y,variable valve lift at inlet valves,,,,,,,,,,,,,,,,,,,,,,,,,,GDI,
Spark Ignition Direct Injection,,Y,0W30,34.3,7,7,,DBMXJ02.0N20,5,600,
2013,BMW,BMW,Z4 sDrive28i,BMX,429,2,4,Manual(M6),22,34,26,28.3664,48.0364,34.774
1,22.2841,34.0033,26.3746,,TC,Turbocharged,M,Manual,6,N,N,R,10,GP,Gasoline (Prem
ium Unleaded Recommended),MPG,N,,,,,,,2200,2200,2,2,1,Two Seaters,car,Derived 5-
cycle label,7/25/12,11092,,N,N,,,N,N,,N,,Y,variable valve timing at inlet and ou
tlet valves,Y,variable valve lift at inlet valves,,,,,,,,,,,,,,,,,,,,,,,,,,GDI,S
park Ignition Direct Injection,,Y,0W30,34.9,7,7,,DBMXJ02.0N20,5,600,
2013,BMW,BMW,Z4 sDrive35i,BMX,436,3,6,Auto(AM-S7),17,24,19,21.1097,32.8224,25.14
8,16.8973,23.5833,19.3682,,TC,Turbocharged,AMS,Automated Manual- Selectable (e.g
. Automated Manual with paddles),7,N,N,R,10,GP,Gasoline (Premium Unleaded Recomm
ended),MPG,N,,,,,,,3000,3000,2,2,1,Two Seaters,car,Derived 5-cycle label,8/7/12,
12492,,N,N,,,N,N,,N,,Y,variable valve timing at inlet and outlet valves,N,,,,,,,
,,,,,,,,,,,,,,,,,,,,GDI,Spark Ignition Direct Injection,,Y,0W30,25.3,4,4,,DBMXV0
3.054R,5,,3400
2013,BMW,BMW,Z4 sDrive35i,BMX,435,3,6,Manual(M6),19,26,21,23.3,36.6,27.855,18.54
45,26.1997,21.352,,TC,Turbocharged,M,Manual,6,N,N,R,10,GP,Gasoline (Premium Unle
aded Recommended),MPG,N,,,,,,,2700,2700,2,2,1,Two Seaters,car,Derived 5-cycle la
bel,8/7/12,11743,,N,N,,,N,N,,N,,Y,variable valve timing at inlet and outlet valv
es,N,,,,,,,,,,,,,,,,,,,,,,,,,,,GDI,Spark Ignition Direct Injection,,Y,0W30,28.1,
5,5,,DBMXV03.054R,5,,1900

最佳答案

只需引用$output

echo "$output"

此外,您可能希望摆脱对 cat 的冗余使用并执行

awk -F ':' '{print $1 "\t" $4 "\t"$5"\t" $6 "\t" $7 "\t" $3}' ./temp.log

代替

关于linux - 用于格式化管道输出的 bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22123183/

相关文章:

bash - LaunchD Plist 不工作

c++ - 打开通过蓝牙发送的接收文件 - 如何等到完全接收?

linux - 通过替换 .so 文件更新 Apache 身份验证模块

bash - 为什么主管找不到命令源

regex - 检查是否由 `perl -i -pe` 完成任何替换

bash - 如何在 bash 中对整数序列进行零填充,以便所有整数都具有相同的宽度?

c++ - Linux 上找不到 SDL 库

linux wake_up_interruptible() 无效

linux - 错误 CoreDNS 所需的最低内核版本为 4.14.35 (UEKR5)

bash - 如何使用 grep 提取不重复的字符串模式