?
Solved

Unix shell script sed command error

Posted on 2014-08-20
1
Medium Priority
?
435 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.
Suggested Courses

762 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