Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

How to combine awk and bash commands in script ?

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-01-2013
Akshay Hegde's Unix or Linux Image
Akshay Hegde Akshay Hegde is offline Forum Advisor  
Forum Advisor
 
Join Date: Nov 2012
Last Activity: 27 July 2015, 5:19 AM EDT
Location: Goa / Bangalore, India
Posts: 1,732
Thanks: 85
Thanked 586 Times in 533 Posts
How to combine awk and bash commands in script ?

Dear friends,

I am just trying write one script using 2 files
one file will contain details like below


Code:
#X SERVER X LOCATION
URL="http://www.abcd.com"
FILENAME="abc.txt"
ID_NAME="myabc_xyz"
SERVER_PATH="/usr/local/dummy/html/....."

#Y SERVER Y LOCATION
URL="http://www.xyz.com"
FILENAME="xyz.txt"
ID_NAME="my_dummy_xyz"
SERVER_PATH="/usr/local/server/htm/dummy"

#DUMMY SERVER
 URL="http://www.dummy.com"
 FILENAME="dummy.txt"
 ID_NAME="LMNOP"
 SERVER_PATH="/home/user/abc"

another file will be shell script to process above details
which looks like this


Code:
cd /mypath
sh javascript.sh -x $URL

mv myjava_file $FILENAME

sed -e "s/abcdic...... name\=\"/$ID_NAME name\=\"/g" $FILENAME

find $SERVER_PATH  -iname "*$FILENAME" -delete

mv $FILENAME $SERVER PATH

Here my problem is how do I use above server detail one after another in processing shell script ?

processing shell script should use X server and X location details such as URL, SERVER_PATH, FILENAME, ID_NAME first, then Y server, Z server till end of file..

please help...
Sponsored Links
    #2  
Old Unix and Linux 03-01-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 29 July 2015, 5:12 PM EDT
Location: Galactic Empire
Posts: 3,419
Thanks: 240
Thanked 1,222 Times in 1,148 Posts

Code:
#!/bin/bash

