Solved

Shell parsing a file and store values

Posted on 2015-01-06
6
70 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
  • 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

773 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