LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 07-14-2006, 06:28 AM   #1
christina_rules
LQ Newbie
 
Registered: May 2006
Posts: 25

Rep: Reputation: 15
cut fields in a file


i need help.
i have a file with fields like name, address, contact and country.
how can i use the cut command to display the name, address and contact based on a given country?
thanks.
 
Old 07-14-2006, 06:52 AM   #2
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,393
Blog Entries: 1

Rep: Reputation: 63
I don't think cut is the best choice to this task.
You can do that very easily with grep.
Code:
$ grep -w "France" your-file
will display only lines with the word France, but Frances, france or whateverFrance will not.

If this is not enough, you can check for the word occurring in a specific position. Lets say the country name is 4th field and the fields are separated by ":", e.g.

"John Doe:45 5th Av:John:United States"


Code:
$ cat your-file | awk -F: '$4="United States" {print $1, $2, $3, $4}'
$ John Doe 45 5th Av John United States
will only print lines of your-file with "United States occuring in the 4th field.

Last edited by marozsas; 07-14-2006 at 06:53 AM.
 
Old 07-14-2006, 06:53 AM   #3
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
If my file looks like this and I want to get everything but country, I would use the command: grep "Japan" < file.txt | cut -d" " -f1-4
Code:
fred some place contact_name USA
sue some place contact_name France
sally some place contact_name England
joe some place contact_name Canada
john some place contact_name Japan
 
Old 07-14-2006, 07:27 AM   #4
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
If you want to print out the entire line based on the value of the 4th field (Country), I believe that awk would be your best choice. You can just use the 4th field in your test. On the otherhand, using grep or sed, you would need to make sure that the test holds for the 4th field. Suppose you had a street address containing "France Street". You could have a grep or sed reg-ex of "^[^ ]* ^[^ ]* ^[^]* France" but because awk was written to work with text data files it would be your best choice.
 
Old 07-14-2006, 10:12 AM   #5
Dragineez
Member
 
Registered: Oct 2005
Location: Annapolis
Distribution: Ubuntu
Posts: 275

Rep: Reputation: 32
Awk

There are a large number of ways to do what you ask, but I would suggest awk as well. That's exactly what it exists to do.
 
Old 07-14-2006, 04:00 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,962
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Quote:
Originally Posted by marozsas
Code:
$ cat your-file | awk -F: '$4="United States" {print $1, $2, $3, $4}'
$ John Doe 45 5th Av John United States
will only print lines of your-file with "United States occuring in the 4th field.
That solution is way too verbose :}
Considering that we don't know what the field separator was
I'll go with your approach and assume it's a :
Code:
awk -F: '$4 ~ /country/' your-file
Grep is not really suitable, because there might be someone
with Frances as their first name, or Germany as their last name,
they'd show up using grep even though they might be both
living in Japan.



Cheers,
Tink

Last edited by Tinkster; 07-14-2006 at 04:01 PM.
 
Old 07-14-2006, 04:44 PM   #7
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
The OP did ask how to do it with cut.
 
Old 07-14-2006, 04:57 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,962
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Quote:
Originally Posted by homey
The OP did ask how to do it with cut.
But was the OP aware of the fact that cut knows nothing
about searching/matching? :} Obviously you can't do it
with cut alone, and since we're invoking at least ONE other
tool already we might as well use the most suitable tool in
the first place?


Cheers,
Tink
 
Old 07-14-2006, 10:17 PM   #9
christina_rules
LQ Newbie
 
Registered: May 2006
Posts: 25

Original Poster
Rep: Reputation: 15
oh ok...yeah i didn't know that cut has got nothing to do with searching and matching..thanks a lot for that..learnt something here.

ok, i'll try it with awk then..thanks a lot
 
Old 07-15-2006, 07:28 AM   #10
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,393
Blog Entries: 1

Rep: Reputation: 63
Quote:
Originally Posted by Tinkster
Code:
awk -F: '$4 ~ /country/' your-file
Cool !, super cool !
 
Old 07-15-2006, 08:16 AM   #11
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,393
Blog Entries: 1

Rep: Reputation: 63
off topic

Quote:
Originally Posted by homey
The OP did ask how to do it with cut.
What "OP" stand for ? I know you are talking about the person who did the question, but I can't figure out what means this 2 letters ?!?!?! If was PO, Post Owner would be a good guess, but OP ???

Last edited by marozsas; 07-15-2006 at 08:17 AM.
 
Old 07-15-2006, 08:27 AM   #12
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

OP => Original Poster.

It is indeed the person that initially started the thread/asked the question.

Hope this clears things up.
 
Old 07-15-2006, 10:00 AM   #13
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,393
Blog Entries: 1

Rep: Reputation: 63
Thanks druuna ! I've seen this term around in this forum several times, and I was wondering what its meaning...thanks for the answer
 
  


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
How to cut a big file into pieces of a certain size? kornerr Linux - General 9 12-31-2005 03:52 PM
Manipulating Fields Of Flat File Prasun1 General 10 09-29-2005 11:42 AM
please help with cut and paste into .c file henry_huckem Programming 7 01-28-2005 05:37 AM
short cut to a file in the path balloon Linux - Software 1 11-15-2004 01:35 PM
Displaying fields in wtmp file? eclapton1 Programming 1 06-26-2004 09:51 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration