script to parse the properties file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to parse the properties file
# 1  
Old 01-22-2012
script to parse the properties file

Hi Friends,
I have a requirement to parse a properties file having a key=value pairs.
i need to count the number of key value pairs in the properties file and iterate through each key-value pair. I have written the script to read the number of lines from the property file, but cannot iterate through the count since the total number of lines is in string. Can you please help me in completing the below script.

Kindly find the script below
Code:
#declaring the propertie file
 . appl.properties
#expression to count the number of lines excluding the blank lines
lines= cat appl.properties | sed '/^\s*$/d' | wc -l
echo "Number of lines is $lines"
for ((i=0;i -le 4; i++ ));
do
 echo "pradeep";


done


sample properties file

Code:
#key=value
firstname=Pradeep
lastname=kumar
country=India
language=English

Desired output from shell script:

It should read the number of line which is 4. And then iterate 4 times using for loop and then printing the name and the value for each key value.



-- please use code tags

Last edited by prashdeep; 01-22-2012 at 09:51 PM..
# 2  
Old 01-22-2012
I do not understand parse. Normally that means to break the file into components.
Not just count lines.

Are there comment lines? Please post a sample properties file
# 3  
Old 01-23-2012
It looks like you are using bash. bash4 has associative arrays, bash3 does not. the bash3 method would save order. here example:

Code:
declare -A array
declare -a keys=()
declare -a vals=()
while IFS='=' read -r key val; do
        [[ $key = '#'* ]] && continue
        array["$key"]="$val"            #bash4
        keys+=("$key"); vals+=("$val")  #bash3
done < "input"

echo "Number of entries: ${#keys[@]} or ${#array[@]}"

# two separate array [bash3]
for ((i = 0; i < ${#keys[@]}; i++)); do
        printf '[%s]=[%s]\n' "${keys[i]}" "${vals[i]}"
done

# associative array [bash4]
for key in "${!array[@]}"; do
        printf '[%s]=[%s]\n' "$key" "${array[$key]}"
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Script parse file

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Hi all, I need help for a script that pulls out a series of numbers from a file (attached file) Basically I... (1 Reply)
Discussion started by: gianvitolinuxs
1 Replies

2. Shell Programming and Scripting

Script parse file Linux

Hi all, I need help for a script that pulls out a series of numbers from a file (attached file) Basically I need a parse to write me in a variable: 9d424312 Can someone help me? Thank you (2 Replies)
Discussion started by: gianvitolinuxs
2 Replies

3. Shell Programming and Scripting

Script to parse bookmarks file

I am using Internet Explorer v10 at work and regularly need to import my personal Firefox bookmarks over. Long story short, I have found the import falling over on any bookmark elements which are over 256 characters. The bookmark file contains bookmarks of this format: <DT><A... (4 Replies)
Discussion started by: ozgadgetguy
4 Replies

4. UNIX for Dummies Questions & Answers

Help to parse csv file with shell script

Hello ! I am very aware that this is not the first time this question is asked here, because I have already read a lot of previous answers, but none of them worked, so... As said in the title, I want to read a csv file with a bash script. Here is a sample of the file: ... (4 Replies)
Discussion started by: Grhyll
4 Replies

5. Shell Programming and Scripting

Script to parse a file faster

My example file is as given below: conn=1 uid=oracle conn=2 uid=db2 conn=3 uid=oracle conn=4 uid=hash conn=5 uid=skher conn=6 uid=oracle conn=7 uid=mpalkar conn=8 uid=anarke conn=1 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.10.5.6 to 10.18.6.5 conn=2 op=-1 msgId=-1 -... (7 Replies)
Discussion started by: sags007_99
7 Replies

6. Shell Programming and Scripting

Reading the Properties File From Shell script

Hi, I am new to the shell script please I need help for following question. I have properties file name called "com.test.properties" I have No of key values in this properties. com.person.name = xyz com.person.age = 55 com.person.address = hello I want read this properties but i... (1 Reply)
Discussion started by: venukjs
1 Replies

7. UNIX for Advanced & Expert Users

shell script to parse html file

hi all, i have a html file something similar to this. <tr class="evenrow"> <td class="data">added</td><td class="data">xyz@abc.com</td> <td class="data">filename.sql</td><td class="modifications-data">08/25/2009 07:58:40</td><td class="data">Added TK prof script</td> </tr> <tr... (1 Reply)
Discussion started by: sais
1 Replies

8. Shell Programming and Scripting

Help!!! Shell script to parse data file.

I am faced with a :confused: tricky problem to parse a data file ( May not be a tricky problem to the scripting guru's ). Here is what I am faced with. I have a file with multiple rows of data and the rows are not of fixed length. "|" is used as a delimiters for individual columns and each row... (3 Replies)
Discussion started by: yajaykumar
3 Replies

9. UNIX for Dummies Questions & Answers

File properties

Hi , I do have a line in my code as follows: if ] ; then ... else ... fi What does the -z does ? Similarly there is -s in some other part of the code. I guess there are many options like this.. Can anybody please tell what all options are available and what do they mean ? (2 Replies)
Discussion started by: risshanth
2 Replies

10. UNIX for Dummies Questions & Answers

How do properties effect script?

Hi, I have noticed that rm -if will perform completely different to rm -fi. Whats the pattern of how I put my options to the script in relation to how it will act. i.e rm -fi treat the remove as interative but rm -if treats it as forced Thansk, Chris. (1 Reply)
Discussion started by: Chiefos
1 Replies
Login or Register to Ask a Question