The UNIX and Linux Forums  


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
can someone help me with modifying this file eamani_sun Shell Programming and Scripting 2 05-22-2008 02:15 PM
Modifying a csv file from Shell Script not4google Shell Programming and Scripting 2 11-21-2006 06:47 AM
Modifying the final output file charbel Shell Programming and Scripting 5 10-10-2006 02:36 PM
Modifying the URL to point to another location in a .sh UNIX file pjanakir UNIX for Dummies Questions & Answers 6 01-25-2006 03:19 PM
Modifying binary file by editing Hex values ? Nicol UNIX for Advanced & Expert Users 4 11-04-2005 08:25 AM

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

Join Date: Oct 2007
Posts: 19
Help need in modifying the text of .txt file

Hi All,

I've written a shell script in which i defined two varibles

for example:

str=1.0.0.15
timeStamp=2008.03.08

now using this varibles i need to modify a text file.

The text content looks like this

************************
* packageNumber : 1.0.0.14 *
* date : 2008.02.08 *
************************

after executing the shell script, the text must look like this

************************
* packageNumber : 1.0.0.15 *
* date : 2008.03.08 *
************************


Could anybody help me in resolving this issue?

Thanks,
Vinna
  #2 (permalink)  
Old 07-17-2008
FunibonE FunibonE is offline
Registered User
  
 

Join Date: Jul 2008
Location: Silicon Forest
Posts: 16
Smile

I'm sure someone will have a better way of achieving this but here's one version:

~/scripts/play % cat file1
************************
* packageNumber : 1.0.0.14 *
* date : 2008.02.08 *
************************


~/scripts/play % cat aa

Code:
#!/bin/sh

str=1.0.0.15
timeStamp=2008.03.08
file=${HOME}/scripts/play/file1

rep1=`grep packageNumber ${file} | awk '{print $4}'`
rep2=`grep date ${file} | awk '{print $4}'`

sed -e 's/'"${rep1}"'/'"${str}"'/' -e 's/'"${rep2}"'/'"${timeStamp}"'/' $file > ${file}.tmp
mv ${file}.tmp $file

~/scripts/play % aa
~/scripts/play % cat file1
************************
* packageNumber : 1.0.0.15 *
* date : 2008.03.08 *
************************
~/scripts/play %
  #3 (permalink)  
Old 07-17-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Cool Made a couple of assumptions, but it works

Assumed that the packageNumber was always eight characters and date was always ten characters in length. Hence the 8 and 10 in my command below.


Code:
> cat sample2
************************
* packageNumber : 1.0.0.14 *
* date : 2008.02.08 *
************************
> echo $str
1.0.0.15
> echo $timeStamp
2008.03.08
> cat sample2 | sed "s/date : .\{10\}/date : $timeStamp/" | sed "s/packageNumber : .\{8\}/packageNumber : $str/"
************************
* packageNumber : 1.0.0.15 *
* date : 2008.03.08 *
************************

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 04:55 AM.


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