Solved

Unix shell script sed command error

Posted on 2014-08-20
1
407 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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 …
Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
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.

749 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