Solved

Shell parsing a file and store values

Posted on 2015-01-06
6
73 Views
Last Modified: 2015-02-16
Hi ,

I have a config file with many ftp details
eg:
[FTP1 DIRECTORY DETAILS]                                                        
SERVER = 127.0.0.1
DIRECTORY = /home/ee
PROCESS= /home/ee/process

In my shell script i have to parse this file and get the details for the needed FTP server and save in a variables/arrays.
How can i do this ?

Thanks
0
Comment
Question by:magento
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 40534416
If your config file was in the format

SERVER=127.0.0.1
DIRECTORY=/home/ee
PROCESS=/home/ee/process

Then in your script, all you would need to do is:

#!/bin/bash
source /path/to/file.config

echo "Server is $SERVER"
echo "Directory is $DIRECTORY"
echo "Process is $PROCESS"

Open in new window


How flexible are you with the format of the configuration file?
0
 
LVL 5

Author Comment

by:magento
ID: 40534576
Hi Tintin,

This is my config file.
[FTP1 DIRECTORY DETAILS]                                                         
SERVER = 127.0.0.1
DIRECTORY = /home/ee
PROCESS= /home/ee/process
[FTP2 DIRECTORY DETAILS]                                                         
SERVER = 127.0.0.2
DIRECTORY = /home/ee2
PROCESS= /home/ee2/process
[FTP3 DIRECTORY DETAILS]                                                         
SERVER = 127.0.0.3
DIRECTORY = /home/ee3
PROCESS= /home/ee3/process

Open in new window


In my script i want to get the values of FTP details.
Eg. If i want to get the FTP3 details, In my script the below variables should get the below value by parsing this script.

SER=127.0.0.3
DIR=/home/ee3
PRO=/home/ee3/process

I tried with grep and awk , but need proper code

Thanks
0
 
LVL 5

Author Comment

by:magento
ID: 40537209
Hi Tintin,

Any help on this?

Thanks
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
ID: 40538960
#!/bin/bash
export $(grep -A3 FTP3 /path/to/file.config | sed 1d | sed "s/ *//g")

echo "Server is $SERVER"
echo "Directory is $DIRECTORY"
echo "Process is $PROCESS"

Open in new window

0
 
LVL 5

Author Comment

by:magento
ID: 40539126
Thanks Tintin , i will try this .
0
 
LVL 5

Author Closing Comment

by:magento
ID: 40613464
Thanks for your help, this is what  i'm looking for.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question