* parsing a string
@ 2004-08-10 0:08 dave
2004-08-10 2:12 ` John Aspinall
0 siblings, 1 reply; 3+ messages in thread
From: dave @ 2004-08-10 0:08 UTC (permalink / raw)
To: linux-newbie
I'm new to bash and am trying to write a script that puts 3 substrings in 3
variables. I have a string with password,firstname,lastname. and I need it to
be in 3 variables. I bought a bash scripting book but I can't seem to make it
work. Thanks in advance.
Dave
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: parsing a string
2004-08-10 0:08 parsing a string dave
@ 2004-08-10 2:12 ` John Aspinall
2004-08-10 8:48 ` Jeff Woods
0 siblings, 1 reply; 3+ messages in thread
From: John Aspinall @ 2004-08-10 2:12 UTC (permalink / raw)
To: dave; +Cc: linux-newbie
dave@dpomeroy.com wrote:
> I'm new to bash and am trying to write a script that puts 3 substrings in 3
> variables. I have a string with password,firstname,lastname. and I need it to
> be in 3 variables.
I'm just an apprentice here, but I think "cut" is a good choice of tool.
$ echo 'password,firstname,lastname' | cut -d',' -f1
password
$ echo 'password,firstname,lastname' | cut -d',' -f2
firstname
$ echo 'password,firstname,lastname' | cut -d',' -f3
lastname
-d is the field delimiter, -f says which fields you want
John
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: parsing a string
2004-08-10 2:12 ` John Aspinall
@ 2004-08-10 8:48 ` Jeff Woods
0 siblings, 0 replies; 3+ messages in thread
From: Jeff Woods @ 2004-08-10 8:48 UTC (permalink / raw)
To: John Aspinall; +Cc: dave, linux-newbie
At 8/9/2004 10:12 PM -0400, John Aspinall wrote:
>dave@dpomeroy.com wrote:
>>I'm new to bash and am trying to write a script that puts 3 substrings in 3
>>variables. I have a string with password,firstname,lastname. and I need
>>it to
>>be in 3 variables.
>
>I'm just an apprentice here, but I think "cut" is a good choice of tool.
>
>$ echo 'password,firstname,lastname' | cut -d',' -f1
>password
>$ echo 'password,firstname,lastname' | cut -d',' -f2
>firstname
>$ echo 'password,firstname,lastname' | cut -d',' -f3
>lastname
>
>-d is the field delimiter, -f says which fields you want
I agree that "cut" is a wonderful tool on the command line. However, a
more purely bash solution is to use a powerful feature of variable
expansion in bash. For example:
pass_first_last='password,firstname,lastname'
pass=${pass_first_last%%,*}
first_last=${pass_first_last#*,}
first=${first_last%,*}
last=${first_last#*,}
or, alternatively,
last=${pass_first_last##*,}
The "#" operator in variable expansion takes a pattern argument and removes
as little as possible from the left side of the variable value that matches
the pattern.
The "##" operator in variable expansion takes a pattern argument and
removes as much as possible from the left side of the variable value that
matches the pattern.
The "%" operator in variable expansion takes a pattern argument and removes
as little as possible from the right side of the variable value that
matches the pattern.
The "%%" operator in variable expansion takes a pattern argument and
removes as much as possible from the right side of the variable value that
matches the pattern.
A common use for this is to extract the final filename from a pathname:
filename=${pathname##*/}
Another is to extract the directory path name from a file's path:
dirname=${pathname%/*}
P.S. I was trying to use cut to extract one field from a file using FS
(\0x1C) as the field delimiter. I discovered it was non-trivial to get the
FS into my cut command. The solution turned out to be a "string expansion"
in bash:
cut -d$'\x1C' -f2 filename
--
Jeff Woods <kazrak+kernel@cesmail.net>
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2004-08-10 8:48 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-08-10 0:08 parsing a string dave
2004-08-10 2:12 ` John Aspinall
2004-08-10 8:48 ` Jeff Woods
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.