The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl: Search for string on line then search and replace text Crypto Shell Programming and Scripting 4 01-04-2008 07:24 AM
Howto capture data from rs232port andpull data into oracle database-9i automatically boss UNIX for Dummies Questions & Answers 1 09-22-2007 11:35 PM
search and grab data from a huge file ting123 UNIX for Dummies Questions & Answers 1 06-06-2006 06:41 PM
search and replace dynamic data in a shell script csejl Shell Programming and Scripting 8 10-21-2003 07:33 PM
Advanced Search Problems.. Search by User Name Neo Post Here to Contact Site Administrators and Moderators 1 05-18-2003 09:28 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #8  
Old 03-24-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
So the question is, how do I print the first field from the first and second lines of a file?

Code:
awk '(NR == 1 || NR == 2) { print $1 }' /etc/hosts
Reply With Quote
Forum Sponsor
  #9  
Old 03-25-2008
Klashxx's Avatar
HP-UX/Linux/Oracle
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 383
Quote:
Originally Posted by era View Post
So the question is, how do I print the first field from the first and second lines of a file?

Code:
awk '(NR == 1 || NR == 2) { print $1 }' /etc/hosts
This is more concise:
Code:
awk 'NR<3{print $1}' /etc/hosts
Reply With Quote
  #10  
Old 03-25-2008
Registered User
 

Join Date: Feb 2008
Posts: 50
hoping near equal.
still i didnt get the exact output i want.

$hostname
datas

$for i in `hostname`
do
grep $i /etc/hosts |awk '(NR == 1 || NR == 2) { print $1,$2 }' > output
tail -1 output
ifconfig lan1 $i
done

this is the sample script. So what i want to change my current lan config to backup lan config.
datas is my curerent
data1 is my backup,

how iwll i do that?
Reply With Quote
  #11  
Old 03-25-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Drop the grep. And the for loop. If you just want the second line then say so.

Code:
ifconfig lan1 `awk 'NR==2 { print $1 }' /etc/hosts`
Try it with echo in front to verify that it does what you want before you deploy it.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux, regex, regular expressions, ubuntu

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:54 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0