Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

some shell script babies!! -- anyone knoe the BEST way to read lines..

Status
Not open for further replies.

mckarl

Programmer
May 5, 1999
147
GB
some shell script babies!! -- anyone knoe the BEST way to read lines from a file into either variables or directly into files?<br><br>Basically, i'm creating an output from a file, <br>(eg; karlssuperduperwotsitfile.fle) and placing it into a format. <br>Ready? -- ok, let me explain a little further...<br><br>File:karlssuperduperwotsitfile.fle<br>data:202 COPY MADSTUPH<br>&nbsp;&nbsp;&nbsp;&nbsp;: 8-S*************<br>&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;Unknown COPY file specified<br>&nbsp;&nbsp;&nbsp;&nbsp;:213 01 FILLER&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PIC X(32).<br>&nbsp;&nbsp;&nbsp;&nbsp;:217-S**<br>&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;Preceeding item at this level has zero length<br>&nbsp;&nbsp;&nbsp;&nbsp;: .....................................<br>&nbsp;&nbsp;&nbsp;&nbsp;:<br><br>So, as you can see (yeah, stop laughing, it IS cobol) i need to be able to read in the files as line input, therefor, i can read $1 for line number, and the rest for <br>the error quoted. For line two, the error code (8-S) and the stars can be omitted. and for line three, the output from the compiler of the error in words. Then, it's all just repeated.<br><br>So, i have looked it in these two methods mainly...<br><br><u>Method 1:</u>&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;line_num=1<br>&nbsp;&nbsp;&nbsp;&nbsp;for line in `cat karlssuperduperwotsitfile.fle`<br>&nbsp;&nbsp;&nbsp;&nbsp;do <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case line_num in<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1) line_err=$1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line_num=2<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2) line_code=$2<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line_num=3<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3) line_explination=$*<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line_num=1<br>&nbsp;&nbsp;&nbsp;&nbsp;done<br>&nbsp;&nbsp;&nbsp;&nbsp;<i><b>... And the story goes on</b><br>&nbsp;&nbsp;&nbsp;&nbsp;Is line a reserved shell-script word i can use for lines? I have done it before, but cant remember how!</i><br><br>Anyhow, it reads variabe in, <br><br>Another way would be:<br><br>using the wc -l and read commands -- cant remember the syntax off hand, but i think you should get the picture... <br><br>ok, i want to be able to read in LINES AT A TIME, rather than variable wannabees (words) at a time.<br><br>Any help will be greately recieved!!!<br><br>Karl.<br><br><img src= <br><br><i>P.S. this has got to be the biggest post i ever wrote!!</i><br><br> <p> Karl<br><a href=mailto:mc_karl@yahoo.com>mc_karl@yahoo.com</a><br><a href= > </a><br> ~ ~ ~ ~<br>
K A R L<br>
~ ~ ~ ~
 
Karl,<br><br>Have you got Perl? It's easy in Perl.... <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
yeah, i have perl :)) and rexx... <p> Karl<br><a href=mailto:mc_karl@yahoo.com>mc_karl@yahoo.com</a><br><a href= > </a><br> ~ ~ ~ ~<br>
K A R L<br>
~ ~ ~ ~
 
Is it possible to pass parameters from perl to a shell script somehow?? <p> Karl<br><a href=mailto:mc_karl@yahoo.com>mc_karl@yahoo.com</a><br><a href= > </a><br> ~ ~ ~ ~<br>
K A R L<br>
~ ~ ~ ~
 
Can you use AWK?<br><br>Its simple:<br><FONT FACE=monospace><br>#!/bin/awk<br>(NR%3)==1 { print &quot;Error at line &quot; $1 }<br>(NR%3)==2 { print &quot;Error code is &quot; $1 }<br>(NR%3)==3 { print &quot;Error description: &quot; $0 }<br></font><br><br>Save it like cobolerr and chmodit executable, then you can do<br><FONT FACE=monospace>cobolerr file.cob</font> and it will processit<br><br>I hope it works...
 
awk is fine - just I prefer Perl &lt;smile&gt; <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
i'm tying this method now, but looking at it ... <br><br>i will read in a file of them, what does the NR%3 do?? -- and how could i compensate this method to allow input of one file, carrying multiple records of text?<br>also, i'm encoorperating(is that such a word?)the awk into my shell script, so is it <br><br>awk begin<br>(NR%3)==1 {PRINT &quot;<br>...<br>...<br>AWK END<br><br>is that right?.. i'v never really used awk b4..<br><br>thanks in advance<br>Karl. <p> Karl<br><a href=mailto:mc_karl@yahoo.com>mc_karl@yahoo.com</a><br><a href= > </a><br> ~ ~ ~ ~<br>
K A R L<br>
~ ~ ~ ~
 
Script in single quotes after the awk command, it can span more than one line. Like this:<br><br>awk '<br>&nbsp;&nbsp;&nbsp;&nbsp;(NR%3)==1 {print}<br>&nbsp;&nbsp;&nbsp;&nbsp;.<br>&nbsp;&nbsp;&nbsp;&nbsp;.<br>&nbsp;&nbsp;&nbsp;&nbsp;.<br>'<br>Don't like it though, <b>awk</b>ward is right.... <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
In Perl, with a file like this.<br><br>202 COPY MADSTUPH<br>8-S*************<br>Unknown COPY file specified<br>213 01 FILLER&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PIC X(32).<br>217-S**<br>Preceeding item at this level has zero length<br><br>first line is the path to the perl executable<br><FONT FACE=monospace><b><br>#!/usr/bin/perl<br><br>while(&lt;&gt;){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' process stdin and/or command line files<br>&nbsp;&nbsp;&nbsp;&nbsp;if (/^\d\d\d/){&nbsp;&nbsp;&nbsp;&nbsp;' if the line starts with three digits<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' print it and...<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$_=&lt;&gt;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' get the next line and...<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' print that as well<br>}<br></font></b><br><br>There. &lt;smile&gt; *much* nicer. <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
1. I guess AWK is much more understand<i>able</i> than that Perl example.<br>2. <FONT FACE=monospace>NR</font> means <b>record number</b>, say, the line<br>3. <FONT FACE=monospace>NR % 3</font> means <b>NR modulus 3</b><br>4. <FONT FACE=monospace>(NR % 3)== 1 { print }</font> means if the record number modulus three equals one
 
&quot;AWK is much more understandable&quot; -- think we're straying into which language is better/religeous issues here, as in:<br><br>Question: &quot;How many pins can you stick in the head of a Perl programmer&quot;<br><br>Answer: Lots. Perl programmers have very big heads with plenty of surface area for pins and the like.<br> <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Good point, but for <b>that</b> particular problems, and pair of solutions I guess mckarl can see clearer the point with my AWKward idea!.
 
waw, it's rumble time! :)<br><br>thanks all!!!<br><br>c-u l8a!<br><br>Karl. <p>Karl Butterworth<br><a href=mailto:karl_butterworth@ordina.co.uk>karl_butterworth@ordina.co.uk</a><br><a href= > </a><br><i>I'm slim shadey, yes i'm the real shadey, all you other slim shadeys are just imitating; so wont the real slim shadey please stand up, please stand up, please stand up!</i>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top