![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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 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. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|