I have a file where i get some informations through a bash script to put data in a DB table and i'd like to know how to read the first characters of a variable because if it starts with "CE-" that line's data will go into a table if not they must be inserted in an other one, how can i do this?
            Asked
            
        
        
            Active
            
        
            Viewed 709 times
        
    0
            
            
        - 
                    Have you tried searching for a solution, this is a common question. – 123 Sep 07 '16 at 08:10
2 Answers
0
            
            
        Like this-
var=CE-xxxxx
echo "$var"
output- CE-xxxxx
var2=$(echo "$var" | cut -c 1-3)
echo "$var2"
output- CE-
Then you can check if $var2 matches your criteria and use it further.
 
    
    
        Chem-man17
        
- 1,700
- 1
- 12
- 27
0
            
            
        You can use cut to get the bytes that you need:
V="CE-IMPORTANT"
I=$(echo $V | cut -b 4-)
If you want to use the - as separator:
I=$(echo $V | cut -d '-' -f 2)
In both cases you get "IMPORTANT" in I var
 
    
    
        Joan Esteban
        
- 1,006
- 12
- 23
