Home > Archive > AWK > May 2006 > selecting a field from a record
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
selecting a field from a record
|
|
| Sparsh_loves_you 2006-05-23, 7:57 am |
|
If I have a record with the following value
record="EXSR U@DMSG^CALLP U@PRINTLOGMSG^^";
note that the field separator in this record is "^" and I want to
pick the first field in this record which is "EXSR U@DMSG".
I want the first field with exact spaces as it appears here.
The following syntax
record="EXSR U@DMSG^CALLP U@PRINTLOGMSG^^";
search_string=`echo $record | awk 'FS="^" {print $1}''`;echo
"$search_string"
prints the value "EXSR U@DMSG" with only one space in between the
words.
How to get the exact field from the record with exact spaces between
the words.
cheers...
| |
| Jens Borchert Pedersen 2006-05-23, 7:57 am |
| "Sparsh_loves_you" <sparsh.mba@gmail.com> wrote in
news:1148383157.890209.26710@38g2000cwa.googlegroups.com:
> If I have a record with the following value
>
> record="EXSR U@DMSG^CALLP U@PRINTLOGMSG^^";
>
> note that the field separator in this record is "^" and I want to
> pick the first field in this record which is "EXSR U@DMSG".
>
> I want the first field with exact spaces as it appears here.
I am not sure but try:
split($0, Array, "^")
print Array[1]
From the GAWK manual:
3.5 Specifying How Fields Are Separated
* Regexp Field Splitting: Using regexps as the field
separator.
* Single Character Fields: Making each character a
separate field.
* Command Line Field Separator: Setting FS from the
command-line.
* Field Splitting Summary: Some final points and a
summary table.
The field separator, which is either a single character or a
regular expression, controls the way awk splits an input
record into fields. awk scans the input record for character
sequences that match the separator; the fields themselves
are the text between the matches.
!!!!! In the examples that follow, we use the bullet symbol (•) to
!!!!! represent spaces in the output. If the field separator is
!!!!! oo, then the following line:
!!!!!
!!!!! moo goo gai pan
!!!!!
!!!!! ! ! !
!!!!! is split into three fields: m, •g, and •gai•pan. Note the
!!!!! leading spaces in the values of the second and third fields.
The field separator is represented by the built-in variable
FS. Shell programmers take note: awk does not use the name
IFS that is used by the POSIX-compliant shells (such as the
Unix Bourne shell, sh, or bash).
Kind regards
Jens Denmark
| |
| Bill Seivert 2006-05-24, 3:57 am |
|
Sparsh_loves_you wrote:
>
> If I have a record with the following value
>
> record="EXSR U@DMSG^CALLP U@PRINTLOGMSG^^";
>
> note that the field separator in this record is "^" and I want to
> pick the first field in this record which is "EXSR U@DMSG".
>
> I want the first field with exact spaces as it appears here.
>
>
> The following syntax
>
> record="EXSR U@DMSG^CALLP U@PRINTLOGMSG^^";
> search_string=`echo $record | awk 'FS="^" {print $1}''`;echo
> "$search_string"
>
> prints the value "EXSR U@DMSG" with only one space in between the
> words.
>
> How to get the exact field from the record with exact spaces between
> the words.
> cheers...
>
Quote your variables, e.g.,
search_string=`echo "$record" | awk 'FS="^" {print $1}'`;echo
"$search_string"
Also, remove extraneous single quote following {print $1}
Bill Seivert
|
|
|
|
|