[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to read parameters from a file in bash?

Posted on 2007-08-04
5
Medium Priority
?
3,587 Views
Last Modified: 2013-12-26
Hi,

I have the following unix bash script. I have the following lines. As you can see I am processing a text file and replacing IP addresses with a hostname. What I would like todo is have a simple parameter file that I can edit and have a shell script to process this.

sed -r 's/(.*192\.168\.1\.99.*)( - )(.*)/\1\ XP1 \3/g' -i access.log
sed -r 's/(.*192\.168\.1\.100.*)( - )(.*)/\1\ XP2 \3/g' -i access.log

So taking the example above it would have a format like:

"192.168.1.99","XP1"
"192.168.1.100","XP2"

Ok given this how would I write a shell script to do the following:

1.  Open the data file above,
2.  Decode the data and build it to a command line that would be able to pass the lines to sed.

Ideally I am looking toget the whole script todo the following. Will reward maximun points for a working script.

Thanks,

Wad
0
Comment
Question by:whorsfall
  • 3
5 Comments
 
LVL 85

Assisted Solution

by:ozo
ozo earned 200 total points
ID: 19633161
#!/usr/bin/perl
while( <> ){
  next unless ($s,$r)=/"(.*?)","(.*?)"/;
  $s{$s}=$r;
  push @s,quotemeta $s;
}
$re = "((".(join'|',@s).").* )- ";
$re = qr/$re/;
@ARGV=qw(access.log);
$^I='';
while( <> ){
  s/$re/$1$s{$2} /;
  print;
}

}
0
 
LVL 27

Expert Comment

by:Nopius
ID: 19633164
> 's/(.*192\.168\.1\.99.*)( - )(.*)/\1\ XP1 \3/g'

This command replaces IPs, but doesn't replaces 'in-place', it writes to stdout instead. If you need to replace, script modification very is simple.

#!/bin/sh
# pure shell and sed
# IPFILE - filename for file in format "IP","hostname"
IPFILE=ip.txt
LOGFILE=access.log
CMDFILE=/tmp/decode.sh.$$

echo -n cat $LOGFILE  > $CMDFILE
while IFS=, read ip host
do
  ip_sed=`echo $ip | sed 's/\./\[.\]/g;s/"//g'`
  host_sed=`echo $host | sed 's/\./\[.\]/g;s/"//g'`
  echo "|" sed -r "'s/(^.*)($ip_sed)( - .*$)/\1$host_sed\3/g' \\" >> $CMDFILE
done < $IPFILE
sh $CMDFILE
rm $CMDFILE
0
 
LVL 27

Accepted Solution

by:
Nopius earned 900 total points
ID: 19633203
Oops, my regexp has bugs (when you decode "192.168.1.1" it will also match "192.168.1.11")

That's a new version, also modified for single 'sed' command without piping.

#!/bin/sh
IPFILE=ip.txt
LOGFILE=access.log
CMDFILE=/tmp/decode.sh.$$

echo -n "sed -r '" > $CMDFILE
while IFS=, read ip host
do
  ip_sed=`echo $ip | sed 's/\./\[.\]/g;s/"//g'`
  host_sed=`echo $host | sed 's/\./\[.\]/g;s/"//g'`
  echo "s/(^.*[^0-9]*)($ip_sed)([^0-9]*.* - .*$)/\1$host_sed\3/g;" >> $CMDFILE
done < $IPFILE
echo "' $LOGFILE" >> $CMDFILE
sh $CMDFILE
rm $CMDFILE
0
 
LVL 58

Assisted Solution

by:amit_g
amit_g earned 900 total points
ID: 19634835
awk -F\" '{IP=$2; SN=$4; gsub(/\./, "\\\.", IP); printf("sed -r '\''s/(.*%s.*)( - )(.*)/\\1\\ %s \\3/g'\'' -i access.log\n", IP, SN);}' YourIPAndAddressFileName
0
 
LVL 27

Expert Comment

by:Nopius
ID: 19635865
amit_g, with awk it can be done without sed and without shell

awk 'BEGIN{FS=",";while(getline<"ip.txt"){gsub("\"", ""); gsub("[.]", "[.]", $1)
; ips[$1]=$2};FS=" - "; OFS=FS};{for (ip in ips) if (match($1, "[^0-9]*" ip "[^0-9]*")) {gsub(ip,ips[ip],$1);};print $0}' < access.log


0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

872 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