Project

General

Profile

Feature #408 ยป file_convert_linux_v_1.0.sh

Peter Manev, 01/29/2012 06:52 AM

 
#!/bin/bash
# You are free to change and use the following script any way you like, at your own risk.
# Run in the "files" directory


# The menu
options=("TIME" "SRC_IP" "DST_IP" "PROTO" "SRC_PORT" "DST_PORT" "FILENAME" )

menu() {
echo;echo;
echo "Please choose the options according to which you would like your files to be renamed."
echo "They will be RENAMED (in the same directory) in this order below,depending on your selection, in the following fashion:"
echo "file.1 will become \"file.1__TIME__SRCIP__DSTIP__PROTO__SRCPORT__DSTPORT__FILENAME\" :"
echo "Notice the use of \"__\" (double _ )for a field delimiter in the filename."
echo "This script is to be EXECUTED in the \"files\" directory !! Your choice: "
echo;
for i in ${!options[@]}; do
printf "%3d%s) %s\n" $((i+1)) "${choices[i]:- }" "${options[i]}"
done
[[ "$msg" ]] && echo "$msg"; :
}

prompt="Check an option (again to uncheck, ENTER when done): "
while menu && read -rp "$prompt" num && [[ "$num" ]]; do
[[ "$num" != *[![:digit:]]* ]] && (( num > 0 && num <= ${#options[@]} )) || {
msg="Invalid option: $num"; continue
}
((num--)); msg="${options[num]} was ${choices[num]:+un}checked"
[[ "${choices[num]}" ]] && choices[num]="" || choices[num]="+"
done

printf "You selected"; msg=" nothing"
for i in ${!options[@]}; do
[[ "${choices[i]}" ]] && { printf " %s" "${options[i]}"; msg=""; } && Value_to_Write[$[${#Value_to_Write[@]}+1]]="${options[i]}"

done
echo "$msg"
echo
# End of the menu


#below we start a loop to loop through ll the file.NUMBER files in the directoy
for i in $(dir -1 |grep -v convert |grep -v meta |grep "file" ); do \

# below we start an inner loop to go over each of the selected fileds, in order to rename the file.
for x in ${Value_to_Write[@]}; do

x=${x//\_/" "} # here we substitute ex. "DST_IP" with "DST IP", because we search the "file.meta" for these specific fields

if [[ "$x" == "FILENAME" ]]; then a="$(awk '/FILENAME/ {print $NF} ' $i.meta | awk -F "/" '{ print $NF }' )";
# here we actually look if we have a "FILENAME" selected and extract only the filename itself, not the whole link
else
a="$(awk " /$x/ {print \$NF} " $i.meta)"
fi

# below we add the selected field value to an array (the name of the renamed file to be)
ToPrint[$[${#ToPrint[@]}+1]]="$a"
done

NameToPrint="$(echo "${ToPrint[*]}" | sed -e 's/ /__/g ' |sed -e 's/\//\-/g' |sed -e 's/\:/\-/g')"
#the line above basically substitutes any of the whitespaces, backslash and colon with a dash "-" globaly for the array to print
#because they are not allowed in a file name under linux

#echo "${ToPrint[*]}"
#echo "${NameToPrint[*]}"
mv $i $i\_\_$NameToPrint #the renaming itself
unset ToPrint # unsetting the array containing the file to be renamed values (we do not want values from previous file to appear on the wrong filename,file)
done

    (1-1/1)