Solved

Shell parsing a file and store values

Posted on 2015-01-06
6
66 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
Comment Utility
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
Comment Utility
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
Comment Utility
Hi Tintin,

Any help on this?

Thanks
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
Comment Utility
#!/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
Comment Utility
Thanks Tintin , i will try this .
0
 
LVL 5

Author Closing Comment

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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
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 …
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now