Solved

Unix shell script sed command error

Posted on 2014-08-20
1
396 Views
Last Modified: 2014-08-25
Below script runs successfully

a=0
b=1
r=10
while [ $a -lt $ncnt ]
do
   echo $a
sed -n $b,$10p test_ids.txt > test_ids_1.txt
echo $b
a=`expr $a + 1`
done


But below code is not work it gives error
I am passing variable r instead of 10
Please help what am I wrong ?

a=0
b=1
r=10
while [ $a -lt $ncnt ]
do
   echo $a
sed -n $b,$rp test_ids.txt > test_ids_1.txt
echo $b
a=`expr $a + 1`
done
0
Comment
Question by:digs developer
1 Comment
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 40274614
You should separate the variable "r" from  from the "p" subcommand by means of the curly braces:

sed -n $b,${r}p test_ids.txt >> test_ids_1.txt

Use ">>" instead of ">" alone, otherwise the output file "test_ids_1.txt" will get overwritten at each iteration - although it will contain the same first ten lines of "test_ids.txt" over and over, depending on the value of "ncnt" (a variable you shouldn't forget to fill, by the way).
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now