2 values to variables line by line (Please Help

2 values to variables line by line (Please Help

Post by Ian Molt » Fri, 01 Oct 1999 04:00:00



Say, there is this file

-------
1999021219993202
1999213519992251
1999523519992564
1999265519994562
...

---------
I would like to treat this file line by line.
I would like to get the value from column 5-6 and put it in a variable1,
get the value from column 13-14 and put it in a variable2 and
do some tests with the variables or put them in different files.
My problem is not with tests,but by getting the two values
in 2 variables , line by line.
I need also count the occurence of every value,if it happens
to be the same many times.
Please,give me some simple solutions in ksh or sh.
There isn't bash on this machine.

Thank you very much in advance for wasting your precious
time on my problem.

Ian Moltke

 
 
 

2 values to variables line by line (Please Help

Post by Barry Margoli » Fri, 01 Oct 1999 04:00:00




>Say, there is this file

>-------
>1999021219993202
>1999213519992251
>1999523519992564
>1999265519994562
>...

>---------
>I would like to treat this file line by line.
>I would like to get the value from column 5-6 and put it in a variable1,
>get the value from column 13-14 and put it in a variable2 and
>do some tests with the variables or put them in different files.
>My problem is not with tests,but by getting the two values
>in 2 variables , line by line.

while read line; do
  variable1=`echo $line | cut -c5-6`
  variable2=`echo $line | cut -c13-14`
  ...
done

Quote:>I need also count the occurence of every value,if it happens
>to be the same many times.

If I'm interpreting this correctly, you may need to use awk or perl to do
this.  They have associative arrays (aka hashes) that you can use to count
the occurrences of values.

--

GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

2 values to variables line by line (Please Help

Post by Charles Dem » Sat, 02 Oct 1999 04:00:00




>Say, there is this file

>-------
>1999021219993202
>1999213519992251
>1999523519992564
>1999265519994562
>...

>---------
>I would like to treat this file line by line.
>I would like to get the value from column 5-6 and put it in a variable1,
>get the value from column 13-14 and put it in a variable2 and
>do some tests with the variables or put them in different files.
>My problem is not with tests,but by getting the two values
>in 2 variables , line by line.
>I need also count the occurence of every value,if it happens
>to be the same many times.
>Please,give me some simple solutions in ksh or sh.
>There isn't bash on this machine.

to put those fields in separate files using awk:

awk '{print substr($0,5,2)>>"file1"; print substr($0,13,2)>>"file2"}' infile

to make a report of the occurance using awk, perhaps something like
this:

awk '{f56[substr($0,5,2)]++;f1314[substr($0,13,2)]++}
     END{print "Field(5-6) Count";
         for (i in f56 ){print i "          "  f56[i]}
         print "Field(13-14) Count";
         for (i in f1314 ){print i "            "  f1314[i]}}' infile

For this infile:

1999021219993202
1999213519992251
1999523519992564
1999265519994562
1999265519994562

the above produces this output:

Field(5-6) Count
52          1
26          2
02          1
21          1
Field(13-14) Count
22            1
32            1
25            1
45            2

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.

 
 
 

2 values to variables line by line (Please Help

Post by Jhansi Pau » Sat, 02 Oct 1999 04:00:00


In K-shell, you can try the following,

while read line
do
 var1=`echo $line|cut -c5-6 `     # I tried cut -c5-6 < $line & it does not
work
 var2=`echo $line|cut -c13-14 ` # So please do not give me "Useless use of
echo
                                                 # award"
<rest of the code >
done < filename

Hope this helps

Cheers
Jhansi


> Say, there is this file

> -------
> 1999021219993202
> 1999213519992251
> 1999523519992564
> 1999265519994562
> ...

> ---------
> I would like to treat this file line by line.
> I would like to get the value from column 5-6 and put it in a variable1,
> get the value from column 13-14 and put it in a variable2 and
> do some tests with the variables or put them in different files.
> My problem is not with tests,but by getting the two values
> in 2 variables , line by line.
> I need also count the occurence of every value,if it happens
> to be the same many times.
> Please,give me some simple solutions in ksh or sh.
> There isn't bash on this machine.

> Thank you very much in advance for wasting your precious
> time on my problem.

> Ian Moltke

 
 
 

2 values to variables line by line (Please Help

Post by Jens M. Felderhof » Sat, 02 Oct 1999 04:00:00



> Say, there is this file

> -------
> 1999021219993202
> 1999213519992251
> 1999523519992564
> 1999265519994562
> ...

> ---------
> I would like to treat this file line by line.
> I would like to get the value from column 5-6 and put it in a variable1,
> get the value from column 13-14 and put it in a variable2 and
> do some tests with the variables or put them in different files.
> My problem is not with tests,but by getting the two values
> in 2 variables , line by line.
> I need also count the occurence of every value,if it happens
> to be the same many times.

For extracting and just comparing these values, a shell script could
look like:
                while read i ;do
                                 a=`echo $i|cut -c5-6`
                                 b=`echo $i|cut -c13-14`
                                 # process $a and $b here
                 done <foo.txt

If you want to do some bookkeeping, I'd recommend a Perl script:

                #!/usr/bin/perl

                while (<>) {
                        $a = substr($_, 4, 2);
                        $b = substr($_, 12, 2);

                        $values{$a}++;
                        $values{$b}++;

                        # process values here
                }

                for $i (sort keys(%values)) {
                        print "$i: $values{$i}\n";
                }

Cheers

Jens
--
Jens M. Felderhoff



 
 
 

2 values to variables line by line (Please Help

Post by chris ulri » Sun, 03 Oct 1999 04:00:00



%%1999021219993202
%%1999213519992251
%%1999523519992564
%%1999265519994562
%%---------
%%I would like to treat this file line by line.
%%I would like to get the value from column 5-6 and put it in a variable1,
%%get the value from column 13-14 and put it in a variable2 and
%%do some tests with the variables or put them in different files.
%%My problem is not with tests,but by getting the two values
%%in 2 variables , line by line.
%%I need also count the occurence of every value,if it happens
%%to be the same many times.
%%Please,give me some simple solutions in ksh or sh.
%%There isn't bash on this machine.
%%
%%Ian Moltke

  This can be done without calling cut twice per line, but with
some icky use of the string chopping functions in posix shells.

while
  read line
do
 # To get columns 5-6
  a1="${line##????}"
  a2="${line##??????}"
  a="${a1%%$a2}"
 # and to get columns 13-14
  b1="${line##????????????}"
  b2="${line##??????????????}"
  b="${b1%%$b2}"

  echo "$a $b"
done

  I'd be surprised if this wasn't faster than using an external
cut binary (though some versions of ksh have cut builtin).  
  Keeping track of which goes where and such, while possible in
the posix shell, is even ickier.  I'll post an example of how
to do it, if anyone *really* wants to know, but I doub't there
are people so sick, even on usenet.
chris

 
 
 

1. please help how to join 3 lines into one line via script

gurus,

I have a user file   approximately 300 lines as following:

line 1:  department  e.g   Marketing and Sales department
line 2:  username     e.g. John smith
line 3:  phone number   e.g.  1-604-222-333

I need to join them into one line as following format

department : username : phone number :

Please advise how I can use shell script to join such lines.

Any help will be much appreciated.

--
*****************************************************************************
This message is intended for the sole use of the intended recipient. The
message and any files transmitted with it may contain material that is
confidential and/or legally privileged. Any review, reliance or distribution
by others or forwarding without express permission is strictly prohibited.
If you are not the intended recipient, please contact the sender and
delete all copies.
*****************************************************************************

2. Diamond Edge 3400XL

3. 3 lines to 1 line ? Help Please

4. prob with 3com905B setup

5. extract a line from multi-line variable

6. Performance while copying BIG files

7. How to search for string and assign the value on next line to a variable

8. NetApps anyone ?

9. Linux permanent Line (connection two linux machines over a leased line (permanent telephone line)

10. line by line reading of the lines in list

11. one liner which prints n lines before and m lines after the line found by grep

12. line by line without using echo "$i" | while read line?

13. Find a string, delete that line, delete 1 line before it and all lines after it...