The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Parse String Using Sed racbern Shell Programming and Scripting 4 04-23-2008 12:14 PM
how to parse this string hcliff Shell Programming and Scripting 13 04-02-2008 04:43 AM
String parse question mnreferee Shell Programming and Scripting 5 03-07-2007 12:30 AM
parse variable mpang_ Shell Programming and Scripting 2 01-03-2007 07:31 AM
parse a string variable methos Shell Programming and Scripting 3 10-18-2005 04:18 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-07-2008
racbern racbern is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 26
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 (permalink)  
Old 04-07-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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 (permalink)  
Old 04-07-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,294
Try this one:

Code:
echo "$TA" | awk 'BEGIN{FS="\[|\]"}{split($2,s," ");print s[1],s[2]}'
Regards
  #4 (permalink)  
Old 04-07-2008
in2nix4life's Avatar
in2nix4life in2nix4life is offline
Registered User
  
 

Join Date: Oct 2007
Location: East Coast
Posts: 58
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:34 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0