LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-03-2013, 10:23 AM   #1
gldetro
LQ Newbie
 
Registered: May 2013
Posts: 2

Rep: Reputation: Disabled
Having a problem removing the path from the output of a variable


Here is my code:

cols=$1
assetType=$2
whichDir=$3
# Check for all parameters
if [ $# -lt 3 ]; then
echo "[$programID] ibmAF-I Missing Asset type parameter"
echo "[$programID] ibmAF-I Usage: $programID <columns> <assettype> <directory>"
exit 2
fi
#########################################################
fileSet="ls $assetDir/$assetType/$whichDir/*"
for file in $fileSet
do
hostName=`echo $file | awk 'BEGIN {FS="::"}{print $1}'`
ipAddress=`echo $file | awk 'BEGIN {FS="::"}{print $2}'`
newStatus=`echo $file | awk 'BEGIN {FS="::"}{print $3}'`
jdate=`echo $file | awk 'BEGIN {FS="::"}{print $4}'`
echo "$hostName...$ipAddress...$newStatus...$jdate"
done


Output:

ibmAFMEB:/opt/lampp/htdocs/ibmQuickCloud/bin # ./ibmQuickCloudBuildTableEntry 4 SA4SD21 Inactive
ls.........
/opt/lampp/htdocs/ibmQuickCloud/assets/SA4SD21/Inactive/SA4TeamSD003...10.91.0.203...Ready...2013151
/opt/lampp/htdocs/ibmQuickCloud/assets/SA4SD21/Inactive/SA4TeamSD004...10.91.0.204...Ready...2013151
/opt/lampp/htdocs/ibmQuickCloud/assets/SA4SD21/Inactive/SA4TeamSD005...10.91.0.205...Ready...2013151
/opt/lampp/htdocs/ibmQuickCloud/assets/SA4SD21/Inactive/SA4TeamSD006...10.91.0.206...Ready...2013151
/opt/lampp/htdocs/ibmQuickCloud/assets/SA4SD21/Inactive/SA4TeamSD007...10.91.0.207...Ready...2013151
/opt/lampp/htdocs/ibmQuickCloud/assets/SA4SD21/Inactive/SA4TeamSD008...10.91.0.208...Ready...2013151
/opt/lampp/htdocs/ibmQuickCloud/assets/SA4SD21/Inactive/SA4TeamSD009...10.91.0.209...Ready...2013151
/opt/lampp/htdocs/ibmQuickCloud/assets/SA4SD21/Inactive/SA4TeamSD010...10.91.0.210...Ready...2013151

I want to remove the /opt/lampp/htdocs/ibmQuickCloud/assets/SA4SD21/Inactive part of the output... any ideas (have played with basename and sed with no luck).
 
Old 06-03-2013, 10:34 AM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,133
Blog Entries: 2

Rep: Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839
What exactly is not working with basename? Here it is how it looks on my system:
Code:
bash-4.2$ basename /opt/lampp/htdocs/ibmQuickCloud/assets/SA4SD21/Inactive/SA4TeamSD010...10.91.0.210...Ready...2013151
SA4TeamSD010...10.91.0.210...Ready...2013151
 
Old 06-03-2013, 10:44 AM   #3
gldetro
LQ Newbie
 
Registered: May 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
the variable hostname is the fully qualified path/filename (/opt/lampp/htdocs/ibmQuickCloud/assets/SA4SD21/Inactive/SA4TeamSD003)

I want to remove the path from the so that it only displays SA4TeamSD003. Basename only works with files so that isn't going to work I have finally figured out.
 
Old 06-03-2013, 11:21 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,516

Rep: Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893
Well not sure where you are getting your information from ... but it is wrong.

Basename does not only work with files:
Code:
$ x=/opt/lampp/htdocs/ibmQuickCloud/assets/SA4SD21/Inactive/SA4TeamSD003
$ basename $x
SA4TeamSD003
Assuming assetDir is being set further up in your code and your unusual file names are of the format 'SA4TeamSD010::10.91.0.210::Ready::2013151', I would probably use something like:
Code:
for file in $assetDir/$assetType/$whichDir/*
do
    items_arr=(${file//::/ })

    echo "${item_arr[0]##*/}...${item_arr[1]}...${item_arr[2]}...${item_arr[3]}"
done
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in setting Path variable for ns2.34 in ubunto 10.04 Aska123 Linux - Software 6 04-11-2013 06:14 AM
Help: removing a variable substring from a string variable in sh script gnparsons Programming 2 06-04-2008 05:21 PM
path variable problem sang_froid Linux - Newbie 1 10-26-2007 09:56 AM
Very strange $PATH variable problem -- Desperate for HELP! W0bbles Slackware 2 01-31-2006 11:24 PM
RedHat 7.3 PATH env variable problem NewRedHatter Linux - Newbie 4 09-01-2002 05:14 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration