tru64-unix-managers (formerly alpha-osf-managers) (1994-2012)
 help / color / mirror / Atom feed
* How many commas
@ 2001-09-11 17:33 Tony Austin
  0 siblings, 0 replies; only message in thread
From: Tony Austin @ 2001-09-11 17:33 UTC (permalink / raw)
  To: tru64-unix-managers

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: multipart/mixed; boundary="------_=_NextPart_001_01C13AE7.D1B59800", Size: 4246 bytes --]

This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_001_01C13AE7.D1B59800
Content-Type: text/plain;
	charset="iso-8859-1"

 
I have a text file that looks like
 
hospital_id, R0,R5,R15,R9
1010,Y,Y,Y,Y
1020,N,N,Y,N
1030,Y,N,N,Y
 
I would like to take the first line, hospital_id... and turn it into :
 
hospital_id number, R0 varchar, R5 varchar, etc.  
 
The problem is that there will be varying number of columns each time.  So
after I grep out the line with hospital in it, how can I determine how many
commas are in the line so that I can loop through it and perform something
like this:
 
 
file="test.txt"
C=`grep -i hosp_id $file`
echo $C
C1=`echo $C | cut -d',' -f1`
echo $C1
C2=`echo $C | cut -d',' -f2`
echo $C2
C3=`echo $C | cut -d',' -f3`
echo $C3
 
> test.sh
HOSP_ID,RO,RS
HOSP_ID
RO
R5
 
Thanks.  Tony
R15

------_=_NextPart_001_01C13AE7.D1B59800
Content-Type: text/html;
	charset="iso-8859-1"

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">


<META content="MSHTML 5.00.2314.1000" name=GENERATOR></HEAD>
<BODY>
<DIV>&nbsp;</DIV>
<DIV><FONT size=2><SPAN class=173402817-11092001>I have a text file that looks 
like</SPAN></FONT></DIV>
<DIV><FONT size=2><SPAN class=173402817-11092001></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT size=2><SPAN class=173402817-11092001>hospital_id, 
R0,R5,R15,R9</SPAN></FONT></DIV>
<DIV><FONT size=2><SPAN 
class=173402817-11092001>1010,Y,Y,Y,Y</SPAN></FONT></DIV>
<DIV><FONT size=2><SPAN 
class=173402817-11092001>1020,N,N,Y,N</SPAN></FONT></DIV>
<DIV><FONT size=2><SPAN 
class=173402817-11092001>1030,Y,N,N,Y</SPAN></FONT></DIV>
<DIV><FONT size=2><SPAN class=173402817-11092001></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT size=2><SPAN class=173402817-11092001>I would like to take the first 
line, hospital_id... and turn it into :</SPAN></FONT></DIV>
<DIV><FONT size=2><SPAN class=173402817-11092001></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT size=2><SPAN class=173402817-11092001>hospital_id number, R0 varchar, 
R5 varchar, etc.&nbsp; </SPAN></FONT></DIV>
<DIV><FONT size=2><SPAN class=173402817-11092001></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT size=2><SPAN class=173402817-11092001>The problem is that there will 
be varying number of columns each time.&nbsp; So after I grep out the line with 
hospital in it, how can I determine how many commas are in the line so that I 
can loop through it and perform something like this:</SPAN></FONT></DIV>
<DIV><FONT size=2><SPAN class=173402817-11092001></SPAN></FONT>&nbsp;</DIV>
<DIV><SPAN class=173402817-11092001><FONT color=#0000ff>&nbsp;</DIV>
<DIV><SPAN class=173402817-11092001></SPAN><FONT color=#000000 size=2>f<SPAN 
class=173402817-11092001>ile</SPAN>="<SPAN 
class=173402817-11092001>test</SPAN>.txt"</FONT></DIV>
<DIV><FONT color=#000000 size=2>C=`grep -i hosp_id $<SPAN 
class=173402817-11092001>file</SPAN>`</FONT></DIV>
<DIV><FONT color=#000000 size=2>echo $C</FONT></DIV>
<DIV><FONT color=#000000 size=2>C1=`echo $C | cut -d',' -f1`</FONT></DIV>
<DIV><FONT color=#000000 size=2>echo $C1</FONT></DIV>
<DIV><FONT color=#000000 size=2>C2=`echo $C | cut -d',' -f2`</FONT></DIV>
<DIV><FONT color=#000000 size=2>echo $C2</FONT></DIV>
<DIV><FONT color=#000000 size=2>C3=`echo $C | cut -d',' -f3`</FONT></DIV>
<DIV><FONT color=#000000 size=2>echo $C3</FONT></DIV>
<DIV><FONT color=#000000 size=2></FONT>&nbsp;</DIV>
<DIV><FONT color=#000000 size=2>&gt;&nbsp;<SPAN 
class=173402817-11092001>test</SPAN>.sh</FONT></DIV>
<DIV><FONT color=#000000 size=2>HOSP_ID,RO,RS</FONT></DIV>
<DIV><FONT color=#000000 size=2>HOSP_ID</FONT></DIV>
<DIV><FONT color=#000000 size=2>RO</FONT></DIV>
<DIV><FONT size=2><FONT color=#000000>R<SPAN 
class=173402817-11092001>5</SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT color=#000000><SPAN 
class=173402817-11092001></SPAN></FONT></FONT>&nbsp;</DIV>
<DIV><FONT size=2><FONT color=#000000><SPAN 
class=173402817-11092001>Thanks.&nbsp; Tony</SPAN></FONT></FONT></DIV>
<DIV></FONT><FONT color=#000000 size=2><SPAN 
class=173402817-11092001>R15</SPAN></FONT></SPAN></DIV></BODY></HTML>

------_=_NextPart_001_01C13AE7.D1B59800--

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2001-09-11 17:33 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2001-09-11 17:33 How many commas Tony Austin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox