|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 (permalink) |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 (permalink) |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 (permalink) |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 (permalink) |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 (permalink) |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 (permalink) |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#7 (permalink) |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#8 (permalink) |
|
Messages: n/a
Hébergeur: |
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" |
|
![]() |
| Outils de la discussion | |
|
|