PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Access 1st 12 charectors of a word.
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Access 1st 12 charectors of a word.

Réponse
 
LinkBack Outils de la discussion
Vieux 22/08/2006, 02h39   #1
Dave Kelly
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Access 1st 12 charectors of a word.

I have a list that is 3 words to each line.
I know how to read the file.
I need only the first 12 character of the 3 word. This word is not
guaranteed to be 12 character long. This is where I'm stuck.

Is there an equivalent to the old basic 'left$'? Where do I find it?

What do I search Google for?

TIA Dave
  Réponse avec citation
Vieux 22/08/2006, 02h55   #2
Xicheng Jia
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Access 1st 12 charectors of a word.

Dave Kelly wrote:
> I have a list that is 3 words to each line.
> I know how to read the file.
> I need only the first 12 character of the 3 word. This word is not
> guaranteed to be 12 character long. This is where I'm stuck.
>
> Is there an equivalent to the old basic 'left$'? Where do I find it?
>


cut -c-12

--
Xicheng

  Réponse avec citation
Vieux 22/08/2006, 02h59   #3
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Access 1st 12 charectors of a word.

On 2006-08-22, Dave Kelly wrote:
> I have a list that is 3 words to each line.
> I know how to read the file.
> I need only the first 12 character of the 3 word. This word is not
> guaranteed to be 12 character long. This is where I'm stuck.
>
> Is there an equivalent to the old basic 'left$'? Where do I find it?


To get the first 12 characters of the third word of every line:

awk '{ printf "%s\n", substr($3,1,12) }'

If you only want lines where the thrid word contains at least 12
letters:

awk 'length($3) >=12 { printf "%s\n", substr($3,1,12) }'

If you want to process it in the shell:

word=qwertyuiopasdfghjklzxcvbnm
mask=????????????
junk=${word#$mask}
printf "%s\n" "${word%"$junk"}"

In bash or ksh93:

printf "%s\n" "${word:0:12}"

For other POSIX shells, there is a substr() function in my book
that can do it. (I may previously have posted it here. All the
scripts from the book are available on line.)

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Réponse avec citation
Vieux 22/08/2006, 03h55   #4
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Access 1st 12 charectors of a word.

Chris F.A. Johnson wrote:
> On 2006-08-22, Dave Kelly wrote:
>
>>I have a list that is 3 words to each line.
>>I know how to read the file.
>>I need only the first 12 character of the 3 word. This word is not
>>guaranteed to be 12 character long. This is where I'm stuck.
>>
>>Is there an equivalent to the old basic 'left$'? Where do I find it?

>
>
> To get the first 12 characters of the third word of every line:
>
> awk '{ printf "%s\n", substr($3,1,12) }'
>


Actually, that'll print the first 12 characters of the 3rd string of
non-blanks. For the OP - what's a "word"? For example, in this context:

Bob says "Hi!".

is <"Hi!".> a word, or <"Hi!">, or <Hi!> or <Hi> or something else?

Ed
  Réponse avec citation
Vieux 22/08/2006, 04h26   #5
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Access 1st 12 charectors of a word.

On 2006-08-22, Ed Morton wrote:
> Chris F.A. Johnson wrote:
>> On 2006-08-22, Dave Kelly wrote:
>>
>>>I have a list that is 3 words to each line.
>>>I know how to read the file.
>>>I need only the first 12 character of the 3 word. This word is not
>>>guaranteed to be 12 character long. This is where I'm stuck.
>>>
>>>Is there an equivalent to the old basic 'left$'? Where do I find it?

>>
>>
>> To get the first 12 characters of the third word of every line:
>>
>> awk '{ printf "%s\n", substr($3,1,12) }'
>>

>
> Actually, that'll print the first 12 characters of the 3rd string of
> non-blanks. For the OP - what's a "word"? For example, in this context:
>
> Bob says "Hi!".
>
> is <"Hi!".> a word, or <"Hi!">, or <Hi!> or <Hi> or something else?


Very true, but the OP did say there were 3 words per line. whatever
a word is.

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Réponse avec citation
Vieux 22/08/2006, 04h45   #6
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Access 1st 12 charectors of a word.

Chris F.A. Johnson wrote:
> On 2006-08-22, Ed Morton wrote:
>
>>Chris F.A. Johnson wrote:
>>
>>>On 2006-08-22, Dave Kelly wrote:
>>>
>>>
>>>>I have a list that is 3 words to each line.
>>>>I know how to read the file.
>>>>I need only the first 12 character of the 3 word. This word is not
>>>>guaranteed to be 12 character long. This is where I'm stuck.
>>>>
>>>>Is there an equivalent to the old basic 'left$'? Where do I find it?
>>>
>>>
>>> To get the first 12 characters of the third word of every line:
>>>
>>>awk '{ printf "%s\n", substr($3,1,12) }'
>>>

>>
>>Actually, that'll print the first 12 characters of the 3rd string of
>>non-blanks. For the OP - what's a "word"? For example, in this context:
>>
>> Bob says "Hi!".
>>
>>is <"Hi!".> a word, or <"Hi!">, or <Hi!> or <Hi> or something else?

>
>
> Very true, but the OP did say there were 3 words per line. whatever
> a word is.
>


Right, but he didn't say if "words" were strictly space-separated (e.g.
is "space-separated" one word or 2 to the OP?).

