From: Tony Austin <taustin () solucient ! com>
To: tru64-unix-managers
Subject: How many commas
Date: Tue, 11 Sep 2001 17:33:07 +0000 [thread overview]
Message-ID: <102653680321915.tru64-unix-managers.marc@archive.invalid> (raw)
[-- 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> </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> </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> </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> </DIV>
<DIV><FONT size=2><SPAN class=173402817-11092001>hospital_id number, R0 varchar,
R5 varchar, etc. </SPAN></FONT></DIV>
<DIV><FONT size=2><SPAN class=173402817-11092001></SPAN></FONT> </DIV>
<DIV><FONT size=2><SPAN class=173402817-11092001>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:</SPAN></FONT></DIV>
<DIV><FONT size=2><SPAN class=173402817-11092001></SPAN></FONT> </DIV>
<DIV><SPAN class=173402817-11092001><FONT color=#0000ff> </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> </DIV>
<DIV><FONT color=#000000 size=2>> <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> </DIV>
<DIV><FONT size=2><FONT color=#000000><SPAN
class=173402817-11092001>Thanks. 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--
reply other threads:[~2001-09-11 17:33 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox