parsing variable contents with comma delimter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting parsing variable contents with comma delimter
# 1  
Old 09-22-2008
parsing variable contents with comma delimter

hi, unix gurus.

i am trying to read a file that has records in this format...

x<group_id>:::<member1a>,<member1b>,<member1c>
x<group_id>:::<member2a>,<member2b>,<member2c>


... and to put it in this format:

<member1a>
<member1b>
<member1c>
<member2a>
<member2b>
<member2c>


with the code i have now, i am able to extract these records from the 4th field and direct them into a file. for example, this command:

Code:
my_script group_id group_id

gives me a file that looks like this:

<member1a>,<member1b>,<member1c>
<member2a>,<member2b>,<member2c>


here's my code:


Code:
# Validate Groups
for group_id; do
  a=$(grep "^x$group_id:" group_file)
  if [ -z "$a" ]; then
    print "value not in group_file"
  continue
  fi

# Get Group Members and Direct Into File
  f4=$(echo $a | cut -d: -f4)
  if [ -z "$f4" ]; then
    print "This group_id does not have any members."
  else
    print $f4, >> member_list.txt
  continue
  fi
done

is it wiser for me to parse $f4, and using a loop, direct each member into a file? or should i cut my generated file, member_list.txt, and pipe that into a new file?

it seems like it would be cleaner to parse $f4. if i am able to do so, i wonder if anyone has any tips on the syntax?

thanks for your help!
# 2  
Old 09-22-2008
Perhaps:

Code:
    print $f4 | tr ',' '\n' >> member_list.txt

# 3  
Old 09-22-2008
that is sweet! thanks a lot.
# 4  
Old 09-22-2008
You can also use sed
Code:
$ sed -e 's/^.*::://;s/[,$]/\
/g' file
<member1a>
<member1b>
<member1c>
<member2a>
<member2b>
<member2c>
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing Comma Separated values to UNIX variable from PLSQL

Hi All, I'm trying to pass the comma separated values (string) returned from Plsql Procedure to UNIX variable. getting the below log message cat: -: Bad file descriptor awk: cmd. line:1: fatal: error reading input file `-': Bad file descriptor The output coming from plsql procedure is... (3 Replies)
Discussion started by: Mahesh3089
3 Replies

2. Shell Programming and Scripting

Needs help in parsing comma separated values

hello experts, i am retrieving values in variables jobKey and jobName within my shell script. these values are returned to me within braces and i am using following command to remove those braces: jobKeys=`echo $jobKeys | sed 's:^.\(.*\).$:\1:'` jobNames=`echo $jobNames | sed... (1 Reply)
Discussion started by: avikaljain
1 Replies

3. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

4. Shell Programming and Scripting

Parsing and filtering multiline text into comma separated line

I have a log file that contains several reports with following format. <Start of delimiter> Report1 header Report1 header continue Report1 header continue Record1 header Record1 header continue Record1 header continue field1 field2 field3 field4 ------... (1 Reply)
Discussion started by: yoda9691
1 Replies

5. Shell Programming and Scripting

parsing comma separated file

Hi, I have a file with th elist of patches separated by comma, like below: patch1, patch 2, patch 3................ t\The number of patches is not known as it changes every time. I need assistance in writing a routine such as it will take patch1 as first variable and performs the... (4 Replies)
Discussion started by: avikaljain
4 Replies

6. Shell Programming and Scripting

Parsing Comma separated Arguments

Hi unix guru's I want to execute a shell script like ksh printdetails.ksh Andy,Bob,Daisy,Johnson like passing all the four names in the as the arguments and these arguments are varies between 1 to 10. How to pass these names to the shell script variable. and also i want to know the count... (4 Replies)
Discussion started by: Reddy482
4 Replies

7. Shell Programming and Scripting

Unix shell script to parse the contents of comma-separated file

Dear All, I have a comma-separated file. 1. The first line of the file(header) should have 4 commas(5 fields). 2. The last line of the file should have 1 comma(2 fields). Pls help me in checking this condition in a shell script. And the number of lines between the first line and last... (11 Replies)
Discussion started by: KrishnaSaran
11 Replies

8. Shell Programming and Scripting

Need help in parsing text file contents

Hi, I need some help in extracting the Exception block between the lines 21 Feb 01:18:54:146 ERROR com.orbits.frameworks.integrationframework.ValidationException - Caught exception in validateRequest() (PID=565584) and 21 Feb 01:18:55:149 INFO ... (0 Replies)
Discussion started by: Alecs
0 Replies

9. Shell Programming and Scripting

Parsing comma delimited text file

I need to delete a set of files in certain directories if there're older than a certain number of days. So I have a text file, with each line containing the directory & number of days. The format is like this: dirA,5 dirB,7 How do I write script to iteratively parse this text file & delete... (5 Replies)
Discussion started by: chengwei
5 Replies

10. Shell Programming and Scripting

File contents parsing

Suppose I have a file which has either the contents CALLED on <date & time> RUNNING on <date & time>or CALLED on <date & time> RUNNING on <date & time> SHUTTING_DOWN on <date & time> DOWN on <date & time> I don't care about the <date & time> part... I just need to know if the last line in... (3 Replies)
Discussion started by: orno
3 Replies
Login or Register to Ask a Question