LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-28-2006, 01:25 AM   #1
rohit2983
LQ Newbie
 
Registered: Dec 2006
Posts: 5

Rep: Reputation: 0
Editing Ascii File Using Shell Script


Hi Gurus

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 in advance for your help

Regards
 
Old 12-28-2006, 02:36 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Is there some reason you cannot use sed?
Is "change this" going to be the same string every time? ...if so
Code:
replacement=`cat file1`
sed #s#\"change this\"#\"$replacement\"#g# file2 > newfile
mv newfile file2
What are you trying to do exactly?
 
Old 12-28-2006, 02:53 AM   #3
rohit2983
LQ Newbie
 
Registered: Dec 2006
Posts: 5

Original Poster
Rep: Reputation: 0
Hi Jim

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
 
Old 12-28-2006, 10:42 AM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
try something like this:
Code:
#!/bin/ksh

while IFS=, && read old new
do    
	sed -i 's/'$old'/'$new'/g' file1
done < file2
 
Old 12-28-2006, 11:22 AM   #5
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

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

Last edited by makyo; 12-28-2006 at 11:44 AM.
 
Old 01-02-2007, 05:24 AM   #6
rohit2983
LQ Newbie
 
Registered: Dec 2006
Posts: 5

Original Poster
Rep: Reputation: 0
Hi All

thanks a lot for your help

Rohit
 
Old 01-03-2007, 04:21 AM   #7
rohit2983
LQ Newbie
 
Registered: Dec 2006
Posts: 5

Original Poster
Rep: Reputation: 0
file not getting edited

Hi 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

Thanks
Rohit
 
Old 01-03-2007, 06:27 AM   #8
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi, Rohit.
Quote:
Originally Posted by rohit2983
Hi 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

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.

Best wishes ... cheers, makyo

Last edited by makyo; 01-06-2007 at 05:49 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help no editing a file through a script procfs Programming 7 07-19-2006 05:59 AM
Editing text file in Shell - Konsole (SuSE 9.1) Ree Linux - Newbie 5 10-03-2004 11:48 AM
Editing a text file in shell Lotmr Linux - Software 5 06-24-2003 12:12 PM
ID3 Tag Editing with a Shell Script chrisk5527 Linux - General 5 03-20-2003 10:38 PM
file editing in a bash script Harpune Programming 4 11-22-2002 11:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration