LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 11-14-2008, 12:51 AM   #1
onesikgypo
Member
 
Registered: Jun 2008
Posts: 56

Rep: Reputation: 15
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.

Thankyou.
 
Old 11-14-2008, 03:10 AM   #2
Stéphane Ascoët
Member
 
Registered: Feb 2004
Location: Orleans, 120 km south of Paris
Distribution: Mandrake, Freeduc (the one I'm making), Slackware, MacOS X
Posts: 115

Rep: Reputation: 20
Post Cut

I would use "cut" to store each part in a separate variable and then obtain the first letter of each one.
 
Old 11-14-2008, 03:45 AM   #3
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 494

Rep: Reputation: 75
Hi Onesikgypo,

As alternative to cut you can use the following as well:
Code:
echo "Joe.Smith.Annual.Financial.Statement.Reviews" | sed -e 's/[a-z]*//g' | sed -e 's/\.//g' | tr [:upper:] [:lower:]
sed -e 's/[a-z]*//g'
Will remove all lower case characters

sed -e 's/\.//g'
Will remove the dots

tr [:upper:] [:lower:]
Will convert the string left to lowercase.
 
Old 11-14-2008, 03:53 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Using awk:
Code:
string=Joe.Smith.Annual.Financial.Statement.Reviews
echo $string | awk -F. '
{ for ( i=1; i<=NF; i++ )
    printf "%s", tolower(substr($i,1,1))
}'
 
Old 11-14-2008, 03:58 AM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Using the shell:
Code:
#!/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:]
 
Old 11-14-2008, 04:07 AM   #6
onesikgypo
Member
 
Registered: Jun 2008
Posts: 56

Original Poster
Rep: Reputation: 15
Hey all,

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?

Thankyou.
 
Old 11-14-2008, 05:12 AM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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
Suppose you have this code in test.sh:
Code:
$ ./test.sh
jsafsr
The same if you want to add the date:
Code:
newstring=$newstring$(date +%y.%m.%d)
 
Old 11-14-2008, 05:18 AM   #8
onesikgypo
Member
 
Registered: Jun 2008
Posts: 56

Original Poster
Rep: Reputation: 15
Ok Thankyou,

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.
 
Old 11-14-2008, 05:22 AM   #9
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
A solution for the initial requirement with zsh:

Code:
printf "%.1s" ${(Ls:.:)s=Joe.Smith.Annual.Financial.Statement.Reviews}
 
Old 11-14-2008, 05:28 AM   #10
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by onesikgypo View Post
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.
 
Old 11-14-2008, 05:32 AM   #11
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by radoulov View Post
A solution for the initial requirement with zsh:

Code:
printf "%.1s" ${(Ls:.:)s=Joe.Smith.Annual.Financial.Statement.Reviews}
Very nice! radoulov, you're an ace one-liner, as always!
 
Old 11-14-2008, 10:52 AM   #12
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
Moin,

a shorter sed example:
Code:
echo "Joe.Smith.Annual.Financial.Statement.Reviews" | \
sed -r 's/([A-Za-z])[^.]*\.?/\1/g' | tr '[:upper:]' '[:lower:]'
It works case insensitive.

Jan
 
Old 11-14-2008, 10:57 AM   #13
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
Well,
this is a Linux forum so I'm assuming GNU sed is available:

Code:
sed -r 's/(.)[^.]*\.?/\L\1/g'
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
non-ascii characters in bash script and unicode igor.R Linux - Newbie 31 12-29-2012 04:45 AM
How do I replace special characters in a string within a bash script? rhaup0317 Linux - Newbie 2 06-03-2008 12:56 PM
bash command for removing special characters from string kkpal Linux - Newbie 5 05-26-2008 09:14 AM
shell script question: automatic \ of escape characters in string lagu2653 Programming 2 03-08-2006 08:58 AM
exploding string into individual characters using a shell script farmerjoe Programming 9 10-13-2004 04:23 AM


All times are GMT -5. The time now is 11:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration