ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have two files(ASCII),One file(which i want to edit) will have some string like "change this"(will be within quotes) and another file will have the the value which will replace "change this" in the first file(it will be in format like "change this","replacement"),the string "replacement" will be added to the first file in place of "change this".
Any idea how can this be acheived using shell scripts???
even if anyone knows how can i edit the first file/add a string to it without using the second file can also post a reply
thanks for the reply
"change this" wont be same everytime
this is what i want to do
file 1,the one which has to be edited will have text as follows
DEFINE F50T
BEGIN
TP = 5
TI = 35
TN = 745
TV = <<"Admin Catalog">>
END
DEFINE F50T
BEGIN
TP = 5
TI = 36
TN = 745
TV = <<"Active Description">>
END
DEFINE F50T
BEGIN
TP = 5
TI = 37
TN = 745
TV = <<"Comments">>
END
in this file text like "Comments"(everything in <<"">> has to be replaced by corespoinding text in file 2)
FILE 2 where i have the replacement will have text like this
Admin Catalog,Admin Catalog1
Active Description,Active Description1
Comments,Comments1
--<<"Admin Catalog">> to be replaced by <<"Admin Catalog1">> in file1 and so on
I have heard that sed and awk can be used to edit files but dunno how can i use them in my script,any method which can do this in a script is fine for me
Jim's elegant solution made me think of how I could do the same thing, except reduce the passes over the data-file to just one (like Patton, I hate paying for the same real estate twice ).
Here's my version of Jim's script:
Code:
#!/bin/sh
# @(#) s2 Demonstrate building command file for sed.
rm -f commands
oldifs="$IFS"
IFS=,
while read old new
do
echo "s/$old/$new/" >> commands
done <file2
IFS="$oldifs"
sed -f commands file1
Which produces:
Code:
% ./s2
DEFINE F50T
BEGIN
TP = 5
TI = 35
TN = 745
TV = <<"Admin Catalog1">>
END
DEFINE F50T
BEGIN
TP = 5
TI = 36
TN = 745
TV = <<"Active Description1">>
END
DEFINE F50T
BEGIN
TP = 5
TI = 37
TN = 745
TV = <<"Comments1">>
END
So in place of n passes over the data file, this collects the created sed commands, and then runs sed over the file once.
The saving and restoring of IFS is probably not necessary, but sometimes I cannot stop my fingers
The credit goes to Jim for the original solution ... cheers, makyo
I am facing one small issue while using your script.
The output generated shows the required file output,but when i open the file1 in vi editor,i see that it remains unchanged
any idea what can be the reason for this???
also what exactly is purpose of
"# @(#) s2 Demonstrate building command file for sed."
and whats the 's2'(% ./s2) used in the output file you have displayed for your script
I am facing one small issue while using your script.
The output generated shows the required file output,but when i open the file1 in vi editor,i see that it remains unchanged
any idea what can be the reason for this???
also what exactly is purpose of
"# @(#) s2 Demonstrate building command file for sed."
and whats the 's2'(% ./s2) used in the output file you have displayed for your script
Thanks
Rohit
The script, like sed itself, writes to STDOUT, it does not modify files in place. There are situations when that is useful, but my practice is to avoid over-writing original files while I am experimenting. You can save the output to another file, view it, and then save over the original if you desire.
The line "# @(#) s2 ..." is a what line. It's obviously a comment, but was originally used by sccs module what to help identify the brief purpose of programs. (The sccs system is a very old source control system, succeeded by RCS, cvs, subversion, etc.) I use those comments to generate indices of scripts -- I have several hundred scripts, shell, perl, awk, etc. -- too many to keep track of without such an aid.
The "% ./s2" shows you how the script, "s2" was called into execution from the current directory, "./s2", and the "%" is the prompt.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.