Find the answer to your Linux question:
Results 1 to 2 of 2
Let's say I have something similar to the following in a file: Name,Description,Info,MoreInfo name1,description1,info1,info2 name2,description2,info2,info2 Where the first line is a title of what each column contains. And the file ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2010
    Posts
    1

    Awk question


    Let's say I have something similar to the following in a file:

    Name,Description,Info,MoreInfo
    name1,description1,info1,info2
    name2,description2,info2,info2

    Where the first line is a title of what each column contains. And the file could contain the columns in any order. Such as Description,Name,MoreInfo,Info. Instead of what is above.

    How can I determine which column contains a certain title?

    For example, I need to know which column is the Description column before parsing the file.

  2. #2
    Linux Enthusiast
    Join Date
    Apr 2004
    Location
    UK
    Posts
    682
    Hi there,

    This should sort you out:

    Code:
    chris@angua:~/dev/scratch$ cat test.txt 
    Name,Description,Info,MoreInfo
    name1,description1,info1,info2
    name2,description2,info2,info2
    chris@angua:~/dev/scratch$ cat pattern.awk 
    { 
      for (i=1; i <= NF; i++) {
        if ($i == "Description") { print i } 
      } 
    }
    chris@angua:~/dev/scratch$ head -1 test.txt | awk -F',' -f pattern.awk 
    2
    Let us know how you get on
    To be good, you must first be bad. "Newbie" is a rank, not a slight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •