LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-09-2013, 03:26 AM   #1
tusharsharma43
LQ Newbie
 
Registered: Jan 2010
Location: Vadodara, Gujarat
Distribution: Fedora, OpenSuse, Ubuntu, Mint and Debian
Posts: 22

Rep: Reputation: 0
How to Get coloumn number of csv file using AWk


Hello,

I want to get coloumn number from csv file using column value or name.

Ex.
xyz.txt

abc|123|xyz|pqr|mno

I want to coloumn number of "xyz" using awk.

Can any one help me with the issue.

Thanks
Tushar
 
Old 01-09-2013, 04:23 AM   #2
bijo505
Member
 
Registered: Nov 2012
Location: Bangalore
Distribution: Fedora & Ubuntu
Posts: 77

Rep: Reputation: 18
Hi,

Are you looking for the o/p like this?
[code]
cat xyz.txt | awk -F "|" '{print NR,$3}' # This will print the field with line number
1 xyz

cat xyz.txt | awk -F "|" '{print $3}' # This will print only the 3rd field
xyz
[\code]
--
Thanks,
Bijo
 
1 members found this post helpful.
Old 01-09-2013, 04:23 AM   #3
theFreethinker
LQ Newbie
 
Registered: Jul 2011
Location: Tartu, Estonia
Distribution: Ubuntu
Posts: 17

Rep: Reputation: Disabled
Set up a counter, and in loop check the column content against previously known string until you find the match. Now you just have to read the counter to get the column number. Probably there are more elegant ways to do it but this is most robust and straight-forward method.
 
1 members found this post helpful.
Old 01-09-2013, 06:02 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Does it have to be awk?

Code:
echo "abc|123|xyz|pqr|mno" | tr '|' '\n' | grep -n xyz | cut -f1 -d':'
 
1 members found this post helpful.
Old 01-09-2013, 06:15 AM   #5
theFreethinker
LQ Newbie
 
Registered: Jul 2011
Location: Tartu, Estonia
Distribution: Ubuntu
Posts: 17

Rep: Reputation: Disabled
Quote:
Originally Posted by Guttorm View Post
Does it have to be awk?

Code:
echo "abc|123|xyz|pqr|mno" | tr '|' '\n' | grep -n xyz | cut -f1 -d':'
If the actual problem involves bigger input file, with changing structure,
Code:
head -1 inputfile.txt
instead of 'echo' might be useful. But solution itself seems to be as elegant as possible.
 
1 members found this post helpful.
Old 01-11-2013, 12:29 AM   #6
tusharsharma43
LQ Newbie
 
Registered: Jan 2010
Location: Vadodara, Gujarat
Distribution: Fedora, OpenSuse, Ubuntu, Mint and Debian
Posts: 22

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by theFreethinker View Post
Set up a counter, and in loop check the column content against previously known string until you find the match. Now you just have to read the counter to get the column number. Probably there are more elegant ways to do it but this is most robust and straight-forward method.
Okay thats the basic way, I thought there might be some command using which i can easily retrieve coloumn number of given string in given csv file.

Thanks & Regards
Tushar Sharma
 
Old 01-11-2013, 12:30 AM   #7
tusharsharma43
LQ Newbie
 
Registered: Jan 2010
Location: Vadodara, Gujarat
Distribution: Fedora, OpenSuse, Ubuntu, Mint and Debian
Posts: 22

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Guttorm View Post
Does it have to be awk?

Code:
echo "abc|123|xyz|pqr|mno" | tr '|' '\n' | grep -n xyz | cut -f1 -d':'
Thanks a lot, that was only thing I was trying.


Thanks & Regards
Tushar Sharma
 
Old 01-11-2013, 01:05 AM   #8
theFreethinker
LQ Newbie
 
Registered: Jul 2011
Location: Tartu, Estonia
Distribution: Ubuntu
Posts: 17

Rep: Reputation: Disabled
Quote:
Originally Posted by tusharsharma43 View Post
Okay thats the basic way, I thought there might be some command using which i can easily retrieve coloumn number of given string in given csv file.
Actually you asked initially how to get the column number using awk, not just how to get the column number.

But still glad you found a solution to your problem!
 
Old 01-11-2013, 01:58 AM   #9
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
Following the suggestion by Guttorm, here is the same algorithm in awk:
Code:
awk 'BEGIN{RS="|"}/xyz/{print NR; exit}' file
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Rename files using csv file and awk tuxlux Programming 6 09-03-2013 07:46 PM
sed or awk for csv file clarifying tnrooster Programming 4 12-03-2011 09:58 AM
awk and csv file Firebar Programming 9 11-18-2010 05:00 AM
Convert a number on each line to CSV file OlRoy Programming 9 08-20-2008 11:05 AM
AWK: change a particular field in a csv file help help help!!!! haydar68 Programming 20 08-03-2008 01:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:29 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