LinuxQuestions.org
Review your favorite Linux distribution.
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 08-06-2008, 09:09 AM   #1
B-Boy
Member
 
Registered: Jan 2008
Posts: 103

Rep: Reputation: 15
problem using cut command


Hi guys


I want to view one field of a file and i just cannot figure out how to with the cut -d command.The file will be something like
Code:
ip address 10.0.0.1 hostname linux1 model dell
now i want to view only the ip address "10.0.0.1" nothing else please help


t
 
Old 08-06-2008, 09:26 AM   #2
alex1983-0112
Member
 
Registered: Apr 2008
Location: Russia
Distribution: Fedora Core
Posts: 30

Rep: Reputation: 16
How about this?
Quote:
cat your_file | awk '{FS=" "; print $3}'
 
Old 08-06-2008, 09:32 AM   #3
ne pas
Member
 
Registered: Jul 2008
Posts: 55

Rep: Reputation: 23
cut's default delimiter is the TAB, use -d to set the delimiter to SPACE and -f to select the field.
Code:
$ cut -d' ' -f 3  example_file
 
Old 08-06-2008, 09:37 AM   #4
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
The -d option to cut is meant to define a delimiter different from the default one (TAB). If you want to use a blank space as delimiter you have to do something like
Code:
echo ip address 10.0.0.1 hostname linux1 model dell | cut -d\  -f3
where the blank space after -d is escaped to let the shell interpret it correctly. In alternative you can embed it in single or double quotes:
Code:
echo ip address 10.0.0.1 hostname linux1 model dell | cut -d' ' -f3
Regarding the suggested awk command, two notes: piping the output from cat is not necessary, since awk accepts one or more file names as arguments. Also the definition of FS is not needed, because the blank space is the default field separator. Hence the command would be simply:
Code:
awk '{print $3}' your_file
 
Old 08-06-2008, 09:48 AM   #5
B-Boy
Member
 
Registered: Jan 2008
Posts: 103

Original Poster
Rep: Reputation: 15
thanks guys that helped alot one more questino is there a way to view a certain line of a file. for example
Code:
cat file (line 7 only)
 
Old 08-06-2008, 09:53 AM   #6
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
Code:
sed -n 2p file
or
Code:
head -7 file | tail -1
or
Code:
awk 'NR==7' file
or...
 
Old 08-06-2008, 11:39 PM   #7
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Rep: Reputation: 33
Quote:
Originally Posted by colucix View Post
Code:
sed -n 2p file
I think this is correct one.....

sed -n 7p file




.
 
Old 08-07-2008, 09:41 AM   #8
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
Oh yeah, just a typo. I stand corrected. Thanks.
 
Old 08-08-2008, 12:27 AM   #9
B-Boy
Member
 
Registered: Jan 2008
Posts: 103

Original Poster
Rep: Reputation: 15
Smile

thanks guys that helped alot...thats what i love about linux we have a community of SKILLED people around the world who are willing to help and there is no question that cannot be answered
 
Old 08-09-2008, 10:45 AM   #10
haydar68
Member
 
Registered: Jul 2008
Posts: 35

Rep: Reputation: 15
Smile

Hi,

You can run this command to be able to get the third field:

echo "ip address 10.0.0.1 hostname linux1 model dell" |sed s/' \{1,\}'/";"/g|cut -d";" -f3

sed s/' \{1,\}'/";"/g will drop all blank space between each field.
 
  


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
Cut command Problem ratul_11 Linux - Newbie 3 10-11-2007 03:02 AM
the 'cut' command and HTML metalx1000 Programming 1 10-27-2006 08:50 PM
finger and cut command Craig Bowes Linux - Newbie 3 10-09-2004 12:49 PM
Cut Command Help!! fooforon Programming 2 02-05-2004 10:09 AM
Easy: Help with Cut command Krash_io Programming 4 07-17-2002 02:29 AM

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

All times are GMT -5. The time now is 12:49 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
Open Source Consulting | Domain Registration