Using the part of a line as a variable?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using the part of a line as a variable?
# 1  
Old 10-29-2011
Using the part of a line as a variable?

Hello Friends,

I need a command (or script line) that allows me to use of a part of line (given by me) as a variable. Let us assume the name of the command is MYCMD.

When I type

MYCMD fish://mfong@vhl.gov.nd/homefolder/hhk/ADS/

it must do the following job

cd /homefolder/hhk/ADS/


I just want to use the part of line strarting from /homefolder...



Can anyone tell me how to do ?

Thanks
# 2  
Old 10-29-2011
Code:
alias mycmd='perl -wle "$ARGV[0]=~/\@.+?(\/.+)/;print qq(cd $1)"'

hmm sad but 'alias' can't understand that line..
mycmd fish://mfong@vhl.gov.nd/homefolder/hhk/ADS/ not working for some reason

Last edited by tip78; 10-29-2011 at 09:59 AM..
This User Gave Thanks to For This Post:
tip78
# 3  
Old 10-29-2011
Thank you very much tip78.

When I use the command mycmd like that

mycmd fish://mfong@vhl.gov.nd/homefolder/hhk/ADS/
it would do
cd /homefolder/hhk/ADS/

but it couldn't cange my directory to
/homefolder/hhk/ADS/ Smilie

Last edited by rpf; 10-29-2011 at 10:06 AM..
# 4  
Old 10-29-2011
try this:
Code:
alias mycmd='perl -we "$ARGV[0]=~/\@.+?(\/.+)/;`cd $1`"'

or that:
Code:
alias mycmd='perl -we "$ARGV[0]=~/\@.+?(\/.+)/;chdir qq($1)"'


Last edited by tip78; 10-29-2011 at 10:19 AM..
This User Gave Thanks to For This Post:
tip78
# 5  
Old 10-29-2011
sorry both of them don't work again.

thank you for your efforts
# 6  
Old 10-29-2011
well..
Code:
alias mycmd='perl -we "$ARGV[0]=~/\@.+?(\/.+)/;system qq(cd $1)"'

???

whatever
make a script 'mycmd' and put it in your /usr/sbin

Code:
#!/usr/bin/perl -w

chdir"$1" if($ARGV[0]=~/\@.+?(\/.+)/);

if it's not work again try to change 'chdir "$1"' for 'system "cd $1"' or `cd $1`

but FIRST don't forget to make 'unalias mycmd'
This User Gave Thanks to For This Post:
tip78
# 7  
Old 10-29-2011
Quote:
Originally Posted by tip78
try this:
Code:
alias mycmd='perl -we "$ARGV[0]=~/\@.+?(\/.+)/;`cd $1`"'

or that:
Code:
alias mycmd='perl -we "$ARGV[0]=~/\@.+?(\/.+)/;chdir qq($1)"'


it gives the error following

Can't exec "cd": No such file or directory at -e line 1.

---------- Post updated at 02:27 PM ---------- Previous update was at 02:25 PM ----------

ok now trying to put it my bin file

---------- Post updated at 02:37 PM ---------- Previous update was at 02:27 PM ----------

Quote:
Originally Posted by tip78
well..
Code:
alias mycmd='perl -we "$ARGV[0]=~/\@.+?(\/.+)/;system qq(cd $1)"'

???

whatever
make a script 'mycmd' and put it in your /usr/sbin

Code:
#!/usr/bin/perl -w

chdir"$1" if($ARGV[0]=~/\@.+?(\/.+)/);

if it's not work again try to change 'chdir "$1"' for 'system "cd $1"' or `cd $1`

but FIRST don't forget to make 'unalias mycmd'

for all of three there are some syntax errors Smilie

---------- Post updated at 02:47 PM ---------- Previous update was at 02:37 PM ----------

for all of three there are still some syntax errors
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove part of the line from start of the line?

Hello, I am java command from a shell script which will generate the below output on the command prompt signature Base64 :... (10 Replies)
Discussion started by: chetanojha
10 Replies

2. Shell Programming and Scripting

Extract a part of variable/line content in a file

I have a variable and assigned the following values ***XYZ_201519_20150929140642_20150929140644_211_0_0_211 I need to read this variable from backward and stop read when I get first underscore (_) In this scenario I should get 211 Thanks Kris (3 Replies)
Discussion started by: mkris
3 Replies

3. Shell Programming and Scripting

Reading text file, comparing a value in a line, and placing only part of the line in a variable?

I need some help. I would like to read in a text file. Take a variable such as ROW-D-01, compare it to what's in one line in the text file such as PROD/VM/ROW-D-01 and only input PROD/VM into a variable without the /ROW-D-01. Is this possible? any help is appreciated. (2 Replies)
Discussion started by: xChristopher
2 Replies

4. Shell Programming and Scripting

[Solved] Printing a part of the last line of the specific part of a file

Hi, I have 80 large files, from which I want to get a specific value to run a Bash script. Firstly, I want to get the part of a file which contains this: Name =A xxxxxx yyyyyy zzzzzz aaaaaa bbbbbb Value = 57 This is necessary because in a file there are written more lines which... (6 Replies)
Discussion started by: wenclu
6 Replies

5. Shell Programming and Scripting

find the line starting with a pattern and save a part in variable

Hi i have a file which has mutiple line in it. inside that i have a pattern similar to this /abc/def/hij i want to fine the pattern starting with "/" and get the first word in between the the symbols "/" i.e. "abc" in this case into a variable. thanks in advance (13 Replies)
Discussion started by: kichu
13 Replies

6. Shell Programming and Scripting

How read the part of the string into a variable?

Hi, I'm using bash and brand new to shell script. I would like to do the following. I have a string which is "UPDATE=1.0". I would like to read the value "1.0" alone in a variable. i.e the things afer "=" How do I do that? Thanks, (1 Reply)
Discussion started by: scriptfriend
1 Replies

7. Shell Programming and Scripting

Variable of Content From Part of Other File

I may not being doing this description justice, but I'll give it a try. I created a mailx script; there will be several messages using the same script where the only difference is the content. So I figured I'd make the content of the message a variable retrieved from a separate file. I have five... (5 Replies)
Discussion started by: royarellano
5 Replies

8. Shell Programming and Scripting

Copy part of a variable

Hi, i was using a input file to get the last line of the file.But now i have stored the values from the file to a variable and want the last line from the variable . Slightly confused on how to extract that data from the variable. previous code, cat input.txt <TIME>00:15:48</TIME>... (2 Replies)
Discussion started by: Shellslave
2 Replies

9. Shell Programming and Scripting

Repacing part of string with a variable

I have following strings in a file DUPTASMTRMMBAL,20070416200704160117232101172321,,,,,,,@@@Y DUPTASMTRMMCON,20070416200704160127189901271899,,,,,,,@@@Y DUPTASMTRMMHG,,20070416200704160112051001120510,,,,,,,@@@Y What i need to do is replace the date 20070416 with anoth date which is stored in... (4 Replies)
Discussion started by: divz
4 Replies

10. Shell Programming and Scripting

ksh: A part of variable A's name is inside of variable B, how to update A?

This is what I tried: vara=${varb}_count (( vara += 1 )) Thanks for help (4 Replies)
Discussion started by: pa3be
4 Replies
Login or Register to Ask a Question