parsing lines with quoted strings as single fields 
Author Message
 parsing lines with quoted strings as single fields

My input file has lines similar to this:

keyword1 keyword2 value2 keyword3 "value for keyword 3" keyword4

I need to be able to treat the values in double quotes as a single
field, while continuing to treat the other values as distinct fields.

Has anyone done this?

TIA



Wed, 03 Nov 2004 00:55:04 GMT  
 parsing lines with quoted strings as single fields

Quote:
>My input file has lines similar to this:

>keyword1 keyword2 value2 keyword3 "value for keyword 3" keyword4

>I need to be able to treat the values in double quotes as a single
>field, while continuing to treat the other values as distinct fields.

>Has anyone done this?

It can't be done with FS for the same reasons I've given in a thread
earlier today on parsing CSV files;


Newsgroups: comp.lang.awk
Subject: How to handle comma in the fields winthin a csv file using awk
Date: 17 May 2002 06:39:12 -0700

You have to split the fields up for yourself, this would get
them out of $0 into an array ;

for(sub(/ */,"");match($0,/[^"][^ ]*|"[^"]*"/);sub(/ */,""))
  {
  fld[++f]=substr($0,RSTART,RLENGTH)
  $0=substr($0,RSTART+RLENGTH,9999)
  }

Quote:

>TIA

byefrom

--

                # if you are bored crack my sig.
1F8B0808CABB793C0000666667002D8E410E83300C04EF91F2877D00CA138A7A
EAA98F30C494480157B623C4EF1B508FDED1CEFA9152A23DE35D661593C5318E
630C313CD701BE92E390563326EE17A3CA818F5266E4C2461547F1F5267659CA
8EE2092F76C329ED02CA430C5373CC62FF94BAC6210B36D9F9BC4AB53378D978
80F2978A1A6E5D6F5133B67B6113178DC1059526698AFE5C17A5187E7D930492



Wed, 03 Nov 2004 02:11:01 GMT  
 parsing lines with quoted strings as single fields

Quote:

> My input file has lines similar to this:

> keyword1 keyword2 value2 keyword3 "value for keyword 3" keyword4

> I need to be able to treat the values in double quotes as a single
> field, while continuing to treat the other values as distinct fields.

> Has anyone done this?

> TIA

I had a related problem when writing hat (html assembler
tool). You'll find it at http://www.kortext.no/awk.htm
The program creates one html document from several
input files, also in html format. I had to avoid printing
body tags  which might or might not span several lines
in the source files.  Basically I printed till the "<body"
was matched. Then I stopped printing and set the var
done="0" as long as ">" was not found. When ">" was
matched, I set done="1" and started printing again.

If you want to treat the values in double quotes as a
single field, even if they are on different lines, I'm
probably lost. You might be able to emulate what
you want by using an array.

Regards,
Jon-Egil Korsvold



Wed, 03 Nov 2004 02:25:02 GMT  
 parsing lines with quoted strings as single fields

Quote:

> My input file has lines similar to this:

> keyword1 keyword2 value2 keyword3 "value for keyword 3" keyword4

> I need to be able to treat the values in double quotes as a single
> field, while continuing to treat the other values as distinct fields.

> Has anyone done this?

> TIA

{

# AAA BBB "CCC DDD" EEE FFF

  delete F
  Fnr = 0

  Xnr = split($0, X, "\"")

  for (x=1; x<=Xnr; x++) {
    if (x%2 == 1) {
      Ynr = split (X[x], Y)
      for (y=1; y<=Ynr; y++) {
        F[++Fnr] = Y[y]
      }
    }
    else {
      F[++Fnr] = X[x]
    }
  }

  for (f=1; f<=Fnr; f++) {
    print f, F[f]
  }

Quote:
}



Sun, 07 Nov 2004 21:54:58 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. single-quoted string conversion to triple-quoted string

2. Parsing lines with quoted strings

3. XML parsing and ReportLab combination cause extra single quote in PDF

4. inserting single quotes inside a smallTalk string

5. using gsub on a string containing single quotes

6. Repost:Single quotes in string

7. Help parsing people's names from single field in credit card form

8. Single quotes in string

9. Making strings with single/double quotes safe for MySQL Insert/Update

10. Putting single quotes inside Prolog strings

11. replace single quote to double quote with an example

12. Parsing a CSV file containing quoted strings (themselves containing commas)

 

 
Powered by phpBB® Forum Software