Extract first letter from a given string

  • Follow


Hi all;
I'm wondering how to do to extract a given letter from a given string
(word). The position of letter to search can change . Also the
position of string (word) can change in a given line.
for example:
in command line I type:
This is a test for extracting letter

I need to extract for example the first letter in each word,i.e :
"T" from This
"i" from is
"a" from a
"t" from test
"f" from  for..... and so on.

What can I do is to copy this line into file, then use the next awk
command:

echo "Enter a line:"
read line
echo "$line" >  file.txt
awk -F' ' '{for (i = 1; i <= NF; i++) print $i}' file.txt

Then I must do some think like this:
awk  '{for (j = 1; j <= NF; j++) print $FIRST_letter_from_$0 }'
file.txt > file1.txt
How to write
$FIRST_letter_from_$0 in shell?
at the end file1.txt will contain only the first letter from each line
in file.txt, i.e:
T
i
a
t
f

Is there simple way to do this?
Thanks
0
Reply Nezhate 5/19/2008 2:23:25 PM

On 19 Mai, 17:23, Nezhate <mazouz.nezh...@gmail.com> wrote:
> Hi all;
> I'm wondering how to do to extract a given letter from a given string
> (word). The position of letter to search can change . Also the
> position of string (word) can change in a given line.
> for example:
> in command line I type:
> This is a test for extracting letter
>
> I need to extract for example the first letter in each word,i.e :
> "T" from This
> "i" from is
> "a" from a
> "t" from test
> "f" from  for..... and so on.
>
> What can I do is to copy this line into file, then use the next awk
> command:
>
> echo "Enter a line:"
> read line
> echo "$line" >  file.txt
> awk -F' ' '{for (i = 1; i <= NF; i++) print $i}' file.txt
>
> Then I must do some think like this:
> awk  '{for (j = 1; j <= NF; j++) print $FIRST_letter_from_$0 }'

You meant $FIRST_letter_from_$i, I guess?

{ for (j = 1; j <= NF; j++)
    print substr($i,1,1)
}


Janis

> file.txt > file1.txt
> How to write
> $FIRST_letter_from_$0 in shell?
> at the end file1.txt will contain only the first letter from each line
> in file.txt, i.e:
> T
> i
> a
> t
> f
>
> Is there simple way to do this?
> Thanks

0
Reply Janis 5/19/2008 2:37:32 PM


On 19 Mai, 17:37, Janis <janis_papanag...@hotmail.com> wrote:
> On 19 Mai, 17:23, Nezhate <mazouz.nezh...@gmail.com> wrote:
>
> > awk -F' ' '{for (i = 1; i <= NF; i++) print $i}' file.txt
>
> > Then I must do some think like this:
> > awk  '{for (j = 1; j <= NF; j++) print $FIRST_letter_from_$0 }'
>
> You meant $FIRST_letter_from_$i, I guess?

Oops, must be 'j'.

>
> { for (j = 1; j <= NF; j++)
>     print substr($i,1,1)

      print substr($j,1,1)

>
> }
>
> Janis
0
Reply Janis 5/19/2008 2:39:23 PM

On Mon, 19 May 2008 07:23:25 -0700 (PDT), Nezhate <mazouz.nezhate@gmail.com> wrote:

>Hi all;
>I'm wondering how to do to extract a given letter from a given string
>(word). The position of letter to search can change . Also the
>position of string (word) can change in a given line.
>for example:
>in command line I type:
>This is a test for extracting letter

~$ echo "This is a test" | awk '{for(i=1;i<=NF;i++)print substr($i,1,1)}'
T
i
a
t

RTFM: http://www.gnu.org/software/gawk/manual/

Grant.
-- 
http://bugsplatter.mine.nu/
0
Reply Grant 5/19/2008 3:08:59 PM

