How to cut a string from a variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to cut a string from a variable
# 1  
Old 02-01-2012
MySQL How to cut a string from a variable

I want to cut column 1 to 5
Code:
echo $somevariable | cut -c 1-5

not working
# 2  
Old 02-01-2012
What is your input? What is the output you want? In what way is it "not working"?
# 3  
Old 02-01-2012
Input is line like

Code:
somevariable="0110SH"

# 4  
Old 02-01-2012
You want column or character?
Code:
root@bt> echo "123456789" | cut -c1-5
12345

root@bt> echo "1 2 3 4 5 6 7 8 9" | cut -f1-5 -d" "
1 2 3 4 5

--ahamed
# 5  
Old 02-01-2012
my line is coming is coming as:
Code:
line=01100S

Code I am using is :
Code:
while read line 
do
i=`$line | cut -c 1-5`    #Command not found
if[ ($line | cut -c 1-5) -eq "01100" ]    #not working
then
    echo "PASS"
fi
done < file_name

# 6  
Old 02-01-2012
Code:
i=`echo $line | cut -c1-5`

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 7  
Old 02-01-2012
Quote:
Originally Posted by ezee
my line is coming is coming as:
Code:
line=01100S

Code I am using is :
Code:
while read line 
do
i=`echo $line | cut -c 1-5`    #Command not found
if [ `echo $line | cut -c 1-5` -eq "01100" ]    #not working
then
    echo "PASS"
fi
done < file_name

You also need a space before the square bracket.

Actually if your shell supports string operations, you might find this simpler and save a cpl of processes
Code:
i=${line:0:5}

Code:
${string:position:length}Extract $length characters substring from $string at $position [zero-indexed, first character is at position 0]

Great reference here I got from another post: http://tldp.org/LDP/abs/html/refcards.html#AEN22518
This User Gave Thanks to gary_w For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check if string variable is a subset of another string variable

Below is my ksh shell script where I need to check if variable fileprops is a subset of $1 argument. echo "FILE PROPERTY: $fileprops" echo "PARAMETER3: $1" if ; then echo "We are Good. $line FILE is found to be INTACT !! " else echo... (2 Replies)
Discussion started by: mohtashims
2 Replies

2. 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

3. Shell Programming and Scripting

Cut the string

---------- Post updated at 10:31 AM ---------- Previous update was at 10:28 AM ---------- Hello, I am trying to get the string cut based on the following needs: String1=Submitted request 25574824 for CONCURRENT SQLAP RUAPACTUALSEXT Y RUAPACTUALS122313100616.dat "2013/01/12 14:50:44"... (6 Replies)
Discussion started by: cartrider
6 Replies

4. Shell Programming and Scripting

Cut the string

Hi in a directory i've files having the following name for_category_info_19990101984301 for_catgry_meta_19991111214601 ------- I just want the name till year and month i.e; for_category_info_199901 for_catgry_meta_199911 How can i achieve the above string Thanks (2 Replies)
Discussion started by: smile689
2 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. Shell Programming and Scripting

Cut string from a line into a variable

Hi, I am working on a ksh script and I´m stuck on the following: I have to get the pthread_id from a procstack file for a particular tid#. ---------- tid# 1274057 (pthread ID: 1800) ---------- ---------- tid# 1736913 (pthread ID: 4019) ---------- ---------- tid# 1478705 (pthread ID: ... (7 Replies)
Discussion started by: tmf33uk
7 Replies

8. Shell Programming and Scripting

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. (5 Replies)
Discussion started by: xyz123
5 Replies

9. Shell Programming and Scripting

read string, check string length and cut

Hello All, Plz help me with: I have a csv file with data separated by ',' and optionally enclosed by "". I want to check each of these values to see if they exceed the specified string length, and if they do I want to cut just that value to the max length allowed and keep the csv format as it... (9 Replies)
Discussion started by: ozzy80
9 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