0

我正在尝试读取文件,并在 bash 中解析它。我需要做一个从todd转换,然后循环并读取 X 字节,将每个 X 字节作为新文件中的一行进行管道传输:EBCDICASCII

#!/bin/bash

# $1 = input file in EBCDIC
# $2 = row length
# $3 = output file

# convert to ASCII and replace NUL (^@) with ' '
dd conv=ascii if=$1 | sed 's/\x0/ /g' > $3.tmp
file=$(cat "$3.tmp")
sIndex=1
fIndex=$2

# remove file
rm $3
echo "filesize: ${#file}";   

# loop, retrieving each fixed-size record and appending to a file
while true; do
    # append record to file
    echo "${file:sIndex:fIndex}" >> $3;

    # break at end of file
    if [ $fIndex -ge ${#file} ] 
    then    
        break;
    fi

    # increment index
    sIndex=$((sIndex+fIndex));
done

# remove tmp
rm $3.tmp

有什么方法可以让整个过程更快?

4

1 回答 1

1

回答我自己的问题。答案很简单,使用fold!

# $1 = ASCII input file
# $2 = file record length (i.e. 100)
# $3 = output file (non-delimited, row-separated file)

# dd : convert from EBCDIC to ASCII
# sed : replace NUL (^@) with space ' '
# fold : wrap input to specified width (record length)

dd conv=ascii if=$1 | sed 's/\x0/ /g' | fold -$2 > $3
于 2015-04-15T04:15:11.753 回答