Deleting double quoted string from a line when line number is variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting double quoted string from a line when line number is variable
# 1  
Old 07-12-2013
Deleting double quoted string from a line when line number is variable

I need to remove double quoted strings from specific lines in a file. The specific line numbers are a variable. For example, line 5 of the file contains

A B C "string"

I want to remove "string". The following sed command works:

Code:
sed '5 s/\"[a-zA-Z0-9]*\"//' $file

If there are multiple lines in the file that need to edited and the line number is a variable, i.e. $lineno, the following does not work:

Code:
sed '$lineno s/\"[a-zA-Z0-9]*\"//' $file

I believe the sed command must be double quoted for the variable to be evaluated, so I tried that and I get the error - Unmatched ".

Code:
sed "$lineno s/\"[a-zA-Z0-9]*\"//" $file


Last edited by Scrutinizer; 07-12-2013 at 09:27 PM.. Reason: code tags
# 2  
Old 07-12-2013
You can double-quote part of a string, like so:

Code:
sed "$lineno"' s/"[a-zA-Z0-9]*"//' $file

I don't think you need to escape " inside single-quotes(since sed shouldn't need them) as nothing can be escaped inside single quotes anyway.
# 3  
Old 07-12-2013
That worked - I feel stupid!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace double quotes with a single quote within a double quoted string

Hi Froum. I have tried in vain to find a solution for this problem - I'm trying to replace any double quotes within a quoted string with a single quote, leaving everything else as is. I have the following data: Before: ... (32 Replies)
Discussion started by: pchang
32 Replies

2. Shell Programming and Scripting

Deleting Line number from different file

file 1 abcde,dfgdfg,sdfds sdfsd,sdfd,sdfg,sdfs dwfds,sdfgdfg,sdfsdf file2 1 3 i need to delete the line numbers file 1 based on file2 i have taken in loop file 1 and i cant able to delete that line number in file 1 Plz help (4 Replies)
Discussion started by: ragu.selvaraj
4 Replies

3. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

4. Shell Programming and Scripting

Deleting a matching string(line) which is also in other lines

Hi, i need help with my shell script I have a file input.txt containing the following contents /. /usr /usr/share /usr/share/doc /usr/share/doc/wine /usr/share/doc/wine/copyright /usr/share/doc/wine/changelog.Debian.gz I need output as /usr/share/doc/wine /usr/share/doc/wine/copyright... (3 Replies)
Discussion started by: Amit0991
3 Replies

5. Shell Programming and Scripting

search a string in a particular column of file and return the line number of the line

Hi All, Can you please guide me to search a string in a particular column of file and return the line number of the line where it was found using awk. As an example : abc.txt 7000,john,2,1,0,1,6 7001,elen,2,2,0,1,7 7002,sami,2,3,0,1,6 7003,mike,1,4,0,2,1 8001,nike,1,5,0,1,8... (3 Replies)
Discussion started by: arunshankar.c
3 Replies

6. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

7. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

8. Shell Programming and Scripting

Deleting a line from a file based on one specific string instance?

Hello! I need to delete one line in a file which matches one very precise instance of a string only. When searching the forum I unfortunately only found a solution which would delete each line on which a particular string occurs. Let's assume I have a file composed of thousands of lines... (4 Replies)
Discussion started by: Black Sun
4 Replies

9. Shell Programming and Scripting

Adding a columnfrom a specifit line number to a specific line number

Hi, I have a huge file & I want to add a specific text in column. But I want to add this text from a specific line number to a specific line number & another text in to another range of line numbers. To be more specific: lets say my file has 1000 lines & 4 Columns. I want to add text "Hello"... (2 Replies)
Discussion started by: Ezy
2 Replies
Login or Register to Ask a Question