Capturing text & putting it in different variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Capturing text & putting it in different variables
# 1  
Old 12-13-2010
Capturing text & putting it in different variables

Dear friends,
I have following text in a variable.

/backup/rem1/10122010/res

From this value of variable i want to take each text in different variables

e.g.

a=backup
b=rem1
c=10122010
d=res

please guide me to do so.
Thank you in advance
Anushree
# 2  
Old 12-13-2010
One way:
Code:
var="/backup/rem1/10122010/res"

set $(echo $var | sed 's/\// /g')

a=$1
b=$2
c=$3
d=$4

# 3  
Old 12-13-2010
Code:
a=`echo "/backup/rem1/10122010/res" | cut -d"\/" -f2`
b=`echo "/backup/rem1/10122010/res" | cut -d"\/" -f3`
c=`echo "/backup/rem1/10122010/res" | cut -d"\/" -f4`
d=`echo "/backup/rem1/10122010/res" | cut -d"\/" -f5`

R0H0N
# 4  
Old 12-13-2010
Thanks Franklin52.
Solution worked...
Thanx a lot
# 5  
Old 12-13-2010
Just Shell:
Code:
var=/backup/rem1/10122010/res

Code:
IFS=/ read x a b c d <<EOF
$var
EOF

bash|ksh93:
Code:
IFS=/ read x a b c d <<<"$var"

ksh:
Code:
echo "$var" | IFS=/ read x a b c d


Last edited by Scrutinizer; 12-13-2010 at 01:13 PM..
# 6  
Old 12-13-2010
Similar to Franklin52

Code:
var="/backup/rem1/10122010/res"
echo "${var}" | sed -e "sX/X Xg"|read a b c d
echo "$a"
echo "$b"
echo "$c"
echo "$d"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Question about global environment variables & fork() exec()

Hello... And thanks in advance for any help anyone can offer me on my question! I've been doing a lot of reading to try and find my answer... But I haven't had any luck What I'm trying to understand is where a child process inherits global environment variables from? I understand the exec()... (2 Replies)
Discussion started by: bodisha
2 Replies

2. Shell Programming and Scripting

Copy files based on specific word in a file name & its extension and putting it in required location

Hello All, Since i'm relatively new in shell script need your guidance. I'm copying files manually based on a specific word in a file name and its extension and then moving it into some destination folder. so if filename contains hyr word and it has .md and .db extension; it will move to TUM/HYR... (13 Replies)
Discussion started by: prajaktaraut
13 Replies

3. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

4. Shell Programming and Scripting

Read record from the text file & assign those values to variables in the script

For eg: I have sample.txt file with 4 rows of record like: user1|password1 user2|password2 user3|password3 user4|password4 The username and password is sepsrated by '|' I want to get the 1st row value from the file and assign it to two different variables(username and password) in my... (1 Reply)
Discussion started by: priya001
1 Replies

5. Shell Programming and Scripting

Shell Variables & awk...Help Please

I apologize if this topic has been beaten to death here, but my limited searching skills did not throw up any results. Here's what I am trying to accomplish List all the files in a certain directory; assign the file names to an array which will be used later in the script. My script looks like... (2 Replies)
Discussion started by: kash80
2 Replies

6. Shell Programming and Scripting

How to check & reset variables

Hi, I'm dealing with a small problem here that I can't seem to overcome by myself. Any help would be greatly appreciated :) Basically, I have three variables, EXTSUB1, EXTSUB2 and EXTSUB3. These variables carry a file the user provides. What I need is to check each of the variables and if the... (1 Reply)
Discussion started by: mutex1
1 Replies

7. Shell Programming and Scripting

regex to remove text before&&after comma chars

Hi, all: I have a question about "cleaning up" a huge file with regular expression(s) and sed: The init file goes like this: block1,blah-blah-blah-blah,numseries1,numseries2,numseries3,numseries4 block2,blah-blah-blah-blah-blah,numseries,numseries2,numseries3,numseries4 ...... (3 Replies)
Discussion started by: yomaya
3 Replies

8. UNIX for Dummies Questions & Answers

Putting echoed text into a new file

Hi, I've set up a script so that a user answers questions, and then these answers come back onto the screen accompanied by text that I've echoed. Is there a way of putting this into a new file? Thanks (7 Replies)
Discussion started by: likelylad
7 Replies

9. Shell Programming and Scripting

scripting headache... loops & variables

Surely there's an easier way to do this, lets see if anyone knows! I am new to scripting so go easy on me! I have the following script and at the moment it doesn't work and I believe the problem is that I am using a while loop within a while loop. When I run the script using sh -x I can see... (6 Replies)
Discussion started by: StevePace
6 Replies

10. Shell Programming and Scripting

Capturing text between ()

Heyo all, I'm looking for the most effective way to capture the text in between a set of parenthesis. For example, my input is this(IBM WebSphereMQ Queue Manager Status): QMNAME(Q1) STATUS(Ended immediately) QMNAME(Q2) ... (7 Replies)
Discussion started by: sysera
7 Replies
Login or Register to Ask a Question