LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-16-2009, 05:51 PM   #1
mending73
LQ Newbie
 
Registered: Sep 2009
Posts: 4

Rep: Reputation: 0
Grep from only a certain column


Im writing a program and I need to grep the variable $CurrentID from a file (output.txt) that looks like this:

kmtfk6 10581 10547 0 17:47 pts/10 00:00:00 vim test_slist.cpp
ajhvff 10702 10277 0 17:48 pts/15 00:00:00 lab3
raj3m8 10705 13879 0 17:48 pts/8 00:00:00 ps -ef
root 11394 2451 0 11:47 ? 00:00:00 sshd: jmshfb [priv]
root 11400 11 0 11:47 ? 00:00:00 [cifsd]
root 11405 11 0 11:47 ? 00:00:00 [cifsd]
jmshfb 11409 11394 0 11:47 ? 00:00:00 sshd: jmshfb@pts/4
jmshfb 11410 11409 0 11:47 pts/4 00:00:00 -bash
root 11666 2451 0 12:18 ? 00:00:00 sshd: hsh8r8 [priv]
root 11672 11 0 12:18 ? 00:00:00 [cifsd]
hsh8r8 11678 11666 0 12:18 ? 00:00:00 sshd: hsh8r8@pts/3


I want to grep the entire line where $CurrentID is located in the second column only and store that line in IDLine.

As of right now I have IDLine=`grep $CurrentID output.txt` and that works, but it will grep if it sees CurrentID anywhere in the document not only in the second column. Any helpful tips on how to specify only that column would be greatly appreciated.
 
Old 09-16-2009, 06:15 PM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,887

Rep: Reputation: 351Reputation: 351Reputation: 351Reputation: 351
Perhaps awk to read the file line by line, focusing on the currentID field, piped through grep to select the currentID of interest, the back through awk to print the entire line.

awk (read file line by line) | grep $currentID | awk print the line

I don't do such things often enough to be able to give you an example. www.google.com/linux for awk and grep examples.
 
Old 09-16-2009, 06:31 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
If that file has variable num of spaces between cols, use awk as mentioned; http://www.grymoire.com/Unix/Awk.html.
If its a set num of spaces/tabs eg 1, you could use cut; http://linux.die.net/man/1/cut
 
Old 09-16-2009, 07:21 PM   #4
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,117
Blog Entries: 2

Rep: Reputation: 113Reputation: 113
egrep "^[^ ]* *?$CurrentID " tmp.txt
 
Old 09-16-2009, 08:01 PM   #5
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by mending73 View Post
Im writing a program and I need to grep the variable $CurrentID from a file (output.txt) that looks like this:

kmtfk6 10581 10547 0 17:47 pts/10 00:00:00 vim test_slist.cpp
ajhvff 10702 10277 0 17:48 pts/15 00:00:00 lab3
raj3m8 10705 13879 0 17:48 pts/8 00:00:00 ps -ef
root 11394 2451 0 11:47 ? 00:00:00 sshd: jmshfb [priv]
root 11400 11 0 11:47 ? 00:00:00 [cifsd]
root 11405 11 0 11:47 ? 00:00:00 [cifsd]
jmshfb 11409 11394 0 11:47 ? 00:00:00 sshd: jmshfb@pts/4
jmshfb 11410 11409 0 11:47 pts/4 00:00:00 -bash
root 11666 2451 0 12:18 ? 00:00:00 sshd: hsh8r8 [priv]
root 11672 11 0 12:18 ? 00:00:00 [cifsd]
hsh8r8 11678 11666 0 12:18 ? 00:00:00 sshd: hsh8r8@pts/3


I want to grep the entire line where $CurrentID is located in the second column only and store that line in IDLine.

As of right now I have IDLine=`grep $CurrentID output.txt` and that works, but it will grep if it sees CurrentID anywhere in the document not only in the second column. Any helpful tips on how to specify only that column would be greatly appreciated.
In this routine, I use the whitespace between fields to split the line up. This method doesn't case how much whitespace there is between fields, but it would care if any first fields were to have spaces embedded in them.

This approach is easier than splitting the fields up on fixed columns, but remember the above limitation.

Code:
currentID=11409

while read line
do
   array=($line)
   if [[ ${array[1]} =~ $currentID ]]
   then
      echo "Found \"$line\"."
   fi
done
You run the script like this:

Code:
$ script_name.sh < data_file.txt
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Read text file column by column RVF16 Programming 11 05-31-2009 07:16 AM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 10:31 PM
Concatenate column 1 and column 2 of related lines cgcamal Programming 4 11-20-2008 10:43 AM
how to grep multiple filters with grep LinuxLover Linux - Enterprise 1 10-18-2007 07:12 AM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM


All times are GMT -5. The time now is 07:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration