Solved

Unix shell script sed command error

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

760 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

21 Experts available now in Live!

Get 1:1 Help Now