How to replace a line content


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace a line content
# 1  
Old 04-17-2008
How to replace a line content

Hi Experts,


I have binary files contain an ID Line as:

:
:
$Header: FNDSCMON.fmb 115.6 2000/01/11 10:26:10 pkm ship$
:
:

where the ID Line format is:
$Header: <File_Name> <Version> <Last_update_date_time> pkm ship$

In this Example:
File_Name = FNDSCMON.fmb
Version = 115.6
Last_update_date_time = 2000/01/11 10:26:10


I want to write a script to update the ID Line as:
File_Name = the file name
Version = old_version + 0.1
Last_update_date_time = current date_time

so, the new ID line should be show as:
:
:
$Header: <the file name> <old_version + 0.1> <current date_time> pkm ship$
:
:

Can anyone help?

Thanks!


Victor Cheung

Last edited by victorcheung; 04-17-2008 at 02:11 AM..
# 2  
Old 04-17-2008
This should do the job:

Code:
#!/bin/sh

curr_dat=`date "+%Y\/%d\/%m"`
curr_time=`date "+%H:%M:%S"`

awk -v ver="old_version + 0.1" -v dat=$curr_dat -v t=$curr_time '
/^\$Header:/{$2=FILENAME;$3=ver;$4=dat;$5=t}1
' file

Regards
# 3  
Old 04-17-2008
Hi Frank,

Sorry, I think I have uncompleted mentions of my question, here is the complete one, can you please help? :

I have binary files contain an ID Line as:

:
:
$Header: FNDSCMON.fmb 115.6 2000/01/11 10:26:10 pkm ship$
:
:

where the ID Line format is:
$Header: <File_Name> <Version> <Last_update_date_time> pkm ship$

In this Example:
File_Name = FNDSCMON.fmb
Version = 115.6
Last_update_date_time = 2000/01/11 10:26:10


I want to write a script to update the ID Line as:
File_Name = the file name
Version = version + 0.1
Last_update_date_time = current date_time

so, the new ID line should be show as:
:
:
$Header: <the file name> <version + 0.1> <current date_time> pkm ship$
:
:


- The line starts with "$Header:" and ends with "pkm ship$"
- The new line length have to be kept as same as the old line length (the whitespaces is adjustable)
- perl should be used for the replacment action (The files I want to change are binary files, using other command, such as, sed will make the file unusable)

Can you help how to write the script?


Many Thanks!
Victor Cheung

Last edited by victorcheung; 04-18-2008 at 01:00 AM..
# 4  
Old 04-18-2008
In that case you have to provide more information, what is the exact length of the fields, beginning in what position?

Regards
# 5  
Old 04-21-2008
Hi Frank,

The exact length between the Start Pattern,$Header:, and the End Pattern,pkm ship$, is fixed to 80 characters,

e.g.1:
$Header: FNDSCMON.fmb 115.6 2000/01/11 10:26:10........................pkm ship$

could be changed as :

$Header: FNDSCMON.fmb 115.7 2008/04/21 13:29:09........................pkm ship$


or, e.g.2:
Other Word $Header: FNDSCMON.fmb 115.6 2000/01/11 10:26:10........................pkm ship$

could be changed as :

Other Word $Header: FNDSCMON.fmb 115.7 2008/04/21 15:36:50........................pkm ship$




p.s. please replace the .... as whitespaces, multi whitespaces will be trunced when posted to this forum, so I use .... instead.



Many Thanks!
Victor Cheung
# 6  
Old 04-21-2008
Try this:

Code:
#!/bin/sh

curr_dat=`date "+%Y\/%m\/%d"`
curr_time=`date "+%H:%M:%S"`

awk -v dat=$curr_dat -v t=$curr_time -v name="old_version" -v ver="0.1" '
/^\$Header:/{
$2=sprintf("%-12s",name)
$3=sprintf("%-5s",ver)
$4=sprintf("%-10s",dat)
$5=sprintf("%-31s",t)
}1
' file

Regards
# 7  
Old 04-21-2008
Code:
perl -pe 's!(\$Header: \S+ )(\d+\.\d+) (\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2})( +pkm ship\$)\n!
    my ($head, $ver, $date, $tail) = ($1, $2, $3, $4);
    my @now = gmtime();
    sprintf "%s %3.1f %04i/%02i/%02i %02i:%02i:%02i%s\n",
      $head, $ver+0.1, $now[5]+1900, $now[4]+1, $now[3], $now[2], $now[1], $now[0], $tail !e' file

