adding the data at a specified location in a file....


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting adding the data at a specified location in a file....
# 1  
Old 10-24-2008
adding the data at a specified location in a file....

Hi all,
I m new to shell programming..Can anyone please guide me how to insert data at a specified location in the file..
I have a configuration file..I want to add data to it through script..I am able to do it...I get that data written at end of my configuration file..I want data to be placed at a specified location...
As,I have a header [mydata] in my configuration file..I want to place the data here...
Is it possible to do that...
Example:
My configuration file:
[global]
hello
aaa
bbbb
[share]
gghg
hnnmm
bbb
[mydata]
gggg
[printing]
cvbvbb
vbvbb
Now,I want my data to be entered after header [mydata], not at the end of Configuration file...

Thanking you...
# 2  
Old 10-24-2008
Try this:
Code:
awk -v dat="Yourdata" '/\[share\]/{print;print dat;next}1' file > newfile

Regards
# 3  
Old 10-24-2008
Code:
sed '/\[mydata\]/a\all i ever wanted\nall i ever needed\nis here in my arms' infile
[global]
hello
aaa
bbbb
[share]
gghg
hnnmm
bbb
[mydata]
all i ever wanted
all i ever needed
is here in my arms
gggg
[printing]
cvbvbb
vbvbb

# 4  
Old 10-24-2008
Hi,
Thanks so much..This has helped me to understand the concept...
Actually,This is my script which inserts the data into the configuration file..

#!/bin/bash
CF="/etc/samba/smb-test.conf" //Configuration file
.....................
...............

insert_mod() { //Function to insert data into config file
NCF="$CF.$$"
[ ! -f $CF ] && touch $CF || :
V="$1"
grep -v -E "^$V=" $CF > $NCF
mv -f $NCF $CF
eval echo "$V\"'\${$V}'\"">> $CF

}
hello=yes
insert_mod hello //Function called with argument
else
illegal
fi

...............
SO,HOW CAN I USE sed COMMAND HERE....

Thanking you in advance...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding a comment in a file next to data

Dear Gurus, I have a file which comes every day with set of data, as a part of processing i want to add a comment at the start of every line. e.g of file <PCL> 2E;"HCA";"COP Car A";"ODBS_CFG" 7C;"DD";"Doors Car D";"ODBS_CFG" 3D;"XA";"Auxiliary Car A";"ODBS_CFG" 3E;"XB";"Auxiliary... (12 Replies)
Discussion started by: guddu_12
12 Replies

2. Shell Programming and Scripting

Adding lines at a particular location in a file.

Hi Experts, Let us take a text file,say items.txt having the following data jar bottle gum tube cereal bag I want to add the content of items.txt to another file say #many lines not necessary ingredients #many line not necesary ingredients I want to append the data in... (3 Replies)
Discussion started by: Pradeep_1990
3 Replies

3. Shell Programming and Scripting

Adding data from a file based on some condition

I collect data in a file in below format(Month Day Year Size) in RedHat Linux. Now i want to calculate the data size date wise. As i code shell script after long time, i forgot the features and syntax. Can anyone help me regard this please. Feb 8 2014 15 Feb 10 2014 32 Feb 10 2014 32 Feb 12... (2 Replies)
Discussion started by: makauser
2 Replies

4. Shell Programming and Scripting

How to find a existing file location and directory location in Solaris box?

Hi This is my third past and very impressed with previous post replies Hoping the same for below query How to find a existing file location and directory location in solaris box (1 Reply)
Discussion started by: buzzme
1 Replies

5. Red Hat

Adding a data file

I need to add a data file, but my user account doesn't have privileges. I have connected to the oracle as sqlplus / as sysdba and it is telling me I'm connect to an idle instance. How can I connect to my database and create a data file if I don't know the passwords of any of the account that have... (1 Reply)
Discussion started by: FaSho76
1 Replies

6. Shell Programming and Scripting

Help in adding a data after a particular line of data in a file.

Hi.. I'm into a bump after trying to solve this prob.. i've a file with contents like below. <blankline> 'pgmId' : 'UNIX', 'pgmData' : 'textfile', 'author' : 'admin', ....... Now i'm trying to insert a new data after pgmId. so the final output will be... (7 Replies)
Discussion started by: arjun_arippa
7 Replies

7. Shell Programming and Scripting

Adding the data before file extension

HI , I have a file with multiple lines like below, I need to check on column starting with #prototype if it has the .Number.txt extension do nothing else add 1.txt extension. Source data Jdbc_app_data :- sample data #prototype= sample data from test.1.txt application_id=122135... (4 Replies)
Discussion started by: gaur.deepti
4 Replies

8. Shell Programming and Scripting

Adding data in a file on same line

Hi, I have one file a.txt ,the contents of the file is A B C D E F and I have another file b.txt, the contents of the file is 1 2 3 4 5 6 now when I am using this command cat a.txt b.txt > c.txtI am getting the output as A B C D E F 1 2 3 4 5 6 but i need the output... (2 Replies)
Discussion started by: prarat
2 Replies

9. Shell Programming and Scripting

Read Write byte range/chunk of data from specific location in file

I am new to Unix so will really appreciate if someone can guide me on this. What I want to do is: Step1: Read binary file - pick first 2 bytes, convert from hex to decimal. Read the next 3 bytes as well. 2 bytes will specify the number of bytes 'n' that I want to read and write... (1 Reply)
Discussion started by: Kbenipel
1 Replies

10. Shell Programming and Scripting

Need Help for Adding Three new columns in existing file from fatching data from file

not required this time (36 Replies)
Discussion started by: Sandeep_Malik
36 Replies
Login or Register to Ask a Question