copying a line from a file using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting copying a line from a file using sed
# 1  
Old 08-06-2011
copying a line from a file using sed

Hi All,

I need to copy a specific line from a file to another file.
lets suppose the line number 13 of a file
when I am writing the line number explicitly.. its working fine
Code:
sed -n '13p' afile > anotherfile

but, when inside a script, i am getting the line number value inside a variable called line, i am not getting the expected result.

Code:
sed -n '$linep' afile > anotherfile

even I tried with
Code:
sed -n '"$line"p' afile > anotherfile

I am getting the error :
Code:
sed: "$line"p is not a recognized function.

Please help !!!
# 2  
Old 08-06-2011
Hi,

See next example:
Code:
$ cat infile
last name,first name,address,zip,telephone,year of birth
Appleby,Anne,Shrimpsbury,SH-124,555-4455,1960
Brown,Carl,Midsomer Garden,MD-945,555-2423,1982
Smith,John,Shrimpsbury,SH-123,555-4584,1978
Underwood,Mary,Oglsby,OG-345,555-3434,1956
$ cat script.sh
#!/usr/bin/env bash

line=3
sed -n "$line"'p' infile
$ bash script.sh
Brown,Carl,Midsomer Garden,MD-945,555-2423,1982

Regards,
Birei
# 3  
Old 08-06-2011
Oh Thanks Birei.. Believe, I have got it from your example.
I was using
Code:
sed -n '"$line"p' infile

rather, it should be
Code:
sed -n "$line"'p' infile

I am not with my system now. Will check and confirm later. Thanks for the help.
# 4  
Old 08-06-2011
Also works:
Code:
$ cat script.sh
#!/usr/bin/env bash                                                                                                                                                                                                                          
                                                                                                                                                                                                                                             
line=3                                                                                                                                                                                                                                       
sed -n "${line}p" infile
$ bash script.sh
Brown,Carl,Midsomer Garden,MD-945,555-2423,1982

Regards,
Birei
# 5  
Old 08-06-2011
Code:
line=13
sed -n ''$line'p' infile

Note: these are two single quotes followed by a $ and then word line

Code:
tmp>wc -l austa1106.002
3079 austa1106.002
tmp>sed -n '13 p' austa1106.002
LA999888
tmp>export myln=13
tmp>sed -n ''$myln' p' austa1106.002
LA999888
tmp>

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copying characters on each line in a file

Hello, I would like to copy the first and third char on each line of a file and place them in the 14h and 17th char positions. The file name is listed first and is 6 char's and the dir name is second and also same char size on each line. The file has thousands of lines. Initial... (6 Replies)
Discussion started by: dmm
6 Replies

2. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

3. Shell Programming and Scripting

sed to replace a line with modified line in same file

i have few lines in a file... i am reading them in a while loop so a particular line is held is $line1.. consider a modified line is held in $line2.... i want to replace $line1 with $line2 in the same file... how to do it..? i have come up till the below code sed "s/$line1/$line2/g" tmpfile.sql... (5 Replies)
Discussion started by: vivek d r
5 Replies

4. Shell Programming and Scripting

sed command for copying the contents of other file replacing it another file on specifc pattern

We have 2 file XML files - FILE1.XML and FILE2.xml - we need copy the contents of FILE1.XML and replace in FILE2.xml pattern "<assignedAttributeList></assignedAttributeList>" FILE1.XML 1. <itemList> 2. <item type="Manufactured"> 3. <resourceCode>431048</resourceCode> 4. ... (0 Replies)
Discussion started by: balrajg
0 Replies

5. Shell Programming and Scripting

Copying a line from one file to other using vi editor

Hi Guys, the command ":yy" copies the line but it can be pasted in the same file. How can it be done if I want to copy it in other file. (2 Replies)
Discussion started by: ajincoep
2 Replies

6. Shell Programming and Scripting

how to use sed for reading from a file, line by line?

I have a file, from where I need to extract some data. But, the condition is like following : The script will read line by line,while checking if any line starts with 'MN'. If found true, it looks for if the immediate line starts with 'PQ' or not. If its true then, extract few fields from that... (1 Reply)
Discussion started by: mady135
1 Replies

7. Shell Programming and Scripting

sed to read line by line and input into another file

I have two files. Fileone contains text string one text string two text string three Filetwo contains Name: Address: Summary: Name: Address: Summary: Name: Address: Summary: I would like to use sed to read each line of file one and put it at the end of the summary line of file... (3 Replies)
Discussion started by: dolacap
3 Replies

8. Shell Programming and Scripting

Copying x words from end of line to specific location in same line

Hello all i know it is pretty hard one but you will manage it all after noticing and calculating i find a rhythm for the file i want to edit to copy the last 12 characters in line but the problem is to add after first 25 characters in same line in other way too copy the last 12 characters... (10 Replies)
Discussion started by: princesasa
10 Replies

9. Shell Programming and Scripting

copying from line N1 to line N2 of a file in a series of files

Dear community, I'm quite a newbie with scripting, I have this problem: I have a file with many lines and I want to copy the lines from 1 to N to file.1, from N+1 to 2N to file.2, and so on up to the end of the file I have tried with something like this (N=43 in this example): awk '{for... (2 Replies)
Discussion started by: paolalup
2 Replies

10. UNIX for Dummies Questions & Answers

filtering and copying contains of a file using awk/sed

Hello folks, I have 2 files one( file1) contains the ddl for a view and file 2 contains the view defination/alias columns. I want to merge the 2 into a third file using awk/sed as follows: cheers ! :b: FILE1 ----- PROMPT FIRST_VIEW CREATE OR REPLACE FORCE VIEW FIRST_VIEW AS SELECT... (2 Replies)
Discussion started by: jville
2 Replies
Login or Register to Ask a Question