Sponsored Content
Top Forums UNIX for Dummies Questions & Answers extract from string variable into new variables Post 100874 by Sniper Pixie on Thursday 2nd of March 2006 12:36:34 PM
Old 03-02-2006
extract from string variable into new variables

I have a variable which consists of a string like this:

001 aaabc 44 a bbb12

How do I extract each substring, delimited by the spaces, into new variables - one for each substring?

eg var1 will be 001, var2 will be aaabc, var3 will be 44, var4 will be a, etc?

I've come up with this:
var1=$(echo $string | awk 'NF=1') ; var2=$(echo $string | awk 'NF=2') ; var3=$(echo $string | awk 'NF=3')

but var2 = NF1 and 2, var3 = NF 1,2,3 etc
It also seems a bit long winded and impractical for operating on a lengthy string.

Last edited by Sniper Pixie; 03-02-2006 at 02:58 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract numbers from a string and store in variables

Hi All, Is it possible in Unix shell script to extract numbers from a string containing ".", such as; 5.2.314 And store in variables so; var1 = 5 var2 = 2 var3 = 314 Thanks in advance for any help anyone can provide dave (6 Replies)
Discussion started by: davewg
6 Replies

2. Shell Programming and Scripting

How to extract variable number from a String

hi,I am new to shell script,I have String,like this: Number of rows exported: 5321 the numbe at end could changing,how can I extract this number and assign it to a variable,then use it later in script. thanks. (19 Replies)
Discussion started by: vitesse
19 Replies

3. UNIX for Dummies Questions & Answers

Using GREP to extract variable following a string

Hello, I'm running calculations and I need to extract a specific number from a output file. So far I've only been able to GREP entire lines containing the string: '1 F=' . I would like to go a step further and extract just the number following '1 F='. The entire line looks like: 1 F=... (10 Replies)
Discussion started by: modey3
10 Replies

4. Shell Programming and Scripting

Search for string in a file and extract another string to a variable

Hi, guys. I have one question: I need to search for a string in a file, and then extract another string from the file and assign it to a variable. For example: the contents of the file (group) is below: ... ftp:x:23: mail:x:34 ... testing:x:2001 sales:x:2002 development:x:2003 ...... (6 Replies)
Discussion started by: daikeyang
6 Replies

5. Shell Programming and Scripting

sub-string extract between variable delimiters

I need to extract certain pieces from a string, wher delimiters may vary. For example A0 B0 C0 12345677 X0 Y0 Z0 A1-B1 C1 12345678 X1 Y0 Z0 A1/B2 C77 12345679 X2 Y0 Z0 I need to get C0 12345677 X0 C1 12345678 X1 C77 12345679 X2 I tried sed, see example below: echo 'A0 B0... (2 Replies)
Discussion started by: migurus
2 Replies

6. Shell Programming and Scripting

Bash:How to split one string variable in two variables?

Hello, I have a paramter $param consisting just of two literals and want to split it into two parameters, so I can combine it to a new parameter <char1><string><char2>, but the following code didn't work: tmp_PARAM_1=cut -c1 $PARAM tmp_PARAM_2=cut -c2 $PARAM... (2 Replies)
Discussion started by: ABE2202
2 Replies

7. Shell Programming and Scripting

Extract a string from a file and store it in variable

Hi, I've a file ImageSizeDetails.txt with the following contents: Image Name: swncd 01.10.00.04 Created: Wed Jan 9 14:05:48 2013 Image Type: ARM Linux Multi-File Image (gzip compressed) Data Size: 7351011 Bytes = 7178.72 kB = 7.01 MB Load Address: 00008000 Entry Point: ... (6 Replies)
Discussion started by: Parameswaran
6 Replies

8. UNIX for Dummies Questions & Answers

Extract variable name in a string

Hi, I'm doing a script to list all scripts called by a "master" script. But I have an issue as there is some variables in the name of the called scripts. Example: % cat master_script.sh ENVIR=PROD VERSION=1.2 /users/maturix/$ENVIR/program_$VERSION.shI would like my script displays a kind... (7 Replies)
Discussion started by: maturix
7 Replies

9. Shell Programming and Scripting

