我需要为以下文件创建一个文件列表:
APPLE_001
APPLE_002
BBB_004
APPLE_003
我只需要为
创建file_listAPPLE_001
APPLE_002
APPLE_003
谢谢, Ipsita
答案 0 :(得分:0)
您的规格不是那么窄,但这里的 bash 脚本符合您的要求:
#!/bin/bash
if [[ $# < 2 ]]
then
echo "[ERROR] this script expects two arguments, input file and output file" >&2
exit 1
fi
input_file=$1
output_file=$2
if [[ ! -f $input_file ]]
then
echo "[ERROR] your input file '$input_file' is missing" >&2
exit 1
fi
if [[ -f $output_file ]]
then
echo "[ERROR] your output file '$ouput_file' already exists please move it away" >&2
exit 1
fi
while read LINE
do
if [[ $LINE =~ APPLE_[0-9]+ ]]
then
echo $LINE >> $output_file
else
echo "'$LINE' does not match expected pattern, skip it"
fi
done < $input_file
if [[ -f $ouput_file ]]
then
echo "'$output_file' generated."
else
echo "[WARNING] no pattern found in '$input_file' no file generated"
fi
使其可执行(chmod + x ./list_starting_with.sh)
使用./list_starting_with.sh运行它file_in.txt file_out.txt