I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables.
I need to read this file which is an input to my script
Config.txt
file name, first path, second path(pipe separated)
file name, first path, second path(pipe separated)
username
ip
path
path
:
:
Now I want to read all these values define in file , but while matching in script, when we get lines having �file name, first path, second path� then comparison must be done with �file name' and actual file name and its neighbouring values(first path, second path) need to be store in variable else for other values(username ,ip,...) skip the matching and only store them in variable for further used.
Please suggest me how to implement this.....
flag=0
while IFS='|' read -r line f_path p_path; do
set -f # disable globbing
# echo $line
if [[ $CHK_FILE =~ "${line}*" ]] ;# True if file name is equal to line* (literal matching).
then
flag=1
break
else
flag=0
fi
done < config .txt
if [ $flag = 1 ]
then
do this
else
do this
fi
In this case I able to make file name comparison but can�t stop to avoid when line contain username ,ip,� also I am planning to put all vales in variable for future understanding
You may want to read every single line and after that use a case statement (e.g. on the first variable read) to check what type of line you're dealing with.
T=0
while IFS='|' read -r line f_path p_path
do
if [ -n "$f_path" ]
then
file_name[${#file_name[@]}]=$line
first_path[${#first_path[@]}]=$f_path
second_path[${#second_path[@]}]=$p_path
continue
fi
let T=T+1
case $T in
1) username=$line ;;
2) ip=$line ;;
*) path=$line ;;
esac
done < config.txt
i=1
while [ $i -le ${#file_name[@]} ]
do
echo "file$i=\"${file_name[i-1]}|${first_path[i-1]}|${second_path[i-1]}\""
let i=i+1
done
echo unmae=\"$username\"
echo ip=\"$ip\"
Here we put the filename and 1st and 2nd paths into 3 arrays. username ip and other variables are assigned based on the line number following the filename data.
I've included a block of code to fetch back and print the input to show how you would access it within your script.
${#file_name[@]} returns the number of elements in the file_name array
i.e. zero when the array is empty; 1 when it contains 1 element; etc.
Since arrays are indexed from zero this is ideal for appending:
When it is empty we assign the first array element (i.e. element number zero)
When it has 1 element we assign the second array element (i.e. element number 1).