LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-27-2009, 01:07 AM   #1
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Rep: Reputation: 30
How to use a command inside sed


Hi all ,


Am trying to print the last n lines of a file using sed, but am unable to do a echo within sed. can someone correct me?


Code:
sed -n '1,40' linetest
is working

if n=40 (am got this from some other output)

I try to the following and failed.

Code:
sed -n '1,"/`echo $num`/"p' linetest
I want to print the first n lines only .I want a substitute for
Code:
head -`echo $num`  linetest
using sed.


Thanks
 
Old 02-27-2009, 01:42 AM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
To print the last 5 lines of a file you would use:

Code:
sed -e :a -e '$q;N;6,$D;ba' file
To print the first 5 lines:

sed -n '1,5p' file

I don't know how to pass values to sed commands, though.
 
Old 02-27-2009, 04:19 AM   #3
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Original Poster
Rep: Reputation: 30
I got that with this option.

Code:
sed -n "1,`echo $num`p" linetest
Thanks sycamorex
 
Old 02-27-2009, 04:37 AM   #4
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
I usually do:

Code:
sed -n '1,'"$(echo $num)"'p' linetest
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed inside awk ZAMO Linux - General 1 02-26-2009 04:13 AM
[Bash] Command inside a variable yvovandoorn Programming 5 01-20-2007 06:48 PM
running sed inside script file bajaj111 Linux - Software 9 11-08-2006 09:23 AM
very new to C, how to execute command inside C? neurotic85 Programming 5 09-05-2005 08:19 AM
how do I run a command from inside a c++ program? exodist Programming 1 04-06-2004 04:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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