... assuming you want the date stamp in GMT (you probably do, even if you think you don't).

The version will monotonously grow by 0.1 for each update; so after 115.9 you get 116.0, 116.1, etc. Hope you won't need to go beyond 999.9 because this code won't handle that (without wrecking the fixed-length constraint).

I don't have anything really to test this on, but report back if it doesn't work.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace Content

Hello all ; ) I'got a file1 with a lot of emails like : fistname.lastname@domaine1.comAnd another file2 with emails like fistname.lastname@domaine2.ct.netI need a shell script that will read each line from the file1 and try to find if in file2 the fistname.lastname exist. If yes, the... (1 Reply)
Discussion started by: Aswex
1 Replies

2. Shell Programming and Scripting

Help with replace comma with new line and repeat the content

Input file G A G A,T A C A G,T,C T C . . Desired Output file G A G A G T A C A G A T A C T C (4 Replies)
Discussion started by: perl_beginner
4 Replies

3. Shell Programming and Scripting

Mysql Content Replace

I have a wordpress site. I need add "post_title" to all my "post_content" mysql field. Thanks. my content: Free <b>Online Tips</b> output:Free post_title <b>Online Tips</b>: This code does not work: UPDATE wp_posts SET post_content = REPLACE ( post_content, 'Free <b>Online... (0 Replies)
Discussion started by: tara123
0 Replies

4. Shell Programming and Scripting

Help with replace all the content within ()

Hi, Below is my input file : AAAG(12) TC(14) AACCCT(66) AACCCT(30) AACCCT(18) AACCCT(48) TCTG(12) TCTG(20) TCTG(16) AC(12) AC(12) TCTG(16) TCTG(12) AC(12) AC(12) AC(12) AC(26) AC(14) AGTG(12) AC(24) AGTG(12) TCC(12) Desired output : AAAG TC AACCCT AACCCT AACCCT AACCCT TCTG TCTG... (4 Replies)
Discussion started by: perl_beginner
4 Replies

5. Shell Programming and Scripting

Sed: replace content from file with the content from file

Hi, I am having trouble while using 'sed' with reading files. Please help. I have 3 files. File A, file B and file C. I want to find content of file B in file A and replace it by content in file C. Thanks a lot!! Here is a sample of my question. e.g. (file A: a.txt; file B: b.txt; file... (3 Replies)
Discussion started by: dirkaulo
3 Replies

6. Shell Programming and Scripting

Help with replace data content

Format of one input file: # >length=1 seq program data 909 1992 seq program record 909 1190 Desired output result: # >length=1 length=1 program data 909 1992 length=1 program record 909 1190 I wanna to replace all the column 1 content (exclude the content start with "#") with the... (5 Replies)
Discussion started by: cpp_beginner
5 Replies

7. Shell Programming and Scripting

Reading several files and summing their content line-by-line

Hey, I am gettin a bit crazy with my script. I have several input datas with the same name (5.ill) in different folders (daysim_01, daysim_02, etc.). The 4. column of each of the data has to be summed with each other and then hass to be written in one new file. So file1: 1 1 0 1 2 1 1 2 ... (7 Replies)
Discussion started by: ergy1983
7 Replies

8. UNIX for Advanced & Expert Users

Search Parameter in first line and replace next line content

Hi, I need help. I have XML file as below &lt;a n=&quot;infoLevel&quot;&gt; &lt;v s=&quot;true&quot;/&gt; &lt;/a&gt; &lt;a n=&quot;localAddr&quot;&gt; &lt;v s=&quot;server.host.com&quot;/&gt; &lt;/a&gt; &lt;a n=&quot;ListenPort&quot;&gt; &lt;v s=&quot;21111&quot;/&gt; &lt;/a&gt; I need to find variable "ListenPort" in line and then replace... (4 Replies)
Discussion started by: rdtrivedi
4 Replies

9. Shell Programming and Scripting

search and replace the content

Hi All,sorry for inconvience....please find the attachement for my question i am unable to paste the question here....please help me in slving thisthanksk.k (4 Replies)
Discussion started by: G.K.K
4 Replies

10. Shell Programming and Scripting

How to replace a variable content

Hi, variable1="This is a car" Now I want to replace the content of variable1, "car" to "dog". Is there any simple command I can use. Thanks. Joseph (4 Replies)
Discussion started by: josephwong
4 Replies
Login or Register to Ask a Question