Ed.
  Réponse avec citation
Vieux 22/08/2006, 04h57   #7
Xicheng Jia
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Access 1st 12 charectors of a word.


Ed Morton wrote:
> Chris F.A. Johnson wrote:
> > On 2006-08-22, Dave Kelly wrote:
> >
> >>I have a list that is 3 words to each line.
> >>I know how to read the file.
> >>I need only the first 12 character of the 3 word. This word is not
> >>guaranteed to be 12 character long. This is where I'm stuck.
> >>
> >>Is there an equivalent to the old basic 'left$'? Where do I find it?

> >
> >
> > To get the first 12 characters of the third word of every line:
> >
> > awk '{ printf "%s\n", substr($3,1,12) }'
> >

>
> Actually, that'll print the first 12 characters of the 3rd string of
> non-blanks. For the OP - what's a "word"? For example, in this context:
>
> Bob says "Hi!".
>
> is <"Hi!".> a word, or <"Hi!">, or <Hi!> or <Hi> or something else?


No big deal, just trim all non-word with one more pipeline or y|tr
commands.

--
XC

  Réponse avec citation
Vieux 22/08/2006, 06h15   #8
Dave Kelly
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Access 1st 12 charectors of a word.

Dave Kelly wrote:
> I have a list that is 3 words to each line.
> I know how to read the file.
> I need only the first 12 character of the 3 word. This word is not
> guaranteed to be 12 character long. This is where I'm stuck.
>
> Is there an equivalent to the old basic 'left$'? Where do I find it?
>
> What do I search Google for?
>
> TIA Dave


I did not provide enough information. My apologies.

I am developing a protected directory on my web server. This directory
will contain a membership list. If you are on the membership list you
can download the membership list.

I want the user name to be the first name and last name with no spaces
in between the 2 words. The password will be the format of the 1st 12
characters of the email address.

The membership is put together with excel, sent to me as a cvs file and
takes the form:

Kelly,Dave,10371 Renfaire
Drive,Plantersville,TX,77363,(936)xxx-1xx0,,,1/30/2001
0:00,daveekelly@xxxxxxxxx.net,R,6/30/2005 0:00,2005-2006,,

code I had started:

#!/bin/bash


main()
{
IFS=, ; while read "LastName" "FirstName" "Address" "City" "State" \
"ZipCode" "HomePh" "BusinessPh" "tmp" "DateJoined" "email" \
"MembershipType" "RenewalDate" "MembershipYear" "Paid" "Notes";

do make_list "$LastName" "$FirstName" "$email" ; done
}
make_list()
{
Email=cut -c12 "$email";
echo >> "$firstname""$lastname","$Email"
}
main

Im currently chasing this error "./pdlist.sh: line 14: -c12: command not
found"
  Réponse avec citation
Vieux 22/08/2006, 06h36   #9
Jon LaBadie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Access 1st 12 charectors of a word.

Dave Kelly wrote:
> Dave Kelly wrote:
>> I have a list that is 3 words to each line.
>> I know how to read the file.
>> I need only the first 12 character of the 3 word. This word is not
>> guaranteed to be 12 character long. This is where I'm stuck.
>>
>> Is there an equivalent to the old basic 'left$'? Where do I find it?
>>
>> What do I search Google for?
>>
>> TIA Dave

