How to use sed to copy specific lines from a file using shell variables?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to use sed to copy specific lines from a file using shell variables?
# 1  
Old 05-06-2011
How to use sed to copy specific lines from a file using shell variables?

hello!
I am trying to use sed to copy specific set of lines from a file for which the starting and ending line numbers of the lines to be copied are stored in shell variables. How can i copy those lines?

if the input_file is something like this
Quote:
abc
def
ghi
jkl
mno
pqr
and if the following is the script
Code:
a=2
b=4
sed '/$a/,/$b/p' input_file > new_file

the output required is
Quote:
def
ghi
jkl
the shell variable a and b can presume different values accordingly. how can these shell variables be used in sed ? Can awk be used for this?
# 2  
Old 05-06-2011
use double quote instead of simple one
and remove the slash by the way :

the more simple:
Code:
sed $a,$b'!d' infile

but those may also work:
Code:
sed ''$a,$b'!d' infile

(4 simple quotes) or
Code:
sed "$a,$b"'!d' infile

(2 double quotes 2 simple quotes)

or simply
Code:
sed -n $a,$b'p' infile


Last edited by ctsgnb; 05-06-2011 at 11:56 AM..
This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 05-06-2011
hey! ctsgnb
None of the modified above suggestions worked for me..
however, the earlier solution suggested by you.. i.e
Code:
sed "/$a/,/$b/!d" input_file > new_file

worked perfectly for me. Thank you, very much. Smilie
# 4  
Old 05-06-2011
All depends what you put in $a and $b :

if you use line number, you should not use the slash
Code:
a=2
b=4
sed $a,$b'!d' infile

but if you want to do your selection based on the pattern matching, then you need the slash
Code:
a="def"
b="jkl"
sed "/$a/,/$b/!d" infile

so your example was confusing because you stated :
Code:
a=2
b=4

so i assumed you were working with line number instead of pattern.
This User Gave Thanks to ctsgnb For This Post:
# 5  
Old 05-06-2011
ok! i now get it! what you are saying is exactly correct!

For the line numbers as in the example given by me.. the slash is not required and for matching the patterns it is required (helped me in the other way).

Thank you so much for the clarification. Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk,sed,shell copy ID in new lines

Hi all i want table file in pattern like under ID(digits and underline) phonetic begin_byte(only plus number) end_byte(only plus number) but ID loose in next lines of my file my input file like this: input 001_1_174 474536 482492 mo 482492 504062 'er/ 504062 517352 ruze 517352... (1 Reply)
Discussion started by: alii
1 Replies

2. Shell Programming and Scripting

ksh sed - Extract specific lines with mulitple occurance of interesting lines

Data file example I look for primary and * to isolate the interesting slot number. slot=`sed '/^primary$/,/\*/!d' filename | tail -1 | sed s'/*//' | awk '{print $1" "$2}'` Now I want to get the Touch line for only the associate slot number, in this case, because the asterisk... (2 Replies)
Discussion started by: popeye
2 Replies

3. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum using sed, awk

Hi friends, This is sed & awk type question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers whenever i find it and produce an output file with the sum. For example ###start of input text file #### abc def ghi 1 2 3 4 kjld random... (3 Replies)
Discussion started by: kaaliakahn
3 Replies

4. Shell Programming and Scripting

How to change a number on a specific lines in a file with shell?

Hello My problem is that I want to change some specific numbers in a file. It is like, 2009 10 3 2349 21.3 L 40.719 27.388 10.8 FRO 7 0.8 1.1LFRO 2.6CFRO 1.1LMAM1 GAP=157 1.69 5.7 5.9 5.8 0.5405E+01 0.4455E+00 0.1653E+02E STAT SP IPHASW D HRMM SECON CODA AMPLIT... (11 Replies)
Discussion started by: miriammiriam
11 Replies

5. Shell Programming and Scripting

copy, then delete lines in file with sed using a pattern

I need to copy lines to a new file from files with sed using a pattern in char postions 1-3. Then after the copy, I need to delete those same lines from the input files. For example, string "ABC" in pos 1-3 (6 Replies)
Discussion started by: laksjfhoius9123
6 Replies

6. Shell Programming and Scripting

Solaris KSH shell script to copy all lines from one file to another

Hello, more of a windows wscript guy. However I took a new position that requires me to support some solaris servers. So... issue is that I need to copy all lines from a file to a temporary file and then copy them back into the original file starting at line 1. Reason I need to do this is... (5 Replies)
Discussion started by: ZigZaggin
5 Replies

7. Shell Programming and Scripting

How to copy specific file.txt in specific folder?

hye there... i have a problem to copy file in specific folder that will change the name according to host,time(%m%s) and date(%Y%M%D) example folder name: host_20100531.154101801 this folder name will always change... but i just want to copy the AAA.txt and BBB.txt file.. really need... (17 Replies)
Discussion started by: annetote
17 Replies

8. Shell Programming and Scripting

using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables this is where i am at grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean grep -n Destination... (2 Replies)
Discussion started by: todd.cutting
2 Replies

9. Shell Programming and Scripting

SED 4.1.4 - INI File Change Problem in Variables= in Specific [Sections] (Guru Help)

GNU sed version 4.1.4 on Windows XP SP3 from GnuWin32 I think that I've come across a seemingly simple text file change problem on a INI formatted file that I can't do with SED without side effects edge cases biting me. I've tried to think of various ways of doing this elegantly and quickly... (5 Replies)
Discussion started by: JakFrost
5 Replies

10. Shell Programming and Scripting

Shell Script to read specific lines in a file

I have a file with contents as follows Record 1: Rejected - Error on table "DWO"."P2G_CUST_EVENTS". ORA-00001: unique constraint (DWO.CUST_EVENTS_PK) violated Record 5: Rejected - Error on table "DWO"."P2G_CUST_EVENTS". ORA-00001: unique constraint (DWO.CUST_EVENTS_PK) violated Record 6:... (5 Replies)
Discussion started by: varshanswamy
5 Replies
Login or Register to Ask a Question