modify the test file by any script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting modify the test file by any script
# 1  
Old 10-13-2012
modify the test file by any script

Hi All the Helpers!

I have a text file which looks like input.txt.I would request to please suggest me how can I make this file look like output.txt

input.txt

Code:
VOP 111 0 1 2
DEM 111 0 222 333 444 555
DEM       879 888 987 888 989
DEM       879 888 987 888 989      

VOP 118 0 12 3 6
DEM 118 0 333 555 768 946
DEM       879 888 987 888 989
DEM       879 888 987 888 989

output.txt

Code:
VOP 0001 0 1 2
DEM 0001 0 222 333 444 555
DEM       879 888 987 888 989
DEM       879 888 987 888 989

VOP 0002 0 12 3 6
DEM 0002 0 333 555 768 946
DEM       879 888 987 888 989
DEM       879 888 987 888 989

So I need that the second column value get replaced by 0001,0002,0003 and goes on with incremantal 0001,but only for VOP and the 1st DEM after VOP.

Your help appreciated as always!!
Best Regards

Last edited by Scrutinizer; 10-13-2012 at 04:30 AM.. Reason: code tags
# 2  
Old 10-13-2012
Try this:
Code:
awk '
/^VOP/ {$2 = sprintf("%04d", ++cnt)
        print
        c1 = 1
        next
}
c1 && /^DEM/ {
        $2 = sprintf("%04d", cnt);
        c1 = 0
}
{       print}' input.txt > output.txt


Last edited by Don Cragun; 10-13-2012 at 04:52 AM.. Reason: Updated input and output filenames
# 3  
Old 10-13-2012
And what was your other thread about then?
# 4  
Old 10-13-2012
Hi Senari,
The input file is bit modified.The last script was doing like for all the lines with DEM.But now I want it only for the line with DEM just following the VOP.
Any idea for a simple script for this...appreciated as always
# 5  
Old 10-13-2012
Quote:
Originally Posted by Indra2011
The input file is bit modified.The last script was doing like for all the lines with DEM.But now I want it only for the line with DEM just following the VOP.
Why you need to create special thread for very small change in requirement.
You can add those requirements to the same thread. Smilie
# 6  
Old 10-13-2012
Ok sorry pamu.....please help me
# 7  
Old 10-13-2012
Quote:
Originally Posted by Indra2011
Ok sorry pamu.....please help me
Don Cragun already answered your question..Smilie
Please check post 2.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl script to modify csv file

Hi Friends, I want to convert a csv file into a ordinary .txt file. I am able to convert but I want the output to look as shown below in the .txt file table findhost= { {"xyz","abc"}, {"rxz","mmz"}, {"vrr","nnz"}, } default={"NONE"} My current perl script #!/usr/bin/env perl... (12 Replies)
Discussion started by: dbashyam
12 Replies

2. Shell Programming and Scripting

Modify the text file by script

Hi All the Helpers! I have a text file which looks like input.txt.I would request to please suggest me how can I make this file look like output.txt input.txt VOP 111 0 1 2 DEM 111 0 222 333 444 555 879 888 987 888 989 VOP 118 0... (2 Replies)
Discussion started by: Indra2011
2 Replies

3. Shell Programming and Scripting

Modify the file by script

Hi All, I have an input file like below, 6984 1225 6989 1220 6994 1214 ... (3 Replies)
Discussion started by: Indra2011
3 Replies

4. Shell Programming and Scripting

Shell script to modify file in several directories

Hi, I want a script shell to automate modifying httpd.conf file for several instances of apache, save httpd.file before changing it, after modifying it and then restart apache. - Replace ServerRoot "xxxx" by ServerRoot "yyyy" of all directories : "... (4 Replies)
Discussion started by: bras39
4 Replies

5. Shell Programming and Scripting

Modify text file using shell script

Hi, I have a text file which is following format - COL VAL ABC 1 ABC 2 ABC 3 ABC 4 ABC 5 My requirement is to search for a particular value (provided by user) in the file and comment the previous entries including that as well. E.g. If I search for number 3, then the output... (6 Replies)
Discussion started by: bhupinder08
6 Replies

6. Shell Programming and Scripting

How to modify the contents of file using script

Hi, Can anyone pls let me know how can i modify the file contents thru script. Eg. I have file abc.dat that contains below lines Merge.resync.cycleFlag Merge.resync.logFlag Merge.resync.maxByteRate Merge.resync.maxSearch Merge.resync.rate Merge.resync.tickLog ... (2 Replies)
Discussion started by: sdosanjh
2 Replies

7. Shell Programming and Scripting

Simple script to modify kickstart file

Hi, I would like to create a script so that it will ask me the following: 1) What is the ip address? 2) What is the gateway address? 3) What is the hostname? and then put the answer to the below kickstart file (kickstart.cfg) Here I included the kickstart.cfg: # Kickstart file... (9 Replies)
Discussion started by: beeloo
9 Replies

8. Shell Programming and Scripting

Need to modify a file of different username through script.

Hi ! All I want to write a script where, it will open a new shell with a username / pwd and modify a file of same username and exit. example: 1. UserA 2. UserB- FileB ScriptA -> su UserB -> Modify FileB -> Exit ScriptA Can somebody give me a direction , on how to... (2 Replies)
Discussion started by: dashok.83
2 Replies

9. Shell Programming and Scripting

how to modify a file using shell script

Hi, i am using SuonOS and ksh. i need to add data into a file(s.txt) using a shell script. i have to pass 3 parameters and these 3 paramaters should add into the file at end of the file. File s.txt is look like, --------------------------------- column1|column2|column3 ... (1 Reply)
Discussion started by: syamkp
1 Replies

10. Shell Programming and Scripting

Modify script to generate a log file

I've seen several examples of scripts in thise forum about having a script generate a log file. I have a script that is run from cron and that monitors a file system for a specfic filename(s) and then performs some actions on them. Normally I call this script from another script (which the one... (2 Replies)
Discussion started by: heprox
2 Replies
Login or Register to Ask a Question