while read line
do
        [[ "$line" =~ ^# ]] && continue
        [[ "$line" =~ ^URL ]] && URL="${line#*=}"
        [[ "$line" =~ ^FILENAME ]] && FILENAME="${line#*=}"
        [[ "$line" =~ ^ID_NAME ]] && ID_NAME="${line#*=}"

        if [[ "$line" =~ ^SERVER_PATH ]]
        then
                SERVER_PATH="${line#*=}"
                URL="${URL//\"/}"
                FILENAME="${FILENAME//\"/}"
                ID_NAME="${ID_NAME//\"/}"
                SERVER_PATH="${SERVER_PATH//\"/}"

                # Now you have all values in variable. 
                # Put your code here
        fi

done < filename

The Following User Says Thank You to Yoda For This Useful Post:
Akshay Hegde (03-01-2013)
Sponsored Links
    #3  
Old Unix and Linux 03-01-2013
Akshay Hegde's Unix or Linux Image
Akshay Hegde Akshay Hegde is offline Forum Advisor  
Forum Advisor
 
Join Date: Nov 2012
Last Activity: 27 July 2015, 5:19 AM EDT
Location: Goa / Bangalore, India
Posts: 1,732
Thanks: 85
Thanked 586 Times in 533 Posts
before putting my code I tried to use echo its not printing


Code:
# Now you have all values in variable. 
 # Put your code here
        echo $URL
        echo $FILENAME
        echo $ID_NAME
        echo $SERVER_PATH
        fi

manually I pasted on terminal




Code:
URL="http://www.abcd.com" 
FILENAME="abc.txt"
ID_NAME="myabc_xyz" 
SERVER_PATH="/usr/local/dummy/html/....."

got output --->


Code:
echo $URL
http://www.abcd.com
user@as:~/Documents/sh$ echo $FILENAME
abc.txt
user@as:~/Documents/sh$ echo $SERVER_PATH
/usr/local/dummy/html/.....

here is error msg

Code:
process.sh: 6: process.sh: [[: not found
process.sh: 7: process.sh: [[: not found
process.sh: 8: process.sh: [[: not found
process.sh: 9: process.sh: [[: not found
process.sh: 11: process.sh: [[: not found
process.sh: 6: process.sh: [[: not found
process.sh: 7: process.sh: [[: not found
process.sh: 8: process.sh: [[: not found
process.sh: 9: process.sh: [[: not found
process.sh: 11: process.sh: [[: not found

    #4  
Old Unix and Linux 03-01-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 29 July 2015, 5:12 PM EDT
Location: Galactic Empire
Posts: 3,419
Thanks: 240
Thanked 1,222 Times in 1,148 Posts
What OS & shell are you using?

Also I noticed that there are some space in front of the string, so remove the caret ^

Code:
#!/bin/bash

while read line
do
        [[ "$line" =~ ^# ]] && continue
        [[ "$line" =~ URL ]] && URL="${line#*=}"
        [[ "$line" =~ FILENAME ]] && FILENAME="${line#*=}"
        [[ "$line" =~ ID_NAME ]] && ID_NAME="${line#*=}"

        if [[ "$line" =~ SERVER_PATH ]]
        then
                SERVER_PATH="${line#*=}"
                URL="${URL//\"/}"
                FILENAME="${FILENAME//\"/}"
                ID_NAME="${ID_NAME//\"/}"
                SERVER_PATH="${SERVER_PATH//\"/}"

                echo "URL: $URL"
                echo "FILENAME: $FILENAME"
                echo "ID_NAME: $ID_NAME"
                echo "SERVER_PATH: $SERVER_PATH"
        fi

done < filename

The Following User Says Thank You to Yoda For This Useful Post:
Akshay Hegde (03-01-2013)
Sponsored Links
    #5  
Old Unix and Linux 03-01-2013
Akshay Hegde's Unix or Linux Image
Akshay Hegde Akshay Hegde is offline Forum Advisor  
Forum Advisor
 
Join Date: Nov 2012
Last Activity: 27 July 2015, 5:19 AM EDT
Location: Goa / Bangalore, India
Posts: 1,732
Thanks: 85
Thanked 586 Times in 533 Posts

Code:
/bin/bash

Ubuntu 12.04 LTS 32-bit
Sponsored Links
    #6  
Old Unix and Linux 03-01-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 29 July 2015, 5:12 PM EDT
Location: Galactic Empire
Posts: 3,419
Thanks: 240
Thanked 1,222 Times in 1,148 Posts
Works for me on Redhat Linux:

Here is the input file:

Code:
$ cat filename
#X SERVER X LOCATION
URL="http://www.abcd.com"
FILENAME="abc.txt"
ID_NAME="myabc_xyz"
SERVER_PATH="/usr/local/dummy/html/....."

#Y SERVER Y LOCATION
URL="http://www.xyz.com"
FILENAME="xyz.txt"
ID_NAME="my_dummy_xyz"
SERVER_PATH="/usr/local/server/htm/dummy"

#DUMMY SERVER
 URL="http://www.dummy.com"
 FILENAME="dummy.txt"
 ID_NAME="LMNOP"
 SERVER_PATH="/home/user/abc"

Here is the o/p:

Code:
$ ./script
URL: http://www.abcd.com
FILENAME: abc.txt
ID_NAME: myabc_xyz
SERVER_PATH: /usr/local/dummy/html/.....
URL: http://www.xyz.com
FILENAME: xyz.txt
ID_NAME: my_dummy_xyz
SERVER_PATH: /usr/local/server/htm/dummy
URL: http://www.dummy.com
FILENAME: dummy.txt
ID_NAME: LMNOP
SERVER_PATH: /home/user/abc

Sponsored Links
    #7  
Old Unix and Linux 03-01-2013
Akshay Hegde's Unix or Linux Image
Akshay Hegde Akshay Hegde is offline Forum Advisor  
Forum Advisor
 
Join Date: Nov 2012
Last Activity: 27 July 2015, 5:19 AM EDT
Location: Goa / Bangalore, India
Posts: 1,732
Thanks: 85
Thanked 586 Times in 533 Posts
then is there any provision to get output in ubuntu 12.04 ?

---------- Post updated at 11:32 PM ---------- Previous update was at 11:27 PM ----------

I installed
Code:
ksh

its printing in #DUMMY SERVER at #1 I had put space(typing mistake) in actual file there is no space , whether I can remove
Code:
^

? and please explain code also..
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Can I combine these two commands into one? boyboy1212 Shell Programming and Scripting 2 03-28-2011 06:01 AM
Execute ssh commands through bash script idet2 Shell Programming and Scripting 3 09-22-2010 11:48 AM
how to run non-standard commands in bash script? EinsteinMcfly Shell Programming and Scripting 12 08-05-2010 01:25 AM
nawk, combine commands numele Shell Programming and Scripting 2 03-17-2010 12:03 PM
Combine commands Spoiler UNIX for Dummies Questions & Answers 2 05-30-2009 04:56 PM



All times are GMT -4. The time now is 10:02 PM.