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 04-09-2008, 07:08 AM   #1
say_hi_ravi
Member
 
Registered: Jan 2008
Posts: 75

Rep: Reputation: 15
Execute desired line from the file


I have below file:

#cat z
ls
touch test
hostname

I want to execute 2nd or line of the above file. How can I proceed?

Something like below:
#exec `head -3|tail -1`

it should give me hostname of my machine.

But unfortunately its not working. Please help.
 
Old 04-09-2008, 07:31 AM   #2
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
In the command line you've posted there is no reference to the file (it should be "head -3 filename | tail -1". Anyway, I should use eval, as in
Code:
eval $(sed -n 3p filename)
eval $(awk 'NR==3' filename)
just two of a long serie of examples!
 
Old 04-09-2008, 07:37 AM   #3
say_hi_ravi
Member
 
Registered: Jan 2008
Posts: 75

Original Poster
Rep: Reputation: 15
It worked!!!
Thank You very much.
 
  


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
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 PM
PHP execute at command line gjmwalsh Mandriva 2 12-06-2003 01:03 AM
Can't execute ifconfig at command line jhsuan Linux - Newbie 3 08-30-2002 08:57 PM

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

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