Bash Script to get the first characters of a string
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Bash Script to get the first characters of a string
Hey,
I'm looking for a way to "filter" everything but the first characters of a piece of string. For example in:
Joe.Smith.Annual.Financial.Statement.Reviews
it would filter to "jsafsr" (this must be lowercase). I'm currently unsure of the most efficient way to do this, so would really appreciate any help that could be given.
#!/bin/bash
string="Joe.Smith.Annual.Financial.Statement.Reviews"
string=${string//./ }
for i in $(echo $string)
do
printf "%s" ${i:0:1}
done | tr [:upper:] [:lower:]
Thankyou for the responses thus far, i like the shell script one that colucix has posted, simply because it is shell, and it doesnt have the pre-requirement that all the letters were starting with a capital letter, wich is good incase one time the string was Financial.report.For.jones.Smith or alike.
I do have a question re that script however, the first is that for some reason it seems to automatically add a proceeding echo line to the output, to make myself more clear:
stringtest3=${stringtest3//./ }
for i in $(echo $stringtest3)
do
printf "%s" ${i:0:1}
done | tr [:upper:] [:lower:]
date2add=$(date +%y.%m.%d)
echo "$date2add"
and following from the example above, it is currently echoing
jsafsr08.11.14
Now though this was going to be my ultimate goal (i was just going to add it on) it seems a bit weird that it would add itself on in its current format.
Also, i was wondering how i could put the output of that section of the script that was posted into a variable, rather then just echoing it?
I don't understand where the newline is added. Please, use code tags to embed the code, so that newlines, blank spaces and tabs are preserved when posting. For the second part of your question, to store the result in a variable simply concatenate each character with the previous value of the variable itself, for example:
Code:
#!/bin/bash
stringtest3=Joe.Smith.Annual.Financial.Statement.Reviews
stringtest3=${stringtest3//./ }
for i in $(echo $stringtest3)
do
newstring=$newstring$(echo ${i:0:1} | tr [:upper:] [:lower:])
done
echo $newstring
I didnt actually change anything in your art of the code so it stays the same at:
Code:
stringtest3=${stringtest3//./ }
for i in $(echo $stringtest3)
do
printf "%s" ${i:0:1}
done | tr [:upper:] [:lower:]
I just had some more code below that piece, which was
Code:
date2add=$(date +%y.%m.%d)
echo "$date2add"
So the code as a whole simply looked like
Code:
stringtest3=${stringtest3//./ }
for i in $(echo $stringtest3)
do
printf "%s" ${i:0:1}
done | tr [:upper:] [:lower:]
date2add=$(date +%y.%m.%d)
echo "$date2add"
Now With That Coding, what im guessing i shoul dhave seen when i ran the script was:
Code:
jsafsr
08.11.14
But what actually outputted was:
Code:
jsafsr08.11.14
Which seems a bit odd to me?
Not sure if you undrstand the issue i was talking about now?
Thankyou for the help in placing it in a variable.
Not sure if you undrstand the issue i was talking about now?
Yes, of course. Very clear now. The printf statement in my script just formats the output as "%s", that is a string. If you want a newline character, you have to explicitly put it in the format specification, as for "%s\n". But this would not give the desired result, since every character would be followed by a newline and the result would have been
Code:
j
s
a
f
s
r
08.11.14
To put a newline, just use a simple echo statement without arguments.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.