>
> I did not provide enough information. My apologies.
>
> I am developing a protected directory on my web server. This directory
> will contain a membership list. If you are on the membership list you
> can download the membership list.
>
> I want the user name to be the first name and last name with no spaces
> in between the 2 words. The password will be the format of the 1st 12
> characters of the email address.
>
> The membership is put together with excel, sent to me as a cvs file and
> takes the form:
>
> Kelly,Dave,10371 Renfaire
> Drive,Plantersville,TX,77363,(936)xxx-1xx0,,,1/30/2001
> 0:00,daveekelly@xxxxxxxxx.net,R,6/30/2005 0:00,2005-2006,,
>
> code I had started:
>
> #!/bin/bash
>
>
> main()
> {
> IFS=, ; while read "LastName" "FirstName" "Address" "City" "State" \
> "ZipCode" "HomePh" "BusinessPh" "tmp" "DateJoined" "email" \
> "MembershipType" "RenewalDate" "MembershipYear" "Paid" "Notes";
>
> do make_list "$LastName" "$FirstName" "$email" ; done
> }
> make_list()
> {
> Email=cut -c12 "$email";
> echo >> "$firstname""$lastname","$Email"
> }
> main
>
> Im currently chasing this error "./pdlist.sh: line 14: -c12: command not
> found"


Email=$(echo "$email" | cut -c12)

  Réponse avec citation
Vieux 22/08/2006, 07h37   #10
Dave Kelly
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Access 1st 12 charectors of a word.

Dave Kelly wrote:
> I have a list that is 3 words to each line.
> I know how to read the file.
> I need only the first 12 character of the 3 word. This word is not
> guaranteed to be 12 character long. This is where I'm stuck.
>
> Is there an equivalent to the old basic 'left$'? Where do I find it?
>
> What do I search Google for?
>
> TIA Dave

Works great, Thanks guys.

This command line:

../pdlist.sh < TFF-Membership-8-19-06.csv > pdlist

#!/bin/bash


main()
{
IFS=, ; while read "LastName" "FirstName" "Address" "City" "State" \
"ZipCode" "HomePh" "BusinessPh" "tmp" "DateJoined" "email" \
"MembershipType" "RenewalDate" "MembershipYear" "Paid" "Notes";

do make_list "$LastName" "$FirstName" "$email" ; done
}
make_list()
{
lastname=$1;
firstname=$2;
email=$3;

Email=$(echo "$email" | cut -c-12)
echo "$firstname$lastname",$Email;
}
main

produced my own entry in the list as such.
Exactly what I wanted.


DaveKelly,daveekelly@e

again, Thanks
Dave

  Réponse avec citation
Vieux 22/08/2006, 09h24   #11
Stephane Chazelas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Access 1st 12 charectors of a word.

On Mon, 21 Aug 2006 21:59:16 -0400, Chris F.A. Johnson wrote:
[...]
> In bash or ksh93:
>
> printf "%s\n" "${word:0:12}"


And in zsh: $word[1,12]

> For other POSIX shells, there is a substr() function in my book
> that can do it. (I may previously have posted it here. All the
> scripts from the book are available on line.)


There's a substr function in awk, and there's expr.

awk 'BEGIN{print substr(ARGV[1], 1, 12); exit}' "$word"
expr "x$word" 'x\(.\{0,12\}\)'


--
Stephane
  Réponse avec citation
Vieux 22/08/2006, 10h22   #12
Stephan 'smg' Grein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Access 1st 12 charectors of a word.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dave Kelly wrote:
> I have a list that is 3 words to each line.
> I know how to read the file.
> I need only the first 12 character of the 3 word. This word is not
> guaranteed to be 12 character long. This is where I'm stuck.
>
> Is there an equivalent to the old basic 'left$'? Where do I find it?
>
> What do I search Google for?
>
> TIA Dave

Maybe you try this:
- --- your code ---
and then:
for string in list;
do echo ${string:0:12} # works for bash
done


