linux - Shell-Script:Create File List Based on Certain Charachter -
linux - Shell-Script:Create File List Based on Certain Charachter -
i need create 1 file list below files:
apple_001
apple_002
bbb_004
apple_003
i need create file_list
apple_001
apple_002
apple_003
thanks, ipsita
your specifications not narrow, here bash script match request :
#!/bin/bash if [[ $# < 2 ]] echo "[error] script expects 2 arguments, input file , output file" >&2 exit 1 fi input_file=$1 output_file=$2 if [[ ! -f $input_file ]] echo "[error] input file '$input_file' missing" >&2 exit 1 fi if [[ -f $output_file ]] echo "[error] output file '$ouput_file' exists please move away" >&2 exit 1 fi while read line if [[ $line =~ apple_[0-9]+ ]] echo $line >> $output_file else echo "'$line' not match expected pattern, skip it" fi done < $input_file if [[ -f $ouput_file ]] echo "'$output_file' generated." else echo "[warning] no pattern found in '$input_file' no file generated" fi
make executable ( chmod +x ./list_starting_with.sh )
run ./list_starting_with.sh file_in.txt file_out.txt
linux shell
Comments
Post a Comment