Sponsored Content
Top Forums Shell Programming and Scripting How to parse a string into variables Post 302324769 by cfajohnson on Thursday 11th of June 2009 05:10:35 PM
Old 06-11-2009
Quote:
Originally Posted by qneill
This comes up enough I thought I'd drop it here (so the next time I need it I can google for it Smilie

Using "IFS", "set", and "for without the in keyword".

Note this will stomp on any arguments passed to the script, and messing with IFS has dangerous side effects to the rest of the script (remember to set it back!)

Code:
  SEP=:  # arbitrary
  FIELDS='yack":blah)::3wasempty:blec!:'

  OLDIFS="$IFS"; IFS=$SEP
  set -- $FIELDS
  for STR
  do
    echo STR=$STR
  done
  IFS="$OLDIFS"


Note what happens if:

Code:
FIELDS='yack":blah):*:3wasempty:blec!:'

And there's no need for that loop if all you're doing is printing the fields:

Code:
set -f
printf "%s\n" $FIELDS
set +f

Your code could also cause failure later in the script if IFS was unset; the behaviour when IFS is unset is not the same as when it is empty.
Quote:
Also, you can use ##, #, %%, % to walk through values but it's a little more painful
Code:
  SEP=:  # arbitrary
  FIELDS='yack":blah)::3wasempty:blec!:'

  REST="$FIELDS"
  for x in 1 2 3 4 5


There's no need to limit the number of fields; see below.
Quote:
Code:
  do
    # chop off trailing fields
    echo ${REST%%$SEP*}
    # chop off leading field
    REST=${REST#*$SEP}
    # detect last field, works with empty fields too (trailing :)
    [ "$REST" = "${REST#*$SEP}" ] && break
  done


Code:
sep=:  # arbitrary
fields='yack":blah)::3wasempty:blec!:'

while [ -n "$fields" ]
do
  # chop off leading field
  rest=${fields#*"$sep"}

  # print the first field
  printf "%s\n" "${fields%"$sep$rest"}"

  fields=$rest
done

Quote:
And finally, you can always use sed/awk/cut/python/ruby/perl ...
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to parse config variables from external file to shell script

How do i use a config.txt to recursively pass a set of variables to a shell script eg my config.txt looks like this : path=c://dataset/set1 v1= a.bin v2= b.bin path=c://dataset/set2 v1= xy.bin v2= abc.bin .................. and so on . and my testscript : (2 Replies)
Discussion started by: pradsh
2 Replies

2. Shell Programming and Scripting

How can I parse a record found in /etc/passwd into variables?

I am working with the Oracle 10.2.0.3 job scheduler on Solaris 10, and unfortunately, the scheduler executes scripts in such a way that several default shell environment variables are not defined. For example, $HOME, $USER, and $LOGNAME are missing. How can I parse the appropriate record in... (7 Replies)
Discussion started by: shew01
7 Replies

3. Shell Programming and Scripting

How to: Parse text string into variables using Korn shell

I am writing a script to keep check on free disk space, and I would like to find a way to parse $LINE (see code below) into a numeric value (for free disk space percentage) and a string value (for mount point). If possible, I would like to avoid sed or any additional use of awk since I am not very... (7 Replies)
Discussion started by: shew01
7 Replies

4. Shell Programming and Scripting

Want to parse output for variables in Bash

Trying to finish up my script that automates some video encoding work. Situation: There is an MKV file to be transcoded. Problem: MKVINFO will give a bunch of output about an MKV file, included in that output are two lines I am interested in: | + Default duration: 41.708ms (23.976 fps... (9 Replies)
Discussion started by: randyharris
9 Replies

5. Shell Programming and Scripting

Parse config file and store the values in variables

Hi, I have a config file that has blank, commented lines. I need to escape commented lines, blank lines, parse the remaining lines and store them in variables or array. the config file contains the following lines. # config file # Define Oracle User ORA_USER=abcde ORA_PASS=xyzabc... (8 Replies)
Discussion started by: Lakshmi Chowdam
8 Replies

6. UNIX for Dummies Questions & Answers

Parse or cut concat variables to individual values

Hello I need to pass some environment parameters to a datastage job and am getting an error when trying to send the complete concatinated variable. I have decided to parse out just the values and send as parameters but am struggling to find the best way to do this (actually I am not very... (3 Replies)
Discussion started by: LynnC
3 Replies

7. Shell Programming and Scripting

Need to parse lines in a file into two words and assign the values to two variables

For example, I have a file with below lines containing VOB tags and VOB paths. * /vobs/fts/FTSUSM20_VOB /ccvobsslx01/projects/vobs/eml/FTSUSM20_VOB * /vobs/fts/FTS20_VOB /ccvobsslx01/projects/vobs/eml/FTS20_VOB * /vobs/pmv/PMS_VOB /ccvobsslx01/projects/vobs/cpm/_/PMS_VOB *... (4 Replies)
Discussion started by: senthilkc
4 Replies

8. Shell Programming and Scripting

parse a mixed alphanumeric string from within a string

Hi, I would like to be able to parse out a substring matching a basic pattern, which is a character followed by 3 or 4 digits (for example S1234 out of a larger string). The main string would just be a filename, like Thisis__the FileName_S1234_ToParse.txt. The filename isn't fixed, but the... (2 Replies)
Discussion started by: keaneMB
2 Replies

9. Linux

Parse ; deliminated variable to create variables

Hi there, would appreciate some help on this parsing problem if anybody can help im trying to parse a variable with the following output, each of the values im trying to parse are deliminated by a ; T192... (8 Replies)
Discussion started by: scottish_jason
8 Replies

10. Shell Programming and Scripting

Parse variables from C++ to shell

Hi All, #include <iostream> int main() { std::int foo = 34; system("mkdir /home/linuxUser/fooDir"); system("vi fooFile") system("write foo in fooFile") foo = 43; foo = read foo from fooFile; std::cout << "foo = " << foo ; } result should be foo = 34 can... (3 Replies)
Discussion started by: linuxUser_
3 Replies
All times are GMT -4. The time now is 08:46 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy