How to combine awk and bash commands in script ? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

How to combine awk and bash commands in script ?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-01-2013
Akshay Hegde's Avatar
Akshay Hegde Akshay Hegde is offline Forum Advisor  
Forum Advisor
 
Join Date: Nov 2012
Last Activity: 18 December 2014, 11:55 AM EST
Location: Goa / Bangalore, India
Posts: 1,652
Thanks: 82
Thanked 543 Times in 494 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 03-01-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 19 December 2014, 12:26 AM EST
Location: Galactic Empire
Posts: 3,395
Thanks: 240
Thanked 1,211 Times in 1,137 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-02-2013)
Sponsored Links
    #3  
Old 03-02-2013
Akshay Hegde's Avatar
Akshay Hegde Akshay Hegde is offline Forum Advisor  
Forum Advisor
 
Join Date: Nov 2012
Last Activity: 18 December 2014, 11:55 AM EST
Location: Goa / Bangalore, India
Posts: 1,652
Thanks: 82
Thanked 543 Times in 494 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 03-02-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 19 December 2014, 12:26 AM EST
Location: Galactic Empire
Posts: 3,395
Thanks: 240
Thanked 1,211 Times in 1,137 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-02-2013)
Sponsored Links
    #5  
Old 03-02-2013
Akshay Hegde's Avatar
Akshay Hegde Akshay Hegde is offline Forum Advisor  
Forum Advisor
 
Join Date: Nov 2012
Last Activity: 18 December 2014, 11:55 AM EST
Location: Goa / Bangalore, India
Posts: 1,652
Thanks: 82
Thanked 543 Times in 494 Posts

Code:
/bin/bash

Ubuntu 12.04 LTS 32-bit
Sponsored Links
    #6  
Old 03-02-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 19 December 2014, 12:26 AM EST
Location: Galactic Empire
Posts: 3,395
Thanks: 240
Thanked 1,211 Times in 1,137 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 03-02-2013
Akshay Hegde's Avatar
Akshay Hegde Akshay Hegde is offline Forum Advisor  
Forum Advisor
 
Join Date: Nov 2012
Last Activity: 18 December 2014, 11:55 AM EST
Location: Goa / Bangalore, India
Posts: 1,652
Thanks: 82
Thanked 543 Times in 494 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 Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 07:01 AM
Execute ssh commands through bash script idet2 Shell Programming and Scripting 3 09-22-2010 12:48 PM
how to run non-standard commands in bash script? EinsteinMcfly Shell Programming and Scripting 12 08-05-2010 02:25 AM
nawk, combine commands numele Shell Programming and Scripting 2 03-17-2010 01:03 PM
Combine commands Spoiler UNIX for Dummies Questions & Answers 2 05-30-2009 05:56 PM



All times are GMT -4. The time now is 09:36 AM.