Solved

Unix shell script sed command error

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CPU monthly average? 12 105
Problem Imaging Computers With Clonezilla 2 82
nodeip 9 75
LINUX ZIP - UNCOMPRESS WINDOWS PATH 3 86
My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
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…
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…

911 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

20 Experts available now in Live!

Get 1:1 Help Now