Find the answer to your Linux question:
Results 1 to 5 of 5
Hi, I would like to know how can I select lines based on the information that I have on the 6th column. My file looks like this: 91102 12684 0 ...
  1. #1
    Just Joined!
    Join Date
    Mar 2010
    Posts
    4

    Question extract line based on column information

    Hi,

    I would like to know how can I select lines based on the information that I have on the 6th column.

    My file looks like this:

    91102 12684 0 0 2 -9 G G C
    91102 12682 12685 12684 1 2 G G A
    91101 12680 0 0 2 -9 G G A
    91101 12678 12681 12680 2 2 G G A

    I want to extract the lines that have in the 6th collumn number 2. So in the end my file should be:

    91102 12682 12685 12684 1 2 G G A
    91101 12678 12681 12680 2 2 G G A

    Thanks in advance.

    Thais

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    This is simple in awk since awk reads a line at a time..

    Code:
    awk '{if ($6 == 2){printf("%s\n", $0);}}' datafile
    Last edited by gerard4143; 03-11-2010 at 01:50 PM.
    Make mine Arch Linux

  3. #3
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Or if your more comfortable with bash..

    Code:
    #! /bin/sh
    
    while read f
    do
    	set -- $f
    	if [ "$6" = "2" ]
    	then
    		echo $f
    	fi
    done < datafile
    Make mine Arch Linux

  4. #4
    Just Joined!
    Join Date
    Nov 2006
    Location
    Hyderabad
    Posts
    85
    Hi i am not gud enough in shell script.

    while read f -> will it read line by line ?
    set -- $f -> what is the meaning of it ? to what it is set to

  5. #5
    Just Joined!
    Join Date
    Mar 2010
    Posts
    4
    Thanks!!!

Posting Permissions

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