cut through variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cut through variable
# 1  
Old 04-18-2008
cut through variable

hi,

can some one put more light.

i want to perform cut operation on a variable, but i am not
able to do so as the variable contain special character.

e.g var="adc|cfr\\df|{dff}|df}}"

command :

var1=`cut -c10-12 $var`

i gives error.

Thanks in advance.
# 2  
Old 04-18-2008
$var is treated as a file in your cut command. Use echo and then pipe it to cut.

Code:
echo $var | cut -c10-12

# 3  
Old 04-18-2008
Probably safer to double quote it, as it contains special characters.

Code:
echo "$var" | cut -c10-12

You could also use the shell's substitution facilities.

Code:
# Pluck off first ten characters
tmp1=${var#??????????}
# Now take another two
tmp2=${tmp1#??}
# Now trim everything after those two
var1=${tmp1%$tmp2}

This probably breaks if you have a too short string (but then so does the cut, albeit in a different way).

Last edited by era; 04-18-2008 at 03:48 AM.. Reason: Final substitution was wrong (var instead of tmp1, duh)
# 4  
Old 04-18-2008
thanks for all your help

can you tell me how to cut the last two character of a variable
if the varaible is not of fix length.

right now i am performing this operation through taking size of variable
then cutting last two character.

i any short cut to it, with out taking the lenght of variable.

thanks
# 5  
Old 04-18-2008
The variable substitution technique can be used just like I showed you above.

Code:
# Pluck off last two
tmp1=${var%??}
# Now return just the last two
var1=${var#$tmp1}

If the variable var contains abcdef then tmp1 will contain abcd, and in the final step, we trim off the value of tmp1 from the beginning of var, leaving ef

(Unclear whether you mean "take away" or "extract" the last two, but, well, this demonstrates both.)
# 6  
Old 04-18-2008
thanks for all your help

it worked fine
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 assign value to variable using cut?

Hey guys. Below is the command I am using to assign "yellow" to the variable yellow. I can seem to echo it out using xargs but when I assign it to yellow and then try to echo it out it prints a blank line. Below is the command. Your help is highly appreciated! cut -c 2- color.txt |... (11 Replies)
Discussion started by: eldan88
11 Replies

2. Shell Programming and Scripting

Result of 'cut' into variable

Hello, I would like to have a result of a cut-command in a variable. The file "t.dat" has the following content: Company 001.239879123.OB1X.672W12.STS UNOLD.001.02 My bash-script: Header="" Header=$(cut -c1-160 t.dat | head -1) echo $Header ... (9 Replies)
Discussion started by: API
9 Replies

3. UNIX for Dummies Questions & Answers

How to cut a string from a variable

I want to cut column 1 to 5 echo $somevariable | cut -c 1-5 not working (6 Replies)
Discussion started by: ezee
6 Replies

4. Shell Programming and Scripting

cut and store last value of a variable into other

Hi All, I am a newbie to unix.starting my career in unix.need 1 help from you all..pls help.. i am passing a file name "abc_delta" as argument to my script1.sh. if file name contains "_delta" at last then echo pass else fail.how to fix it. Note:file name will always contain "_delta" at... (10 Replies)
Discussion started by: pradeepcarya
10 Replies

5. Shell Programming and Scripting

How to cut a string from a variable

hello, i need to cut a string in unix but not from file ,, from variable, i searched on special line , and i need a specific data from this line , i get it on variable, how can i cut data that i need ??? merci (2 Replies)
Discussion started by: Reham.Donia
2 Replies

6. Shell Programming and Scripting

Cut last 7 characters of a variable

I need to cut the last seven characters of a variable length variable. The variable may be 7 characters or 70. I need to always be able to grab the last 7 characters. I looked at the cut command but it always seems to want to start at the beginning of a line, not the end and count backwards. ... (5 Replies)
Discussion started by: kblawson14
5 Replies

7. UNIX for Dummies Questions & Answers

Cut from inside a saved variable

ok, heres my problem. i have saved the last line from a ls -l command into a variable. What i want to do is be able to cut the second character into that (i.e. the r if its there of the - if its not there) and save that value into another variable. here is the current code i have to save... (1 Reply)
Discussion started by: strasner
1 Replies

8. UNIX for Dummies Questions & Answers

Cut Command value assign to variable

Hi, I am new to UNIX Scripting. I have been trying to use the CUT command to retrieve part of the header from a file and assign it to a variable. I have tried searching a lot, but I am still unsuccessful. Sample Header: HJAN BALANCE 20090616 I need to retrieve the date here, which always... (10 Replies)
Discussion started by: ragz_82
10 Replies

9. Shell Programming and Scripting

Cut position as a variable

Hi All, I wish to cut an input by using the below command but i would want to have the cut position as a variable. For eg, the position number is 11 now and i wish that this number is being assigned to a variable before putting into the cut function. Can this be done ? Can any expert help?... (1 Reply)
Discussion started by: Raynon
1 Replies

10. UNIX for Dummies Questions & Answers

how to cut a string from a variable

Hello All, I have a string type variable called "FULLSTR". This variable has a value of "path=/type/source/logs". I need to cut the characters after the character "=" to end of the string. Example: the new variable "MATCHSTR" should have value like this... "/type/source/logs" So... (2 Replies)
Discussion started by: kjaisan
2 Replies
Login or Register to Ask a Question