The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to search and replace text in same file Vrgurav Shell Programming and Scripting 1 04-25-2008 06:20 AM
search and replace in a file satyam_sat Shell Programming and Scripting 2 04-11-2008 01:28 AM
Perl: Search for string on line then search and replace text Crypto Shell Programming and Scripting 4 01-04-2008 10:24 AM
Search and replace multi-line text in files marz Shell Programming and Scripting 10 10-10-2005 08:05 AM
How do you search and replace a text with markerA that end with markerB drone Shell Programming and Scripting 1 06-19-2003 09:12 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-27-2008
ommatidia ommatidia is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 2
automating file search and replace text

Hi, I am trying something like this: Let's say I have a file called File1 with contents:
x=-0.3
y=2.1
z=9.0

I have another file, File2, with contents:

xx=
yy=
zz=

(nothing after "="). What I want to do is get the value of x in File1 and set it to xx in File2, i.e., xx=-0.3. And the same thing for y and z, value of y will be set to yy and z to zz (yy=2.1, zz=9.0).

Now, I want to do this with grep and sed and without using any other
language. Also, I cannot know the numbers in File1. The process should be automatic. Is there a way to do this in the command line itself? Only one step is enough, for example, x in File1 to xx in File2 step. thanks,
  #2 (permalink)  
Old 02-27-2008
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
Try using awk instead of grep and sed, e.g...
Code:
$ head file[12]
==> file1 <==
x=-0.3
y=2.1
z=9.0

==> file2 <==
xx=
yy=
zz=

$ awk 'BEGIN{FS=OFS="="}NR==FNR{a[$1 $1]=$2;next}{print $1,a[$1]}' file1 file2
xx=-0.3
yy=2.1
zz=9.0
  #3 (permalink)  
Old 02-28-2008
chella chella is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 75
Hi,

Another way of doing this would be using grep and cut commands inside a loop.

Regards,
Chella
  #4 (permalink)  
Old 02-28-2008
ommatidia ommatidia is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 2
Ygor,

I am not familiar with awk, when I tried your suggestion, I got this:

x=
y=
z=
xx=
yy=
zz=

What can be the issue?
thanks,
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:50 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0