Quick question on parsing a string!

Quick question on parsing a string!

Post by Scott Trowbridg » Fri, 09 Nov 2001 04:23:23



Hi

Fast question.

    I've got flat text with fields that are separated by '|'.

    QUESTION:

    How can I read them in and assign them variable names to work with using
the '|' as a field separator?

--
Scott Trowbridge,

--
Scott Trowbridge, VP

 
 
 

Quick question on parsing a string!

Post by John Gord » Fri, 09 Nov 2001 04:26:52



> I've got flat text with fields that are separated by '|'.
> How can I read them in and assign them variable names to work with using
> the '|' as a field separator?

awk sounds like the perfect candidate.

---
"... What with you being his parents and all, I think that you could
be trusted not to shaft him."  -- Robert Chang, rec.games.board



 
 
 

Quick question on parsing a string!

Post by Chris F.A. Johnso » Fri, 09 Nov 2001 04:37:34



> Hi

> Fast question.

>     I've got flat text with fields that are separated by '|'.

>     QUESTION:

>     How can I read them in and assign them variable names to work with using
> the '|' as a field separator?

IFS="|"
while read field1 field2 field3 field4 [....]
do
   ## do what you like with them
done

or:

awk -F '|' '{ ## do whatever
              print $1
              print $2 ## ertc...
            }'

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2001, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

Quick question on parsing a string!

Post by Lekeas G » Fri, 09 Nov 2001 05:36:59


Hi,

You can use the IFS variable (IFS="|").

George


> Date: Wed, 7 Nov 2001 14:23:23 -0500

> Newsgroups: comp.unix.programmer, comp.unix.shell
> Subject: Quick question on parsing a string!

> Hi

> Fast question.

>     I've got flat text with fields that are separated by '|'.

>     QUESTION:

>     How can I read them in and assign them variable names to work with using
> the '|' as a field separator?

> --
> Scott Trowbridge,

> --
> Scott Trowbridge, VP

George
 
 
 

Quick question on parsing a string!

Post by Ben.Altma » Fri, 09 Nov 2001 06:44:49



> There was another answer that was close but not right on the money. This
> is what you want, assuming you have 4 fields:

> while IFS='|' read var1 var2 var3 var4; do
>     # Something with $var1 ... $var4
> done < inputfile.txt

The other answer is more portable as I think this will only work with ksh.
 
 
 

Quick question on parsing a string!

Post by stanislav shaluno » Fri, 09 Nov 2001 11:11:57



>     I've got flat text with fields that are separated by '|'.
>     How can I read them in and assign them variable names to work with using
> the '|' as a field separator?

comp.unix.programmer answer: strsep(3) function man page has sample
code for performing a similar task.

comp.unix.shell answer: awk -F'|' should do it.

--
Stanislav Shalunov              http://www.internet2.edu/~shalunov/

A fanatic is one who can't change his mind and won't change the
subject.                                   -- Winston Churchill

 
 
 

1. C Question -- ANSI C -- *** Please help me Parsing an IP String ****

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main()
{
   char *ip;
   char *loc, bit1[5], bit2[5], bit3[5], bit4[5];
   char tmp1[13];
   int ch = '.';

   ip = malloc(15);
   strcpy(ip, "172.30.43.103");

The code above is just an example of how the IP address will look.
All I want to do is fill in each bit variable with each number.
Example as follows:

bit1=172
bit2=30
bit3=43
bit4=103  or

bit1=172
bit2=20
bit3=125
bit4=10

After I parse this I need to add zeros on all numbers less then 3
digits.  

0 would be 000 and 10 would be 010 or 23 would be 023.  

ultimately I would like to have each variable filled like shown below.

bit1=205
bit2=023
bit3=120
bit4=002

So the IP would be 205.023.120.002 instead of 205.23.120.2.   Then I
have a file filled with IP address and I want to compare the bits to
the IP's in the file.  The file is the easy part but parsing the users
IP is a bit more difficult.

Please any help with this will be appreciated.

Thank you.

2. More Newbie Questions

3. SED question, parsing strings

4. Slackware 1.1.2 weird tex behaviour - kernel problem?

5. I have a Quick Question, need quick answer.

6. What happened to gramofile?

7. Quick Command For Parsing Filename

8. Single User Optimization?

9. string parse

10. Parsing a string with KSH

11. Parsing strings in CGI scripts

12. Parsing a bash string?

13. string parsing in sh