b.r.,
- --
Stephan 'smg' Grein, <stephan at stephan minus rockt dot de>
http://stephangrein.de
GnuPG-Key-ID: 0xF8C275D4
FingerPrint: 5B6F 134A 189B A24D 342B 0961 8D4B 0230 F8C2 75D4
Geek by nature, Linux by choice.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)

iQIVAwUBROrMyo1LAjD4wnXUAQLElQ/+J4KINCgnaeIVWyeRVWcqdXM47lUOQPj8
EVRa9hvR96ve1l5xC7/o5q4+5agECp274P2S0ay+j6bAMO9NdTrTN5hRiOe5eb9n
zpAsQ3FRzbvSBFDv8ks0XeLxcy65JLfIzWUS+nomrrTJHw6o2E Bna7O/yQTNQIPy
TJgHpwRTKtPBPFU0GNs+Lbqf9JIz4MJv3UDki5rt7Cm/YVxoKXo2OTpVWRR4ulOU
KvJ/RJ1P11F7il7YxF4zT4IpkuVO71bdrPQJcHyKUC/R5SAqh0Wfv9O3SSoLTFu9
UWvRFdKenc68M9Ii/paltbyVgOdFqSWjJ518dQLytzZbbpEpeW6RHxkiGuyTVTXM
IZ+ZQP5rgCSkktuDff4dLfN74J+Kd2RVsmrdW/ouzdJRCFVJmRanOtaDK288Y2qb
iuyyLw6gaK4N6iwlb3EzFUplrNdjOsFY1NPwdskUabDhgDM+qT kBAJaz/DD+i00o
FRNL0uV1/Fts+LL7CUPZnrPPY/4UMnQEJfYnAh7z3xXHI30z8NI3Rk4+DEw42BjM
A/K/dI11WbGth0QGqM3Cf3g8unuJXMyjePrqEsdN5Md3E4jnvjBGEe Forqu5Ehvy
6szPlpbKUzK85jBjGVIY84JvyXjgzO/gteq28QU1/Y/c50Xai9iLvNYQQrUCSmgj
SRooj4A+FAo=
=G0ER
-----END PGP SIGNATURE-----
  Réponse avec citation
Vieux 22/08/2006, 12h58   #13
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Access 1st 12 charectors of a word.

Dave Kelly wrote:
> Dave Kelly wrote:
>
>> I have a list that is 3 words to each line.
>> I know how to read the file.
>> I need only the first 12 character of the 3 word. This word is not
>> guaranteed to be 12 character long. This is where I'm stuck.
>>
>> Is there an equivalent to the old basic 'left$'? Where do I find it?
>>
>> What do I search Google for?
>>
>> TIA Dave

>
> Works great, Thanks guys.
>
> This command line:
>
> ./pdlist.sh < TFF-Membership-8-19-06.csv > pdlist
>
> #!/bin/bash
>
>
> main()
> {
> IFS=, ; while read "LastName" "FirstName" "Address" "City" "State" \
> "ZipCode" "HomePh" "BusinessPh" "tmp" "DateJoined" "email" \
> "MembershipType" "RenewalDate" "MembershipYear" "Paid" "Notes";
>
> do make_list "$LastName" "$FirstName" "$email" ; done
> }
> make_list()
> {
> lastname=$1;
> firstname=$2;
> email=$3;
>
> Email=$(echo "$email" | cut -c-12)
> echo "$firstname$lastname",$Email;
> }
> main
>
> produced my own entry in the list as such.
> Exactly what I wanted.
>
>
> DaveKelly,daveekelly@e
>
> again, Thanks
> Dave
>


awk -F, '{ printf "%s%s,%s\n", $1,$2,substr($3,1,12) }' <
TFF-Membership-8-19-06.csv > pdlist

would do the same.

Ed.
  Réponse avec citation
Vieux 22/08/2006, 17h21   #14
Dave Kelly
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Access 1st 12 charectors of a word.

Ed Morton wrote:
> Dave Kelly wrote:
>> Dave Kelly wrote:
>>
>>> I have a list that is 3 words to each line.
>>> I know how to read the file.
>>> I need only the first 12 character of the 3 word. This word is not
>>> guaranteed to be 12 character long. This is where I'm stuck.
>>>
>>> Is there an equivalent to the old basic 'left$'? Where do I find it?
>>>
>>> What do I search Google for?
>>>
>>> TIA Dave

