Simple awk problem - Unix

This is a discussion on Simple awk problem - Unix ; I'm useless with awk and sed and I really want to go home, I have a comma seperated list that reads like this random text here 1,2,3,"sample","sample2","sa3","4","5","6","7","8" 1,2,3,"sample","sample2","sa3","4","5","6","7","8" 1,2,3,"sample","sample2","sa3","4","5","6","7","8" 1,2,3,"sample","sample2","sa3","4","5","6","7","8" 1,2,3,"sample","sample2","sa3","4","5","6","7","8" 1,2,3,"sample","sample2","sa3","4","5","6","7","8" end of file As you'll notice the first ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: Simple awk problem

  1. Simple awk problem

    I'm useless with awk and sed and I really want to go home,
    I have a comma seperated list that reads like this

    random
    text
    here
    1,2,3,"sample","sample2","sa3","4","5","6","7","8"
    1,2,3,"sample","sample2","sa3","4","5","6","7","8"
    1,2,3,"sample","sample2","sa3","4","5","6","7","8"
    1,2,3,"sample","sample2","sa3","4","5","6","7","8"
    1,2,3,"sample","sample2","sa3","4","5","6","7","8"
    1,2,3,"sample","sample2","sa3","4","5","6","7","8"
    end of file

    As you'll notice the first three fields have no quotes but they need
    them. I've tried various things with awk, the one I though would work was

    awk '{print \"$1\" \"$2\" \"$3\"$4}' input_file >>output_file

    awk is complaining and gawk tells me that backslash is not last
    character on line

    The whole point is to get this ...

    "1","2","3","sample","sample2","sa3","4","5","6","7","8"
    "1","2","3","sample","sample2","sa3","4","5","6","7","8"
    "1","2","3","sample","sample2","sa3","4","5","6","7","8"
    "1","2","3","sample","sample2","sa3","4","5","6","7","8"
    "1","2","3","sample","sample2","sa3","4","5","6","7","8"
    "1","2","3","sample","sample2","sa3","4","5","6","7","8"
    end of file

    obviously the data is not the same on each line but there are 8 fields
    on each line.

    I know it should be simple but this is the sort of thing that gives me
    nightmares.
    This weekend I'll be teaching myself about regex's awk and sed but as
    usual ... it a rush job.
    Many thanks for any help

  2. Re: Simple awk problem

    Jimi Gixxer wrote:
    > I'm useless with awk and sed and I really want to go home,
    > I have a comma seperated list that reads like this
    >
    > random
    > text
    > here
    > 1,2,3,"sample","sample2","sa3","4","5","6","7","8"
    > 1,2,3,"sample","sample2","sa3","4","5","6","7","8"
    > 1,2,3,"sample","sample2","sa3","4","5","6","7","8"
    > 1,2,3,"sample","sample2","sa3","4","5","6","7","8"
    > 1,2,3,"sample","sample2","sa3","4","5","6","7","8"
    > 1,2,3,"sample","sample2","sa3","4","5","6","7","8"
    > end of file
    >
    > As you'll notice the first three fields have no quotes but they need
    > them. I've tried various things with awk, the one I though would work was
    >
    > awk '{print \"$1\" \"$2\" \"$3\"$4}' input_file >>output_file
    >
    > awk is complaining and gawk tells me that backslash is not last
    > character on line


    You should be using "\"" instead of just \".

    > The whole point is to get this ...
    >
    > "1","2","3","sample","sample2","sa3","4","5","6","7","8"
    > "1","2","3","sample","sample2","sa3","4","5","6","7","8"
    > "1","2","3","sample","sample2","sa3","4","5","6","7","8"
    > "1","2","3","sample","sample2","sa3","4","5","6","7","8"
    > "1","2","3","sample","sample2","sa3","4","5","6","7","8"
    > "1","2","3","sample","sample2","sa3","4","5","6","7","8"
    > end of file
    >
    > obviously the data is not the same on each line but there are 8 fields
    > on each line.
    >
    > I know it should be simple but this is the sort of thing that gives me
    > nightmares.
    > This weekend I'll be teaching myself about regex's awk and sed but as
    > usual ... it a rush job.
    > Many thanks for any help


    Using GNU awk:

    awk -F'"?,"?' -vOFS='","' '$1="\""$1' file

    Ed.

  3. Re: Simple awk problem

    Ed Morton wrote:
    > Jimi Gixxer wrote:
    >> I'm useless with awk and sed and I really want to go home,
    >> I have a comma seperated list that reads like this
    >>
    >> random
    >> text
    >> here
    >> 1,2,3,"sample","sample2","sa3","4","5","6","7","8"
    >> 1,2,3,"sample","sample2","sa3","4","5","6","7","8"
    >> 1,2,3,"sample","sample2","sa3","4","5","6","7","8"
    >> 1,2,3,"sample","sample2","sa3","4","5","6","7","8"
    >> 1,2,3,"sample","sample2","sa3","4","5","6","7","8"
    >> 1,2,3,"sample","sample2","sa3","4","5","6","7","8"
    >> end of file
    >>
    >> As you'll notice the first three fields have no quotes but they need
    >> them. I've tried various things with awk, the one I though would work was
    >>
    >> awk '{print \"$1\" \"$2\" \"$3\"$4}' input_file >>output_file
    >>
    >> awk is complaining and gawk tells me that backslash is not last
    >> character on line

    >
    > You should be using "\"" instead of just \".
    >
    >> The whole point is to get this ...
    >>
    >> "1","2","3","sample","sample2","sa3","4","5","6","7","8"
    >> "1","2","3","sample","sample2","sa3","4","5","6","7","8"
    >> "1","2","3","sample","sample2","sa3","4","5","6","7","8"
    >> "1","2","3","sample","sample2","sa3","4","5","6","7","8"
    >> "1","2","3","sample","sample2","sa3","4","5","6","7","8"
    >> "1","2","3","sample","sample2","sa3","4","5","6","7","8"
    >> end of file
    >>
    >> obviously the data is not the same on each line but there are 8 fields
    >> on each line.
    >>
    >> I know it should be simple but this is the sort of thing that gives me
    >> nightmares.
    >> This weekend I'll be teaching myself about regex's awk and sed but as
    >> usual ... it a rush job.
    >> Many thanks for any help

    >
    > Using GNU awk:
    >
    > awk -F'"?,"?' -vOFS='","' '$1="\""$1' file
    >
    > Ed.


    Ed, you're a star ! Thats running perfectly.
    This weekend I'll make it my homework to figure out exactly what that
    line is doing.
    Many thanks, I'll sleep well tonight.

  4. Re: Simple awk problem

    Ed Morton wrote:
    > Jimi Gixxer wrote:
    >> I'm useless with awk and sed and I really want to go home,
    >> I have a comma seperated list that reads like this
    >>
    >> random
    >> text
    >> here
    >> 1,2,3,"sample","sample2","sa3","4","5","6","7","8"
    >> 1,2,3,"sample","sample2","sa3","4","5","6","7","8"
    >> 1,2,3,"sample","sample2","sa3","4","5","6","7","8"
    >> 1,2,3,"sample","sample2","sa3","4","5","6","7","8"
    >> 1,2,3,"sample","sample2","sa3","4","5","6","7","8"
    >> 1,2,3,"sample","sample2","sa3","4","5","6","7","8"
    >> end of file
    >>
    >> As you'll notice the first three fields have no quotes but they need
    >> them. I've tried various things with awk, the one I though would work was
    >>
    >> awk '{print \"$1\" \"$2\" \"$3\"$4}' input_file >>output_file
    >>
    >> awk is complaining and gawk tells me that backslash is not last
    >> character on line

    >
    > You should be using "\"" instead of just \".
    >
    >> The whole point is to get this ...
    >>
    >> "1","2","3","sample","sample2","sa3","4","5","6","7","8"
    >> "1","2","3","sample","sample2","sa3","4","5","6","7","8"
    >> "1","2","3","sample","sample2","sa3","4","5","6","7","8"
    >> "1","2","3","sample","sample2","sa3","4","5","6","7","8"
    >> "1","2","3","sample","sample2","sa3","4","5","6","7","8"
    >> "1","2","3","sample","sample2","sa3","4","5","6","7","8"
    >> end of file
    >>
    >> obviously the data is not the same on each line but there are 8 fields
    >> on each line.
    >>
    >> I know it should be simple but this is the sort of thing that gives me
    >> nightmares.
    >> This weekend I'll be teaching myself about regex's awk and sed but as
    >> usual ... it a rush job.
    >> Many thanks for any help

    >
    > Using GNU awk:
    >
    > awk -F'"?,"?' -vOFS='","' '$1="\""$1' file
    >
    > Ed.

    Aha I see it now !
    I need a LOT more practice with this
    Many thanks again Ed

+ Reply to Thread