deleting few lines from a file dynamically


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting deleting few lines from a file dynamically
# 1  
Old 12-07-2011
deleting few lines from a file dynamically

here is the part of the code

Code:
var1="replicate-ignore-db"
var2="replicate-same-server-id"
var3="skip-slave-start"
var4="report-host"
var5="master-host"
var6="master-user"
var7="master-password"
var8="master-port"
#code below deleted paramters as above if exists in my.cnf
for i in 1 2 3 4 5 6 7 8
do
        echo "`sed "/$var$i/d" my.cnf`" > my.cnf
done

but the code is not working since i am trying to use variable var dynamically in for loop by using $var$i when i incremented everytime the variable name is changed.... how to make this work... plz help me out here,,,,, SmilieSmilie
# 2  
Old 12-07-2011
Try this...
Code:
#!/bin/bash
var1="replicate-ignore-db"
...
var8="master-port"
#code below deleted paramters as above if exists in my.cnf
for i in $(seq 1 8)
do
        val=$( eval eval echo \$var$i )
        sed -i "/$val/d" my.cnf
done

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 3  
Old 12-07-2011
oh thanks your code is working.. :-)
similarly i have one more doubt...

hre is the part of the code
Code:
#code below adds the above paramters to my.cnf
no=28
for i in $(seq 1 8)
do
        no=`expr $no + 1`
        val2=$( eval eval echo \$variable$i )
        sed -i "$noi $val2" my.cnf
done

here the variable1, variable2 etc etc has some content which is added to my.cnf file after line 28.. so here i am trying to add them dynamically... but its givine the error as below..

Code:
sed: -e expression #1, char 17: unterminated `s' command
sed: -e expression #1, char 3: extra characters after command
sed: -e expression #1, char 2: unknown command: `m'
sed: -e expression #1, char 2: unknown command: `m'
sed: -e expression #1, char 2: unknown command: `m'

# 4  
Old 12-07-2011
Code:
...
sed -i "$no i $val2" my.cnf
...

Note the space between $no and i or
Code:
...
sed -i "${no}i $val2" my.cnf
...

HTH
--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 5  
Old 12-07-2011
Thanks a lot.. :-) its working...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue in Concatenation/Joining of lines in a dynamically generated file

Hi, I have a file containing many records delimited by pipe (|). Each record should contain 17 columnns/fields. there are some fields having fields less than 17.So i am extracting those records to a file using the below command awk 'BEGIN {FS="|"} NF !=17 {print}' feedfile.txt... (8 Replies)
Discussion started by: TomG
8 Replies

2. Shell Programming and Scripting

appending lines to a file dynamically

Hi all, I have a file with some number of lines. I need to add certain number of lines from another file which may vary according to the user's input and to it. eg code: I/P file 1 apps/file/xyz apps/file/abc apps/file/def file 2 progs/file/xyz ... (2 Replies)
Discussion started by: Ananthdoss
2 Replies

3. Shell Programming and Scripting

Extracting few lines from a file based on identifiers dynamically

i have something like this in a file called mysqldump.sql -- -- Table structure for table `Table11` -- DROP TABLE IF EXISTS `Table11`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `Table11` ( `id` int(11) NOT NULL... (14 Replies)
Discussion started by: vivek d r
14 Replies

4. Shell Programming and Scripting

deleting the lines at the end of the file.

I have a text file with two coulmn first column is just used in to show the line number, these line number are not there in the real file. I want to delete the line 16(in this file) here, even tough there is no data inside it . this empty line is causing me a problem by throwing me garbage... (12 Replies)
Discussion started by: shashi792
12 Replies

5. Shell Programming and Scripting

deleting lines from file

We have a server that logs transactions to a file. I want to write a script that will delete the first 50 lines of the file daily without renameing the file or moving the file. (8 Replies)
Discussion started by: daveisme
8 Replies

6. UNIX for Advanced & Expert Users

Deleting lines from a file

How I can delete 100 lines anywhere in a file without opening a file and without renaming the file. (11 Replies)
Discussion started by: Nirgude07
11 Replies

7. UNIX for Dummies Questions & Answers

Deleting whole lines from a file

I have a file with 65 sets of 35 coordinates, and would like to isolate these coordinates so that I can easily copy the coordinates to another file. The problem is, I've got a 9 line header before each set of coordinates (so each set is 44 lines long). There are a zillion threads out there about... (3 Replies)
Discussion started by: red baron
3 Replies

8. Shell Programming and Scripting

Deleting lines in a file

How do I delete all the lines after the line containing text ***DISCLOSURES*** . I want to delete this line too. Thank you (2 Replies)
Discussion started by: reachsamir
2 Replies

9. Shell Programming and Scripting

Deleting last 2 lines from the file.

Hi I have a file & always I need to remove or delete last 2 lines from that file. So in a file if I have 10 lines then it should return me first 8 lines. Can someone help me? (4 Replies)
Discussion started by: videsh77
4 Replies

10. Shell Programming and Scripting

Deleting Lines from .csv file

Hello All, I have a .csv file and I have to delete the selcted records stored in a vairable e.g echo $lname 7 88 91 94 97 100 103 106 I dont know how to pass the variable name to "sed" for deleting the $lname from a file can any one help as this is very urgent. $lname is changing the... (3 Replies)
Discussion started by: 009satya
3 Replies
Login or Register to Ask a Question