LinuxQuestions.org
Visit Jeremy's Blog.
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: 39
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: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
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


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
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


All times are GMT -5. The time now is 10:56 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration