Extraction of the output from a string.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extraction of the output from a string.
# 1  
Old 04-06-2009
Extraction of the output from a string.

Hi Everyone,

I stored the result of a certain awk script in the variable arr.The result is /inets /banking /tools.
arr= /inets /banking /tools

These are 3 direcctories. I should be able to move in to these directories using "cd" command.Can you tell me how to extract a particular directory,say '/inets', from this variable? Thanks in advance to everyone.
# 2  
Old 04-06-2009
Say your variable stores the three dirs
amit@softpc142108 /export/home/amit $ echo $arr
/inets /banking /tools
amit@softpc142108 /export/home/amit $ echo $arr | cut -d ' ' -f1
/inets
amit@softpc142108 /export/home/amit $ echo $arr | cut -d ' ' -f2
/banking
amit@softpc142108 /export/home/amit $ echo $arr | cut -d ' ' -f3
/tools

So you can use

cd `echo $arr | cut -d ' ' -f1`
# 3  
Old 04-08-2009
If you needed to go to each of the directories...

arr="/inets /banking /tools"

for DIR in $(echo ${arr})
do
cd ${DIR}
pwd
done
# 4  
Old 04-08-2009
Thanks a lot both of you..that cleared my problem...One more question...How to prevent the "root" directory details from appearing in the ouput of the df -k command? Basically I am trying to create a disk-monitoring script here...Your help is greatly appreciated...
# 5  
Old 04-08-2009
df -k | grep -v root
# 6  
Old 04-08-2009
Sorry for not being specific...by root i mean "/"...This is the output obtained when i run df -k command:
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/hda2 10317860 4464896 5328844 46% /
/dev/hdc2 4128432 3840868 77852 99% /banktools
/dev/hda1 256667 60949 182466 26% /boot
/dev/hdc3 4128432 2212820 1705900 57% /home
/dev/hdc1 25802268 20828460 3663084 86% /inets
none 2050664 0 2050664 0% /dev/shm
/dev/hda5 2063504 34000 1924684 2% /tmp
/dev/hda3 8254272 2184980 5649996 28% /var

In order to make my script work I need to remove the entry marked in red from the output.
When i run df -k | grep -v / it is displaying only the following line in the output:
Filesystem 1K-blocks Used Available Use% Mounted on

Thanks a lot for your continual assistance.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

String Extraction

I am trying to extract a time from the below string in perl but not able to get the time properly I just want to extract the time from the above line I am using the below syntax x=~ /(.*) (\d+)\:(\d+)\:(\d+),(.*)\.com/ $time = $2 . ':' . $3 . ':' . $4; print $time Can... (1 Reply)
Discussion started by: karan8810
1 Replies

2. UNIX for Dummies Questions & Answers

String extraction from log file

Hi, Log file will contain text like below: May 7 14:12:09 nap_fujitsu-nexus_pbb-denek-01-r_DE.de.ignite.net 108767: 115673: May 7 14:12:08: %RTT-3-IPSLATHRESHOLD: IP SLA Monitor(112): Threshold below for rtt May 8 14:12:09 nap_fujitsu-nexus_pbb-denek-01-r_DE.de.ignite.net 108767: 115673:... (3 Replies)
Discussion started by: Dip
3 Replies

3. Solaris

string extraction won't work. Why?

#!/usr/bin/ksh set -x testfile=my.test.file.flag echo ${testfile: (-4)} #/home/maldohe/scripts/spawn1& sleep 3 echo myspawn is now ending exit Background: I am trying to extract the word flag from anf given file name. This is a demo script that I am working on to fix a production issue.... (8 Replies)
Discussion started by: Harleyrci
8 Replies

4. Solaris

extraction of sar output

Hi, Anyone knows how to extract sar command output to excel or Is there any free grapical tools to extract this sar log file. thanks, regards (2 Replies)
Discussion started by: vijill
2 Replies

5. Shell Programming and Scripting

Sub-string extraction on arrays

Hi, I'm trying to extract the middle of an array that is of variable length but always has a first and last common element, The following works OK... #!/bin/bash ARRAY='switch' ARRAY='option1' ARRAY='option2' ARRAY='option3' ARRAY='value' SWITCH=${ARRAY:0:1} VALUE=${ARRAY:(-1)}... (1 Reply)
Discussion started by: ASGR
1 Replies

6. Shell Programming and Scripting

String Extraction in Perl

I have a string stored in a variable. For instance, $str = " Opcode called is : CM_OP_xxx " where xxx changes dynamically and can be either LOGIN or SEARCH..... depends on runtime. For example : $str = " Opcode called is : CM_OP_SEARCH " $str = " Opcode called is : CM_OP_LOGIN " I... (3 Replies)
Discussion started by: vkca
3 Replies

7. Shell Programming and Scripting

Need help in string extraction using regular expressions

Hi, I am a new bee to this forum. I am trying to extract the text after a matching pattern from a url using regular expression. Ex: http://locatlhost:2020/proxy/checkthisout I want to extract the string after proxy/. I am not familiar with reg ex. Can someone please help? (2 Replies)
Discussion started by: akatraga
2 Replies

8. UNIX for Dummies Questions & Answers

String extraction from a text file

The following script code works great for extracting 'postmaster' from a line of text stored in a variable named string: string="PenaltyError:=554 5.7.1 Error, send your mail to postmaster@LOCALDOMAIN" stuff=$( echo $string | cut -d@ -f1 | awk '{ print $NF }' ) echo $stuff However, I need to be... (9 Replies)
Discussion started by: cleanden
9 Replies

9. Shell Programming and Scripting

Extraction of string from Stringlist using delimiter

Hi Experts, I need to extract some set of strings one be one using delimiter. Example: shellscript.sh|unix.sh|script_file.sh i need to extract this shellscript.sh,unix.sh,script_file.sh separately. I tried but couldn't get. Please help me.. Thanks & Regards :), Kanda (3 Replies)
Discussion started by: spkandy
3 Replies

10. Shell Programming and Scripting

String extraction from user input - sh

Hi, I have a shell script to build components of a product. The follow snippet will explain what I am doing. # !/bin/sh for choice in "$@" ; do case $choice in "o") echo "Calling $choice" ; o ;; "i") echo... (8 Replies)
Discussion started by: vino
8 Replies
Login or Register to Ask a Question