In article <is53345khpum87dms1doa8cq6mak12hamh@4ax.com>,
Grant  <g_r_a_n_t_@dodo.com.au> wrote:
>On Mon, 19 May 2008 07:23:25 -0700 (PDT), Nezhate
><mazouz.nezhate@gmail.com> wrote:
>
>>Hi all;
>>I'm wondering how to do to extract a given (?) letter (!) from a given string
>>(word). The position of letter (!) to search can change . Also the
>>position of string (word) can change in a given line.
>>for example:
>>in command line I type:
>>This is a test for extracting letter
>
>~$ echo "This is a test" | awk '{for(i=1;i<=NF;i++)print substr($i,1,1)}'
>T
>i
>a
>t

Um, I think you guys are missing the point.

 % echo 5guys ._hi | gawk '{for (i=1;i<=NF;i++) print substr($i,match($i,/[A-Za-z]/),1)}'
g
h
 %
0
Reply gazelle 5/19/2008 3:17:20 PM

On Mon, 19 May 2008 15:17:20 +0000 (UTC), gazelle@xmission.xmission.com (Kenny McCormack) wrote:

>In article <is53345khpum87dms1doa8cq6mak12hamh@4ax.com>,
>Grant  <g_r_a_n_t_@dodo.com.au> wrote:
>>On Mon, 19 May 2008 07:23:25 -0700 (PDT), Nezhate
>><mazouz.nezhate@gmail.com> wrote:
>>
>>>Hi all;
>>>I'm wondering how to do to extract a given (?) letter (!) from a given string
>>>(word). The position of letter (!) to search can change . Also the
>>>position of string (word) can change in a given line.
>>>for example:
>>>in command line I type:
>>>This is a test for extracting letter
>>
>>~$ echo "This is a test" | awk '{for(i=1;i<=NF;i++)print substr($i,1,1)}'
>>T
>>i
>>a
>>t
>
>Um, I think you guys are missing the point.
>
> % echo 5guys ._hi | gawk '{for (i=1;i<=NF;i++) print substr($i,match($i,/[A-Za-z]/),1)}'
>g
>h
> %

So that's why it looked too easy?  :o)

Grant.

-- 
http://bugsplatter.mine.nu/
0
Reply Grant 5/19/2008 3:27:53 PM


On 5/19/2008 10:27 AM, Grant wrote:
> On Mon, 19 May 2008 15:17:20 +0000 (UTC), gazelle@xmission.xmission.com (Kenny McCormack) wrote:
> 
> 
>>In article <is53345khpum87dms1doa8cq6mak12hamh@4ax.com>,
>>Grant  <g_r_a_n_t_@dodo.com.au> wrote:
>>
>>>On Mon, 19 May 2008 07:23:25 -0700 (PDT), Nezhate
>>><mazouz.nezhate@gmail.com> wrote:
>>>
>>>
>>>>Hi all;
>>>>I'm wondering how to do to extract a given (?) letter (!) from a given string
>>>>(word). The position of letter (!) to search can change . Also the
>>>>position of string (word) can change in a given line.
>>>>for example:
>>>>in command line I type:
>>>>This is a test for extracting letter
>>>
>>>~$ echo "This is a test" | awk '{for(i=1;i<=NF;i++)print substr($i,1,1)}'
>>>T
>>>i
>>>a
>>>t
>>
>>Um, I think you guys are missing the point.
>>
>>% echo 5guys ._hi | gawk '{for (i=1;i<=NF;i++) print substr($i,match($i,/[A-Za-z]/),1)}'
>>g
>>h
>>%
> 
> 
> So that's why it looked too easy?  :o)
> 
> Grant.
> 

If Kenny's right and "word"s might start with non-alpha-numeric characters since
the OP said "letter" instead of "character", then it's entirely possible that
"word"s aren't always space-separated so this:

$ echo "The end.The start." | gawk '{for (i=1;i<=NF;i++) print substr($i,match(
$i,/[A-Za-z]/),1)}'
T
e
s

still isn't correct. We need some clearer input from the OP.

	Ed.

0
Reply Ed 5/19/2008 10:38:31 PM

It works! Thanks to all for help.
The link for Gawk manual is very useful !
Nezhate.
0
Reply Nezhate 5/20/2008 6:48:52 AM

7 Replies
331 Views

(page loaded in 0.287 seconds)

Similiar Articles:













7/22/2012 8:29:13 AM


Reply: