Need help with File parsing

Need help with File parsing

Post by Steve Ralst » Fri, 17 Feb 1995 07:13:47



Hi all.  I need some help with parsing a text file.  A sample file
might look like:

XXX1   YY1 ZZ1
AAA1   BB1 CC1

XXX2   YY2 ZZ2
AAA2   BB2 CC2

I need a way to print the contents of the AAA variable if XXX equals
some known value.  I am using /bin/sh to write my script.
Do I need to use awk or a for loop in /bin/sh or what?

Thanks in advance for all your help!

---

                     __\/__
                  .  / ^  _ \  .
                  |\| (o)(o) |/|
#----------------.OOOo--oo--oOOO.-----------#
#       Steve Ralston                       #

#_________________________Oooo._____________#
                  .oooO   (   )
                  (   )    ) /
                   \ (    (_/
                    \_)

 
 
 

Need help with File parsing

Post by zh.. » Fri, 17 Feb 1995 21:05:18


|> Hi all.  I need some help with parsing a text file.  A sample file
|> might look like:
|>
|> XXX1   YY1 ZZ1
|> AAA1   BB1 CC1
|>
|> XXX2   YY2 ZZ2
|> AAA2   BB2 CC2
|>
|> I need a way to print the contents of the AAA variable if XXX equals
|> some known value.  I am using /bin/sh to write my script.
|> Do I need to use awk or a for loop in /bin/sh or what?

Try the following statement:

1.      cat textfile | awk '$1 == "XXX1" {getline; print $1}'

or more general:

2.      cat textfile | awk '/^XXX*/ {getline; print $1}'

Steven

---------------------------------------------------------------------
Have whatever kind of day you want to have
---------------------------------------------------------------------

 
 
 

Need help with File parsing

Post by Randal L. Schwar » Tue, 21 Feb 1995 16:54:42


zhbwy> 1.    cat textfile | awk '$1 == "XXX1" {getline; print $1}'

zhbwy> or more general:

zhbwy> 2.    cat textfile | awk '/^XXX*/ {getline; print $1}'

Or, without the useless use of cat:

        awk '/^XXX*/ {getline; print $1}' textfile

Just another Unix hacker,
--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying

Web: <A HREF="http://www.teleport.com/~merlyn/">My Home Page!</A>

 
 
 

Need help with File parsing

Post by stephe » Sat, 25 Feb 1995 03:50:43



: |> Hi all.  I need some help with parsing a text file.  A sample file
: |> might look like:
: |> XXX1   YY1 ZZ1
: |> AAA1   BB1 CC1
: |>
: |> XXX2   YY2 ZZ2
: |> AAA2   BB2 CC2
: |>
: |> I need a way to print the contents of the AAA variable if XXX equals
: |> some known value.  I am using /bin/sh to write my script.
: Try the following statement:
: 1.    cat textfile | awk '$1 == "XXX1" {getline; print $1}'
: or more general:
: 2.    cat textfile | awk '/^XXX*/ {getline; print $1}'

You trying for the useless ``cat' award?

--

####################################################

# Each set includes a turntable, nine inch icing   #
# bag, six high definition nozzles...              #
####################################################

 
 
 

Need help with File parsing

Post by Hrvoje Niks » Sat, 25 Feb 1995 21:24:36




> Try the following statement:
> 1. cat textfile | awk '$1 == "XXX1" {getline; print $1}'
> or more general:
> 2. cat textfile | awk '/^XXX*/ {getline; print $1}'

Of course, it should be:
awk '$1 == "XXX1" {getline; print $1}' textfile
and
awk '/^XXX*/ {getline; print $1}' textfile

Hack, it's not original, but Randal hasn't all the time in the world :)

Hrv
--
Hrvoje Niksic                |

http://www.srce.hr/~hniksic/ |  University of Zagreb, Croatia
-------------------------------------------------------------------

Quote:>Is there any virus protections software for UNIX systems??

Yes.  It is called UNIX.
 
 
 

Need help with File parsing

Post by Dan Newcom » Tue, 21 Feb 1995 23:25:43




>zhbwy> 1.       cat textfile | awk '$1 == "XXX1" {getline; print $1}'
>zhbwy> or more general:
>zhbwy> 2.       cat textfile | awk '/^XXX*/ {getline; print $1}'
>Or, without the useless use of cat:
>        awk '/^XXX*/ {getline; print $1}' textfile

True, but do you always know that the input is going to come from a file and
not from several programs through a pipe?

Besides, I think in the man page for cat there is a clause which requires it
to be used at least once a day or else you have a "utility discrimination"
lawsuit on your hands.  :)

        -Dan

--

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
"And the man in the mirror has sad eyes."       -Marillion

 
 
 

1. Need help parsing a file in sh

I need to read a file line-by-line in Bourne shell, parsing the lines as I
read, and I need to remember how many lines I read before EOF.  The code that
I tried looks like this:

$!/bin/sh

i=1
IFS='|'
while [ $eof -ne 0 ]
do
  read a b c
  if [ $? -ne 0 ]
  then
    last=`expr $i -1`
    eof=0
  else
    echo $i)  $a $b $c
    i=`expr $i + 1`
  fi
done < file.dat
echo $last $i

Ok, so my first question is why/how does this work.  The idea to redirect
from file.dat on the done line was borrowed from a Korn shell script that
does the same thing, and it is not at all apparent why or how it works, at
least not to me.

My next question concerns an unintended effect of the redirection, which
probably would help to answer the first question.  If you run my script as
above you  will notice that when the last line is executed, nothing is
printed out for $last and that the value of i is 1, the value to which it
was initialized outside the while loop.

After some experimenting it became aparent that any variables assigned within
the while loop were not available outside the loop.  Thinking that the
redirection may have something to do with this, I changed the while condition
to just count to 10 and removed the redirection operator from the done line.
Now both $last and $i printed out the expected values.

I suspected that the shell might be forking another process to handle the
while loop with redirection, so I inserted some appropriate pauses and debugs
and watched my processes from another window.  And, as I expected, another
process was present when the script was inside the loop.

So, since another process is being forked to run the while loop, how can I
get the value of any variables assigned within the loop.  And, why does it
do this?

Thanks in advance.
--
--------------------------------------------------------------------------
Dave Humes        |    Johns Hopkins University Applied Physics Laboratory

---------------------------------------------------------------------------

2. fuser on Solaris 2.3

3. help with parsing files needed

4. Appsfilter port

5. Need Help :: GCC parse error in include header files.

6. want to know x86 price

7. HELP - parsing text file as input to script - HELP

8. UNSUSCRIBE

9. Need to parse a file

10. YACC/LEX parsing help needed ../

11. HELP! Need to parse UNIX binary on Mac

12. Need Apache help, "server-parsed" with "send-as-is".

13. Need help with string parsing from piped stdout