LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-10-2007, 09:01 AM   #1
severian23
Member
 
Registered: Jan 2003
Location: Stone Mountain, GA
Distribution: Currently: RHEL/Centos 5.x-6.x servers, Mint Desktop
Posts: 41

Rep: Reputation: 15
Find a field number for a value in text file - BASH


I'm trying to find a bash solution to return the field number for a string in a tab-delimited text file. Anyone have an idea?

Last edited by severian23; 01-10-2007 at 09:07 AM.
 
Old 01-10-2007, 09:41 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
awk -Ft would parse a line using tabs as delimiters.

You could do something like:

Code:
FLDCNT=1
for FIELD in `cat <file> |awk -Ft '{print $0}' inputline`
do if echo $FIELD |grep <pattern> >/dev/null 2>&1
   then echo <pattern> was found in field $FLDCNT
   fi
   FLDCNT=`expr $FLDCNT + 1`
done

Of course you'd put in whatever word/pattern you're looking for in palce of <pattern> and your text file name in place of <file>.

Note this gives you the field number per line so if you had it on more than one line you could see multiple field numbers (one for each line it is found on).
 
Old 01-10-2007, 09:45 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
With gawk you can try

Code:
gawk '{split($0,array,"\t"); for (x in array) if (array[x] == "string") {print x}}' filename.txt
this look for a field that matches exactly the given "string". If you look for a field that contains a given "substring", you can try

Code:
gawk '{split($0,array,"\t"); for (x in array) if (match(array[x],"substring") > 0) {print x}}' filename.txt

Last edited by colucix; 01-10-2007 at 09:47 AM.
 
Old 01-10-2007, 09:49 AM   #4
severian23
Member
 
Registered: Jan 2003
Location: Stone Mountain, GA
Distribution: Currently: RHEL/Centos 5.x-6.x servers, Mint Desktop
Posts: 41

Original Poster
Rep: Reputation: 15
[QUOTE=colucix]With gawk you can try

Code:
gawk '{split($0,array,"\t"); for (x in array) if (array[x] == "string") {print x}}' filename.txt
this look for a field that matches exactly the given "string".

___________________

Thanks so very much! That was exactly what I needed. I appreciate all your help!
 
  


Reply



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
bash read a given line number from text bendeco13 Programming 7 08-31-2012 03:49 PM
flash animation text input field shows nothing kyuso Linux - Software 1 09-19-2006 10:17 AM
javascript: change value of text field and submit codec Programming 3 06-06-2005 08:08 AM
Replacing text in a specific field Seventh_Warrior Programming 5 04-11-2005 12:04 PM
how to find out if argument is number: bash kubicon Linux - Newbie 1 09-19-2003 04:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:27 PM.

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
Open Source Consulting | Domain Registration