parsing problem with script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting parsing problem with script
# 1  
Old 08-03-2006
Question parsing problem with script

I'm trying to parse the variables out of a comma delimited expression, but i'm having trouble with script:

num_var=1
while [ $num_var -lt 4 ]
do
a=`echo "a=7, b=8, c=9" | awk '{print $num_var}' | cut -d= -f2`
b=`echo $a | cut -d, -f1`
echo $b
num_var=`expr $num_var + 1`
done

the output is:

7
7
7

I know the problem is in the statement print $num_var ... for each time in the loop I want that print statement to be "print $1" then "print $2" then "print $3". How would I go about doing this using my num_var expression?
# 2  
Old 08-03-2006
If i understand correctly, what u want is that three comma separated values should be printed in separate lines(in this case 7,8,9).

This should do it:
Code:
echo "7,8,9" | awk -F"," '{OFS="\n"; print $1,$2,$3}'

# 3  
Old 08-03-2006
Error follow up

but i just can't have them listed "7, 8, 9" as there are variable names listed with the values...that is why I have to do the second cut. Is there any way to implement with a correction to the way I have it????
# 4  
Old 08-03-2006
You can use:

Code:
  echo "a=7,b=8,c=9" | awk -F"[=,]" '{OFS="\n"; print $2,$4,$6}' 

or
If you have input like:

a=7
b=8
c=9


Then use :
Code:
  echo "$a,$b,$c" | awk -F"," '{OFS="\n"; print $1,$2,$3}' 

Also, you can't directly access shell variables in awk. To do what you are trying using your code :


num_var=1
while [ $num_var -lt 4 ]
do
a=`echo "a=7, b=8, c=9" | awk -v field=$num_var '{print $field}' | cut -d= -f2`
b=`echo $a | cut -d, -f1`
echo $b
num_var=`expr $num_var + 1`
done

Last edited by vish_indian; 08-03-2006 at 09:15 AM..
# 5  
Old 08-03-2006
This might help you

echo "a=7,b=8,c=9" | awk -F"," '{ for ( i = 1; i <= NF; i++) { print substr($i,3,1) ; } } '
# 6  
Old 08-03-2006
Bug thanks

thanks for the help you guys, it worked fine.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk parsing problem

Hello fellow unix geeks, I am having a small dilemna trying to parse a log file I have. Below is a sample of what it will look like: MY_TOKEN1(group) TOKEN(other)|SSID1 MY_TOKEN2(group, group2)|SSID2 What I need to do is only keep the MY_TOKEN pieces and where there are multiple... (7 Replies)
Discussion started by: dagamier
7 Replies

2. Shell Programming and Scripting

Problem parsing

Hi, I want to fetch a text.Clipping. ... (5 Replies)
Discussion started by: protocomm
5 Replies

3. Shell Programming and Scripting

Problem parsing args

Heya Tooltip: Parsing (getopts) for -u successfully sets mode=umnt, but case umnt is not executed, instead it either executes/show help or regular mount screen. I had copy pasted the structure of a getopts 'structure' from Man Page for getopts (posix Section 1) - The UNIX and Linux Forums... (1 Reply)
Discussion started by: sea
1 Replies

4. Shell Programming and Scripting

Parsing problem

Hello, I have a similar problem so I continue this thread. I have: my_script_to_format_nicely_bdf.sh | grep "RawData" |tr -s ' '|cut -d' ' -f 4|tr -d '%' So it supposed to return the percentage used of RawData FS: 80 (Want to use it in a alert script) However I also have a RawData2 FS so... (17 Replies)
Discussion started by: drbiloukos
17 Replies

5. Shell Programming and Scripting

Parsing Problem

Hi all, I am having problems parsing the following file: cat mylist one,two,three four five,six My goal is to get each number on a seperate line. one two three four five six I tried this command: sed -e 's/\,/^M/g' mylist (11 Replies)
Discussion started by: rob11g
11 Replies

6. Shell Programming and Scripting

Parsing problem

I need to parse a string which looks like "xyx","sdfsdf","asf_asdf" into var1="xyx" var2="sdfsdf" var3="asf_asdf" (3 Replies)
Discussion started by: Sushir03
3 Replies

7. Shell Programming and Scripting

Parsing problem

Hi, i need to parse a string which looks like this "xyz","1233","cm_asdfasdf" (2 Replies)
Discussion started by: Sushir03
2 Replies

8. Shell Programming and Scripting

Parsing problem

I need to separate out the contents in the string "xyz","1233","cm_asdfasdf" as xyz,1233,cm_asdfasdf Can anyone help me on this?? (1 Reply)
Discussion started by: Sushir03
1 Replies

9. Shell Programming and Scripting

awk parsing problem

I need help with a problem that I have not been able to figure out. I have a file that is about 650K lines. Records are seperated by blank lines, fields seperated by new lines. I was trying to make a report that would add up 2 fields and associate them with a CP. example output would be... (11 Replies)
Discussion started by: timj123
11 Replies

10. Shell Programming and Scripting

Problem parsing line in file

I am VERY new to unix scripting. I am having trouble parsing a line into fields for further processing. I have this script: #bin/sh cat ztest2.txt | while read line do zvar1=`echo $line | cut -f6` echo "zvar1 is " $zvar1 done ******************** ztest2.txt looks like: 1 ... (2 Replies)
Discussion started by: rlwilli
2 Replies
Login or Register to Ask a Question