0

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?

1
  • Have you tried searching for a solution, this is a common question. Commented Sep 7, 2016 at 8:10

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.