ksh String Manipulation - removing variables from within a variable

Hi. I'd like to remove all values in a string variable that also exist in a second variable. What is the appropriate approach to take here? I can use a 'For' loop and check each element and then populate a new string. But is there a cleaner, simpler way? E.g. I have the following 2 variables ... (19 Replies)
Discussion started by: user052009
19 Replies

10. Shell Programming and Scripting

Enhance existing script: Extract Multiple variables & Input in an echo string

Hi Experts I need your help to optimize my script to execute better as I have nearly 1M records & the script is taking close to 40 minutes to execute, so would need support on a faster alternative. Input: file {"house":"1024","zip":"2345","city":"asd","country":"zzv"}... (2 Replies)
Discussion started by: nk1984
2 Replies
scan(3tcl)						       Tcl Built-In Commands							scan(3tcl)

__________________________________________________________________________________________________________________________________________________

NAME
scan - Parse string using conversion specifiers in the style of sscanf SYNOPSIS
scan string format ?varName varName ...? _________________________________________________________________ INTRODUCTION
This command parses substrings from an input string in a fashion similar to the ANSI C sscanf procedure and returns a count of the number of conversions performed, or -1 if the end of the input string is reached before any conversions have been performed. String gives the input to be parsed and format indicates how to parse it, using % conversion specifiers as in sscanf. Each varName gives the name of a variable; when a substring is scanned from string that matches a conversion specifier, the substring is assigned to the corresponding vari- able. If no varName variables are specified, then scan works in an inline manner, returning the data that would otherwise be stored in the variables as a list. In the inline case, an empty string is returned when the end of the input string is reached before any conversions have been performed. DETAILS ON SCANNING
Scan operates by scanning string and format together. If the next character in format is a blank or tab then it matches any number of white space characters in string (including zero). Otherwise, if it is not a % character then it must match the next character of string. When a % is encountered in format, it indicates the start of a conversion specifier. A conversion specifier contains up to four fields after the %: a XPG3 position specifier (or a * to indicate the converted value is to be discarded instead of assigned to any variable); a number indicating a maximum substring width; a size modifier; and a conversion character. All of these fields are optional except for the conversion character. The fields that are present must appear in the order given above. When scan finds a conversion specifier in format, it first skips any white-space characters in string (unless the conversion character is [ or c). Then it converts the next input characters according to the conversion specifier and stores the result in the variable given by the next argument to scan. If the % is followed by a decimal number and a $, as in "%2$d", then the variable to use is not taken from the next sequential argument. Instead, it is taken from the argument indicated by the number, where 1 corresponds to the first varName. If there are any positional specifiers in format then all of the specifiers must be positional. Every varName on the argument list must correspond to exactly one con- version specifier or an error is generated, or in the inline case, any position can be specified at most once and the empty positions will be filled in with empty strings. The size modifier field is used only when scanning a substring into one of Tcl's integer values. The size modifier field dictates the | integer range acceptable to be stored in a variable, or, for the inline case, in a position in the result list. The syntactically valid | values for the size modifier are h, L, l, and ll. The h size modifier value is equivalent to the absence of a size modifier in the the | conversion specifier. Either one indicates the integer range to be stored is limited to the same range produced by the int() function of | the expr command. The L size modifier is equivalent to the l size modifier. Either one indicates the integer range to be stored is limited | to the same range produced by the wide() function of the expr command. The ll size modifier indicates that the integer range to be stored | is unlimited. The following conversion characters are supported: d The input substring must be a decimal integer. It is read in and the integer value is stored in the variable, truncated as required by the size modifier value. o The input substring must be an octal integer. It is read in and the integer value is stored in the variable, truncated as required by the size modifier value. x The input substring must be a hexadecimal integer. It is read in and the integer value is stored in the variable, truncated as required by the size modifier value. u The input substring must be a decimal integer. The integer value is truncated as required by the size modifier value, and the corresponding unsigned value for that truncated range is computed and stored in the variable as a decimal string. The conversion makes no sense without reference to a truncation range, so the size modifier ll is not permitted in combination with conversion character u. i The input substring must be an integer. The base (i.e. decimal, binary, octal, or hexadecimal) is determined in the same fashion as described in expr. The integer value is stored in the variable, truncated as required by the size modifier value. c A single character is read in and its Unicode value is stored in the variable as an integer value. Initial white space is not skipped in this case, so the input substring may be a white-space character. s The input substring consists of all the characters up to the next white-space character; the characters are copied to the vari- able. e or f or g The input substring must be a floating-point number consisting of an optional sign, a string of decimal digits possibly contain- ing a decimal point, and an optional exponent consisting of an e or E followed by an optional sign and a string of decimal dig- its. It is read in and stored in the variable as a floating-point value. [chars] The input substring consists of one or more characters in chars. The matching string is stored in the variable. If the first character between the brackets is a ] then it is treated as part of chars rather than the closing bracket for the set. If chars contains a sequence of the form a-b then any character between a and b (inclusive) will match. If the first or last character between the brackets is a -, then it is treated as part of chars rather than indicating a range. [^chars] The input substring consists of one or more characters not in chars. The matching string is stored in the variable. If the character immediately following the ^ is a ] then it is treated as part of the set rather than the closing bracket for the set. If chars contains a sequence of the form a-b then any character between a and b (inclusive) will be excluded from the set. If the first or last character between the brackets is a -, then it is treated as part of chars rather than indicating a range value. n No input is consumed from the input string. Instead, the total number of characters scanned from the input string so far is stored in the variable. The number of characters read from the input for a conversion is the largest number that makes sense for that particular conversion (e.g. as many decimal digits as possible for %d, as many octal digits as possible for %o, and so on). The input substring for a given conversion terminates either when a white-space character is encountered or when the maximum substring width has been reached, whichever comes first. If a * is present in the conversion specifier then no variable is assigned and the next scan argument is not consumed. DIFFERENCES FROM ANSI SSCANF
The behavior of the scan command is the same as the behavior of the ANSI C sscanf procedure except for the following differences: [1] %p conversion specifier is not supported. [2] For %c conversions a single character value is converted to a decimal string, which is then assigned to the corresponding varName; no substring width may be specified for this conversion. [3] The h modifier is always ignored and the l and L modifiers are ignored when converting real values (i.e. type double is used for the internal representation). The ll modifier has no sscanf counterpart. [4] If the end of the input string is reached before any conversions have been performed and no variables are given, an empty string is returned. EXAMPLES
Convert a UNICODE character to its numeric value: set char "x" set value [scan $char %c] Parse a simple color specification of the form #RRGGBB using hexadecimal conversions with substring sizes: set string "#08D03F" scan $string "#%2x%2x%2x" r g b Parse a HH:MM time string, noting that this avoids problems with octal numbers by forcing interpretation as decimals (if we did not care, we would use the %i conversion instead): set string "08:08" ;# *Not* octal! if {[scan $string "%d:%d" hours minutes] != 2} { error "not a valid time string" } # We have to understand numeric ranges ourselves... if {$minutes < 0 || $minutes > 59} { error "invalid number of minutes" } Break a string up into sequences of non-whitespace characters (note the use of the %n conversion so that we get skipping over leading whitespace correct): set string " a string {with braced words} + leading space " set words {} while {[scan $string %s%n word length] == 2} { lappend words $word set string [string range $string $length end] } Parse a simple coordinate string, checking that it is complete by looking for the terminating character explicitly: set string "(5.2,-4e-2)" # Note that the spaces before the literal parts of # the scan pattern are significant, and that ")" is # the Unicode character u0029 if { [scan $string " (%f ,%f %c" x y last] != 3 || $last != 0x0029 } then { error "invalid coordinate string" } puts "X=$x, Y=$y" An interactive session demonstrating the truncation of integer values determined by size modifiers: | % set tcl_platform(wordSize) | 4 | % scan 20000000000000000000 %d | 2147483647 | % scan 20000000000000000000 %ld | 9223372036854775807 | % scan 20000000000000000000 %lld | 20000000000000000000 | SEE ALSO
format(3tcl), sscanf(3) KEYWORDS
conversion specifier, parse, scan Tcl 8.4 scan(3tcl)
All times are GMT -4. The time now is 10:13 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy