将Windows bat文件转换为Linux sh shell脚本

我正在尝试parsing和合并大量包含Ordnance Survey地址数据的csv文件。 操作系统提供了一个batch file,但我希望能够在Linux服务器上运行它,因为这些文件是巨大的。

我已经尝试从bat文件转换为ssh使用这个非常手指南 。 但是我正在努力调用这个过程。 有人能够检查什么是错的?

原来的脚本(我在这里截掉了一点)

FOR /F %%A IN ('dir *.csv /b/s') DO (CALL :process "%%A" "%%~NA") @rem -- merge the individual record identifier files copy *_10_Records.csv Master_10_Records.out copy *_11_Records.csv Master_11_Records.out copy *_15_Records.csv Master_15_Records.out copy *_21_Records.csv Master_21_Records.out del *_Records.csv @rem -- add header records to the individual record identifier files copy Record_10_HEADER_Header.csv+Master_10_Records.out ID10_Header_Records.csv copy Record_11_STREET_Header.csv+Master_11_Records.out ID11_Street_Records.csv copy Record_15_STREETDESCRIPTOR_Header.csv+Master_15_Records.out ID15_StreetDesc_Records.csv copy Record_21_BLPU_Header.csv+Master_21_Records.out ID21_BLPU_Records.csv del *.out pause exit @rem -- split the source csv into individual files based on the record identifier :process SET tempvar1=%~1 SET tempvar2=%~2 gawk < %tempvar1% -F "," "{ if ($1 == \"10\") { print $0 } }" > %tempvar2%_10_Records.csv gawk < %tempvar1% -F "," "{ if ($1 == \"11\") { print $0 } }" > %tempvar2%_11_Records.csv gawk < %tempvar1% -F "," "{ if ($1 == \"15\") { print $0 } }" > %tempvar2%_15_Records.csv gawk < %tempvar1% -F "," "{ if ($1 == \"21\") { print $0 } }" > %tempvar2%_21_Records.csv GOTO :EOF 

我(穷人)尝试将其翻译为shell脚本语法:

 FOR /FA IN ['dir *.csv /b/s'] DO (CALL :process "%%A" "%%~NA") # merge the individual record identifier files cp *_10_Records.csv Master_10_Records.out cp *_11_Records.csv Master_11_Records.out cp *_15_Records.csv Master_15_Records.out cp *_21_Records.csv Master_21_Records.out rm *_Records.csv # add header records to the individual record identifier files cp Record_10_HEADER_Header.csv+Master_10_Records.out ID10_Header_Records.csv cp Record_11_STREET_Header.csv+Master_11_Records.out ID11_Street_Records.csv cp Record_15_STREETDESCRIPTOR_Header.csv+Master_15_Records.out ID15_StreetDesc_Records.csv cp Record_21_BLPU_Header.csv+Master_21_Records.out ID21_BLPU_Records.csv rm *.out sleep exit # split the source csv into individual files based on the record identifier :process export tempvar1=%~1 export tempvar2=%~2 gawk < %tempvar1% -F "," "{ if ($1 == \"10\") { print $0 } }" > %tempvar2%_10_Records.csv gawk < %tempvar1% -F "," "{ if ($1 == \"11\") { print $0 } }" > %tempvar2%_11_Records.csv gawk < %tempvar1% -F "," "{ if ($1 == \"15\") { print $0 } }" > %tempvar2%_15_Records.csv gawk < %tempvar1% -F "," "{ if ($1 == \"21\") { print $0 } }" > %tempvar2%_21_Records.csv GOTO :EOF 

任何帮助将非常感激! 我通过谷歌search学到了很多东西,但我仍然对此感到陌生。

你可能想尝试下面的东西。 (请注意,我其实并没有尝试过,但我希望它能帮助你。)

 #!/bin/bash # split the source csv into individual files based on the record identifier function process() { tempvar1="$1" tempvar2="$2" awk < "$tempvar1" -F "," '{ if ($1 == "10") { print $0 } }' > "$tempvar2"_10_Records.csv awk < "$tempvar1" -F "," '{ if ($1 == "11") { print $0 } }' > "$tempvar2"_11_Records.csv awk < "$tempvar1" -F "," '{ if ($1 == "15") { print $0 } }' > "$tempvar2"_15_Records.csv awk < "$tempvar1" -F "," '{ if ($1 == "21") { print $0 } }' > "$tempvar2"_21_Records.csv } for A in *.csv ; do process "$A" "${A%.*}" done # merge the individual record identifier files cat *_10_Records.csv > Master_10_Records.out cat *_11_Records.csv > Master_11_Records.out cat *_15_Records.csv > Master_15_Records.out cat *_21_Records.csv > Master_21_Records.out rm *_Records.csv # add header records to the individual record identifier files cat Record_10_HEADER_Header.csv Master_10_Records.out > ID10_Header_Records.csv cat Record_11_STREET_Header.csv Master_11_Records.out > ID11_Street_Records.csv cat Record_15_STREETDESCRIPTOR_Header.csv Master_15_Records.out > ID15_StreetDesc_Records.csv cat Record_21_BLPU_Header.csv Master_21_Records.out > ID21_BLPU_Records.csv rm *.out echo -n "Press ENTER to continue..." read DUMMY exit 

它做什么/差异: