LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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
 
LinkBack Search this Thread
Old 04-09-2008, 07:08 AM   #1
say_hi_ravi
Member
 
Registered: Jan 2008
Posts: 62

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
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: OpenSUSE 12.1 CentOS 6.2
Posts: 9,012

Rep: Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353
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: 62

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


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
Trackbacks are Off
Pingbacks are On
Refbacks are 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


All times are GMT -5. The time now is 04:50 AM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration