LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 01-09-2012, 05:22 AM   #1
anandg111
Member
 
Registered: Jan 2012
Posts: 56

Rep: Reputation: Disabled
Smile how to use variable in sed command ?


I want to use variable in sed with the option p.
here is my code


counter=1
while [ $counter -lt 4 ]
do
sed -n $counter p /tmp/file1
counter=$counter+1
done

but this codeis not working. it is throwing the below error.

sed: 1 is not a recognized function.

please help me


Thanks.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 01-09-2012, 06:18 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Have a look at this:
Code:
counter=1
while [[ $counter < 4 ]]
do
 sed -n "${counter}p" /tmp/file1
 let counter=$counter+1
done
The curly brackets are needed, otherwise bash thinks it is using counterp instead of counter.

Also fixed some other problems in the posted code snippet.

Hope this helps.
 
2 members found this post helpful.
Old 01-09-2012, 11:52 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
1) Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability.

2) QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS. You should never leave the quotes off a variable expansion unless you explicitly want the resulting string to be word-broken by the shell. This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

As you can see, the solution to your sed problem also comes from understanding proper quoting. (BTW, re druuna's post, the brackets aren't necessary if you separate the "p" from "counter", as sed ignores spaces between letter commands. You got the error you did because the entire expression needs to be passed to sed as a single unit.)

3) It's recommended to use ((..)) for numerical tests, and [[..]] for string tests and other complex expressions. Don't use the old [..] test unless you specifically need POSIX-style portability.

http://mywiki.wooledge.org/BashFAQ/031
http://mywiki.wooledge.org/ArithmeticExpression

4) In this case, a c-style for loop would be better than a while loop. You can completely avoid the test and manually incrementing the variable.

Code:
for (( cnt=1 ; cnt < 4 ; cnt++ )); do
	sed -n "$cnt p" /tmp/file1
done
http://wiki.bash-hackers.org/syntax/ccmd/c_for

5) When you get a chance, read through the whole BashGuide. It covers all the basic concepts in bash scripting, in an easy-to-read format.

http://mywiki.wooledge.org/BashGuide
 
1 members found this post helpful.
Old 01-11-2012, 11:27 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
You know, I just noticed...if all you want to do is print a range of lines from the file (or apply some expression to them), there's no need to run a loop. sed can be told to address a range of lines, as well as a single one.

Code:
cnt1=1
cnt2=3

sed -n "$cnt1,$cnt2 p" /tmp/file1

Here are a few useful sed references.
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt
 
  


Reply

Tags
bash, loop, sed, word splitting



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
sed substitution of variable with a variable ngyz86 Linux - Newbie 6 01-05-2011 07:44 AM
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 03:44 PM
how to replace with variable using sed? babag Programming 4 09-17-2008 03:28 PM
sed help with variable RudraB Programming 1 07-04-2008 01:10 AM
sed and variable chirophil91 Programming 2 10-19-2004 03:23 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:51 AM.

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