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
Solved

Shell parsing a file and store values

Posted on 2015-01-06
6
71 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
linux redhat 7.2 10 89
SonarQube on Linux vs Windows 3 52
trouble on installing syslog-ng on CentOS 7 7 54
AWK: Pytthagoras bp script Part REVISITED 3 17
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…
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
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…
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.

856 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