Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Shell parsing a file and store values

Posted on 2015-01-06
6
Medium Priority
?
76 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 48

Accepted Solution

by:
Tintin earned 2000 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

Technology Partners: 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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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.
Suggested Courses

618 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