Strip a string in sh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Strip a string in sh
# 1  
Old 03-23-2010
Strip a string in sh

I have a list of servers that I need my script to ping however this list also has the env they belong too such as SIT, PRD, warehouse and so on.
The break character for each section is :
A value in my list would look like this...
Code:
brutus.grhq.xxx.com:warehouse

Where brutus.grhq.gfs.com is the server that the following code needs to ping. I am trying to figure out how to say....
Code:
/lcl/tools/bin/sshPing.pl $remote_server > /dev/null

But drop the :warehouse part. So another words I am trying to figure out how to say sshPing.pl brutus.grhq.gfs.com and drop the :warehouse.
I thought you could do something like |cut -f 1 -d: however I do not see how I could use that in that line of code to break up the string.
Any ideas? Really lost on this one.Thanks.
# 2  
Old 03-23-2010
use Parameter Expansion
# 3  
Old 03-23-2010
Code:
echo "brutus.grhq.xxx.com:warehouse" | sed 's/:.*//'

# 4  
Old 03-23-2010
Any examples of how I might do that?
I do not see much for examples on Google.
# 5  
Old 03-23-2010
Code:
var='brutus.grhq.xxx.com:warehouse'
remoteserver=${var%:*}

# 6  
Old 03-23-2010
Well the problem is I do not really need to echo it. I just need to use the string.

I tried....
Code:
/lcl/tools/bin/sshPing.pl $remote_server | sed 's/:.*//' > /dev/null

But that did not work.

---------- Post updated at 07:43 AM ---------- Previous update was at 07:40 AM ----------

Also tried...

Code:
/lcl/tools/bin/sshPing.pl ${remote_server%:*} > /dev/null

But I got... bad substitution

---------- Post updated at 07:47 AM ---------- Previous update was at 07:43 AM ----------

Also tried...

Code:
var=$remote_server
remote=${var%:*}
/lcl/tools/bin/sshPing.pl $remote > /dev/null

But I still get an error of bad sub.

---------- Post updated at 07:55 AM ---------- Previous update was at 07:47 AM ----------

Tried this as well....

Code:
/lcl/tools/bin/sshPing.pl $remote_server sed 's/:.*//' > /dev/null

Any ideas? Thanks
# 7  
Old 03-23-2010
Suppose you have the list of remote servers in a file:
Code:
while read var
do
  remote=${var%:*}
  /lcl/tools/bin/sshPing.pl $remote > /dev/null
done < file

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Strip leading and numbers from a string.

Hello I have two vars loaded with $VAR1="ISOMETHING103" $VAR2="COTHERTHING04" I need to: 1) Strip the first char. Could be sed 's/^.//' 2) The number has it's rules. If it has "hundreds", it needs to be striped. If it is just two digits it shouldn't. So, for VAR1 output should be... (7 Replies)
Discussion started by: tristezo2k
7 Replies

2. Shell Programming and Scripting

Strip First few Characters

I want to strip first few characters from each record until a proper datesamp is found. Request for getNextPage.................06/29/12 07:49:30 VVUKOVIC@67.208.166.131{7A805FEF76A62FCBB23EA78B5380EF95.tomcat1}TP-Processor14 LogExchUsage: ERROR:: isprof=false : exch=NSDQ output should be... (2 Replies)
Discussion started by: ratheeshjulk
2 Replies

3. Solaris

strip error

Hi I am getting the below error while using strip command. strip: libelf error. Request error: no string table strip: a.out: file not manipulated Could somebody please let me know what might be the solution?? It is in ksh and solaris 10. Thanks in advance (4 Replies)
Discussion started by: vali__
4 Replies

4. Shell Programming and Scripting

Strip out the string

awk -F"\t" -vOFS="\t" '{print $1"\t-\t-","",$6,$7"\t-"$8"\t-\t-\t"$15}' file.tsv > output.tsv Using the above command how to remove the string www.abc.com from the $7 value. (7 Replies)
Discussion started by: sandy1028
7 Replies

5. Shell Programming and Scripting

How to strip out common terms in string

Hi, I have this kinda of data:- 0,0,0,0,1,2,0,4,5,6,7,foo 0,0,0,0,1,4,0,5,5,5,5,foo1 0,0,6,0,1,6,0,6,1,2,3,orange etc... I wanted to remove the 0 which occur on the same rows of foo,foo1 and orange in this case. Desired output is:- 0,1,2,4,5,6,7,foo 0,1,4,5,5,5,5,foo1... (9 Replies)
Discussion started by: ahjiefreak
9 Replies

6. Programming

Strip command

I am new in Unix. I go through the man strip. But did not understand that, why when we have -G (debug and release ) option in the compiler, than using strip command to strip the debug information from the objects. i want to binary for teh production i will compile it without debug option. What the... (4 Replies)
Discussion started by: Saurabh78
4 Replies

7. Shell Programming and Scripting

Need to strip a string

I have a file that looks like this: /home/fred/opt/bin /opt/usr/bin /usr/sbin/var/opt I need a way to chop of everything after the last occurance of the / sign including the /. So the file above will now look like this below. /home/fred/opt /opt/usr /usr/sbin/var I tried using... (6 Replies)
Discussion started by: x96riley3
6 Replies

8. Shell Programming and Scripting

Strip all non-alphanumerics

Hi, Can someone let me know how do I strip out any non-alphanumeric character in string tomake it alphanumeric? i.e abc def ghi ->abcdefghi abc-def-ghi ->abcdefghi abc#def-ghi->abcdefghi Thanks in advance (3 Replies)
Discussion started by: braindrain
3 Replies

9. Shell Programming and Scripting

How to strip strins

Guys, Please can someone tell me how to strip each string from the following ? TABLE1||METHOD||TYPE||STATUS||DATE What i need is to assign each value to the variable TABLE1 to var1 METHOD to var2 and so on If there is NULL in one of them, something like A||B||||C|| I want the... (14 Replies)
Discussion started by: kamathg
14 Replies
Login or Register to Ask a Question