>>
>> Works great, Thanks guys.
>>
>> This command line:
>>
>> ./pdlist.sh < TFF-Membership-8-19-06.csv > pdlist
>>
>> #!/bin/bash
>>
>>
>> main()
>> {
>> IFS=, ; while read "LastName" "FirstName" "Address" "City"
>> "State" \
>> "ZipCode" "HomePh" "BusinessPh" "tmp" "DateJoined" "email" \
>> "MembershipType" "RenewalDate" "MembershipYear" "Paid" "Notes";
>>
>> do make_list "$LastName" "$FirstName" "$email" ; done
>> }
>> make_list()
>> {
>> lastname=$1;
>> firstname=$2;
>> email=$3;
>> Email=$(echo "$email" | cut -c-12)
>> echo "$firstname$lastname",$Email;
>> }
>> main
>>
>> produced my own entry in the list as such.
>> Exactly what I wanted.
>>
>>
>> DaveKelly,daveekelly@e
>>
>> again, Thanks
>> Dave
>>

>
> awk -F, '{ printf "%s%s,%s\n", $1,$2,substr($3,1,12) }' <
> TFF-Membership-8-19-06.csv > pdlist
>
> would do the same.
>
> Ed.

Wouldn't that give me 'LastNameFirstName,Address' instead of
'FirstNameLastName,email'?

Would 'substr($10,1,12)' get me email?

Thanks for the feedback.
Dave
  Réponse avec citation
Vieux 22/08/2006, 17h30   #15
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Access 1st 12 charectors of a word.

Dave Kelly wrote:
> Ed Morton wrote:
>
>> Dave Kelly wrote:
>>
>>> Dave Kelly wrote:
>>>
>>>> I have a list that is 3 words to each line.
>>>> I know how to read the file.
>>>> I need only the first 12 character of the 3 word. This word is not
>>>> guaranteed to be 12 character long. This is where I'm stuck.
>>>>
>>>> Is there an equivalent to the old basic 'left$'? Where do I find it?
>>>>
>>>> What do I search Google for?
>>>>
>>>> TIA Dave
>>>
>>>
>>> Works great, Thanks guys.
>>>
>>> This command line:
>>>
>>> ./pdlist.sh < TFF-Membership-8-19-06.csv > pdlist
>>>
>>> #!/bin/bash
>>>
>>>
>>> main()
>>> {
>>> IFS=, ; while read "LastName" "FirstName" "Address" "City"
>>> "State" \
>>> "ZipCode" "HomePh" "BusinessPh" "tmp" "DateJoined" "email" \
>>> "MembershipType" "RenewalDate" "MembershipYear" "Paid" "Notes";
>>>
>>> do make_list "$LastName" "$FirstName" "$email" ; done
>>> }
>>> make_list()
>>> {
>>> lastname=$1;
>>> firstname=$2;
>>> email=$3;
>>> Email=$(echo "$email" | cut -c-12)
>>> echo "$firstname$lastname",$Email;
>>> }
>>> main
>>>
>>> produced my own entry in the list as such.
>>> Exactly what I wanted.
>>>
>>>
>>> DaveKelly,daveekelly@e
>>>
>>> again, Thanks
>>> Dave
>>>

>>
>> awk -F, '{ printf "%s%s,%s\n", $1,$2,substr($3,1,12) }' <
>> TFF-Membership-8-19-06.csv > pdlist
>>
>> would do the same.
>>
>> Ed.

>
> Wouldn't that give me 'LastNameFirstName,Address' instead of
> 'FirstNameLastName,email'?
>

Yes. Switch $1 and $2 and replace $3 by $11.

> Would 'substr($10,1,12)' get me email?


Looks to me like it'd be $11.

Ed.
  Réponse avec citation
Vieux 23/08/2006, 17h59   #16
Michal Nazarewicz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Access 1st 12 charectors of a word.

Dave Kelly <daveekelly@earthlink.net> writes:

> I need only the first 12 character of the 3 word. This word is not
> guaranteed to be 12 character long. This is where I'm stuck.


That's pretty ugly but works on ny POSIX shell and does not require
external aplications (thus fork + exec):

while [ $#WORD -gt 12 ]; do WORD="${WORD%?}"; done

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo--
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 09h57.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,31488 seconds with 24 queries