Parse String from a Variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parse String from a Variable
# 1  
Old 04-07-2008
Parse String from a Variable

Hello,

Is there a quick way to parse the values from a variable?

The variable has the following sample input:
TA=[IV_Test PF_SAPP_FWK]

The values of the TA variable is not fixed/hardcoded

Basically I need to get the IV_Test and PF_SAPP_FWK values.

I created a script that first use sed to remove [ ] , then redirect it to a file
Then use awk to parse the contents of the file, and redirect it to another file.

Code:
echo $TA | sed 's/\[//g' | sed 's/\]//g' > $TEMP_FILE
awk -F" " '{
        for (i = 1; i <= NF; i++) {
           n = split($i, q, " ")
           print q[n] >> "$TEMP_FILE1"
        }
        }'$TEMP_FILE

while read TA_LEVEL
        do
        echo "VALUE: $TA_LEVEL"
done < $TEMP_FILE1


Im just a newbie in scripting and I appreciate your help.

Thanks,
racbern
# 2  
Old 04-07-2008
You don't really need the temp file.

Take care to properly quote any user input.

Code:
echo "$TA" | sed -e 's/TA=\[//' -e 's/]$//' -e 's/ /\
/g' |
while read TA_LEVEL; do
  echo TA_LEVEL="$TA_LEVEL"
done

Some seds are picky when it comes to newlines inside a quoted string so you might have to experiment with that, or maybe fall back to your awk solution (or tr ' ' '\012' or some such).

Do I infer correctly that the number of labels between the [brackets] can be variable, and you want to break them up to one per line? That wasn't entirely clear from your problem description, but looks like that's what your code does. If it's always two fields then maybe this could be simplified further.
# 3  
Old 04-07-2008
Try this one:

Code:
echo "$TA" | awk 'BEGIN{FS="\[|\]"}{split($2,s," ");print s[1],s[2]}'

Regards
# 4  
Old 04-07-2008
If you're looking to do it in a single line, try this:

echo $TA | perl -pe 's/^\[(.*) (.*)\]$/$1\n$2/' > file.txt

Hope this helps.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grepping for one variable while using awk to parse an associated variable

Im trying to search for a single variable in the first field and from that output use awk to extract out the lines that contain a value less than a value stored in another variable. Both the variables are associated with each other. Any guidance is appreciated. File that contains the... (6 Replies)
Discussion started by: ncwxpanther
6 Replies

2. Shell Programming and Scripting

Help about parse the variable

I'm using bash the code is QEMU_CMD="qemu-system-x86_64 -smp 2 -m 512 $QEMU_PARAMETER -hda $GUEST_IMAGE -kernel $GUEST_KERNEL -append \"root=/dev/hda rw console=ttyS0,115200 ip=$IP_PARAMETER \" -nographic" echo "..............................." echo "qemu command is... (9 Replies)
Discussion started by: yanglei_fage
9 Replies

3. Shell Programming and Scripting

How to parse xml file in variable-string?

In the wake of the post: how-parse-following-xml-file Thank you for the very useful chakrapani response 302355585-post4 ! A close question. How to pass a file to xmllint in variable? For example, let it be: NEARLY_FILE='<?xml version="1.0" encoding="iso-8859-1"?><html><set label="09/07/29"... (0 Replies)
Discussion started by: OleM2k
0 Replies

4. 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

5. UNIX for Advanced & Expert Users

How to parse nested variable

Hi, I want to parse nested variable in my script. for exp- c=1 G1='0318' G2='0023' G3='3092' G4='0014' while ;do g=G$c a=$g echo "Group=$g and value=$a" c=`expr $c + 1` done final output are as - --------------------------- Group=G1 and... (4 Replies)
Discussion started by: apskaushik
4 Replies

6. Shell Programming and Scripting

Parse string

Hi, I need to parse a string, check if there are periods and strip the string. For example i have the following domains and subdomains: mydomain.com, dev.mydomain.com I need to strip all periods so i have a string without periods or domain extensions: mydomain, devmydomain. I use this for... (12 Replies)
Discussion started by: ktm
12 Replies

7. Shell Programming and Scripting

how to parse value of the variable

I have a variable which has a full path to the file, for example : A=/t1/bin/f410pdb Does anybody know the command to parce this variable and assign the result to 3 other variables so each subdirectory name will be in a new variable like this B=t1 C=bin D=f410pdb Many thanks -A (5 Replies)
Discussion started by: aoussenko
5 Replies

8. Shell Programming and Scripting

how to parse this string

I want to get filenames from the following input. How can I parse this in bash. input data ------------------------------------------------------------------- path=/aaa/bbb/filename1;/aaa/filename2;/aaa/bbb/ccc/ddd/filename3 -------------------------------------------------------------------... (13 Replies)
Discussion started by: hcliff
13 Replies

9. Shell Programming and Scripting

parse variable

I have a variable (it is a date actually -> 2007-01-03) which would be passed in as parameter, what I want is to parse in and put year, month, and day in separate variables, I have tried the following but doesn't work echo $dt | awk -F- '{print $1 $2 $3}' | read y m d Thanks in... (2 Replies)
Discussion started by: mpang_
2 Replies

10. Shell Programming and Scripting

parse a string variable

Hello all, need a little help. I have an input variable such as ARGV which equals something like /use/home/name/script/test.dat I need to be able to get just the "test.dat" (i.e. the file name) at the end of the directory and the directory can be anything and any length. To put it another... (3 Replies)
Discussion started by: methos
3 Replies
Login or Register to Ask a Question