LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 05-28-2008, 02:28 AM   #1
pooppp
Member
 
Registered: May 2008
Posts: 32

Rep: Reputation: 15
Bash


Hai,
I am having a txt file tht contains two fields such as name & IP address. I need to extract only tht IP address field. Give some idea.
 
Old 05-28-2008, 02:33 AM   #2
indeliblestamp
Member
 
Registered: Feb 2006
Distribution: Fedora
Posts: 341
Blog Entries: 3

Rep: Reputation: 40
A sample entry would be useful. Assuming its something like /etc/hosts, this should work fine:
Code:
cat filename | awk '{print $2}'
 
Old 05-28-2008, 02:36 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
Some examples
Code:
IP=$(cut -f2 -d\  file)
note the escaped blank space specified as delimiter (cut considers TAB as default delimiter).
Code:
IP=$(awk '{print $2}' file)
if the file has multiple lines and you want to read the second field of each line on at a time, you can use a loop using read with two input variables
Code:
while read name ip
do
  echo $ip
done < file
 
Old 05-28-2008, 07:41 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Personally, instead of

IP=$(cut -f2 -d\ file)

I'd use

IP=$(cut -f2 -d' ' file)

I just dislike using backslash if I can do it 'nicer'

Of course, you may also see the backquote used

IP=`cut -f2 -d' ' file`

bit more old school and the brackets version nests better.
 
  


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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
Bash development - move chmod options to bash jhwilliams Programming 9 06-26-2007 12:13 PM
Bash Sleep vs crontab and bash serial port nutthick Programming 4 06-01-2006 02:42 AM
Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators stefanlasiewski Programming 9 02-07-2006 05:20 PM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 06:42 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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