LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-20-2012, 02:13 AM   #1
rchapman
LQ Newbie
 
Registered: Nov 2006
Posts: 8

Rep: Reputation: 0
Linux scripting for text editing... help needed


I have several hundred map calibration files - that for historical reasons contain a systematic error. The algorithm for fixing them is very simple - but it is very tedious to do it manually. I think awk and/or sed can probably help...

I need to take the two numbers in the 3rd and 4th columns of line 11 of each file - and copy it to columns 3 and 4 of line 56 - replacing the numbers currently in that position.

The files are comma delimited text files - and the relevant lines have unique text in them if that is helpful for searching. Here is an example of a file containing the error.
----------------
OziExplorer Map Data File Version 2.2
SE5308 Calvert Hills
\\C5\Backups\PDA Storage card\Maps\Natmap 2003 CD1\osf\se5308.ozf2
1 ,Map Code,
Australian Geocentric 1994 (GDA94),, 0.0000, 0.0000,WGS 84
Reserved 1
Reserved 2
Magnetic Variation,,,E
Map Projection,(UTM) Universal Transverse Mercator,PolyCal,No,AutoCalOnly,No,BSBUseWPX,No
Point01,xy, 0, 0,in, deg, , ,, , ,, grid, 53, 601927.125, 8126091.88,S
Point02,xy, 7134, 4022,in, deg, , ,, , ,, grid, 53, 828431.625, 7998393.38,S
Point03,xy, , ,in, deg, , ,, , ,, grid, , , ,
Point04,xy, , ,in, deg, , ,, , ,, grid, , , ,
Point05,xy, , ,in, deg, , ,, , ,, grid, , , ,
Point06,xy, , ,in, deg, , ,, , ,, grid, , , ,
Point07,xy, , ,in, deg, , ,, , ,, grid, , , ,
Point08,xy, , ,in, deg, , ,, , ,, grid, , , ,
Point09,xy, , ,in, deg, , ,, , ,, grid, , , ,
Point10,xy, , ,in, deg, , ,, , ,, grid, , , ,
Point11,xy, , ,in, deg, , ,, , ,, grid, , , ,
Point12,xy, , ,in, deg, , ,, , ,, grid, , , ,
Point13,xy, , ,in, deg, , ,, , ,, grid, , , ,
Point14,xy, , ,in, deg, , ,, , ,, grid, , , ,
Point15,xy, , ,in, deg, , ,, , ,, grid, , , ,
Point16,xy, , ,in, deg, , ,, , ,, grid, , , ,
Point17,xy, , ,in, deg, , ,, , ,, grid, , , ,
Point18,xy, , ,in, deg, , ,, , ,, grid, , , ,
Point19,xy, , ,in, deg, , ,, , ,, grid, , , ,
Point20,xy, , ,in, deg, , ,, , ,, grid, , , ,
Point21,xy, , ,in, deg, , ,, , ,, grid, , , ,
Point22,xy, , ,in, deg, , ,, , ,, grid, , , ,
Point23,xy, , ,in, deg, , ,, , ,, grid, , , ,
Point24,xy, , ,in, deg, , ,, , ,, grid, , , ,
Point25,xy, , ,in, deg, , ,, , ,, grid, , , ,
Point26,xy, , ,in, deg, , ,, , ,, grid, , , ,
Point27,xy, , ,in, deg, , ,, , ,, grid, , , ,
Point28,xy, , ,in, deg, , ,, , ,, grid, , , ,
Point29,xy, , ,in, deg, , ,, , ,, grid, , , ,
Point30,xy, , ,in, deg, , ,, , ,, grid, , , ,
Projection Setup,,,,,,,,,,
Map Feature = MF ; Map Comment = MC These follow if they exist
Track File = TF These follow if they exist
Moving Map Parameters = MM? These follow if they exist
MM0,Yes
MMPNUM,4
MMPXY,1,1819,199
MMPXY,2,6851,253
MMPXY,3,6796,3745
MMPXY,4,1792,3681
MMPLL,1, 136.500010, -17.000543
MMPLL,2, 137.999941, -16.999450
MMPLL,3, 137.999951, -18.000490
MMPLL,4, 136.500136, -17.999562
MM1B,31.749907
MOP,Map Open Position,0,0
IWH,Map Image Width/Height,2500,1395
-------------------

In this case the last line should be corrected to read:

IWH,Map Image Width/Height,7134, 4022

Is anyone able to get me started with some awk or sed commands?

Thanks

Richard.
 
Old 08-20-2012, 02:27 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Would this help:
Code:
awk 'BEGIN{ FS="," ; OFS=","} {if ( NR == 11) { x = $3 ; y = $4 } if ( NR == 56 ) { print $1,$2,x,y ; next } { print }}' infile
 
Old 08-20-2012, 03:26 AM   #3
rchapman
LQ Newbie
 
Registered: Nov 2006
Posts: 8

Original Poster
Rep: Reputation: 0
Hey... That is fantastic....
Is there a way to now make that edit the file "in place" and edit (say) all files in the current directory?
 
Old 08-20-2012, 05:25 AM   #4
rchapman
LQ Newbie
 
Registered: Nov 2006
Posts: 8

Original Poster
Rep: Reputation: 0
Or to ask the question another way - how do I redirect standard out from the above edit - to be the same as the input file..... or to ask it yet another way - how do I make the above edit work on a set of input files - with the output going to eh same (or a related) set of files.

Thanks Druuna

Richard.
 
Old 08-20-2012, 06:32 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Assuming that there are no sub-directories present in the directory you execute this from:
Code:
 for FILE in *; do awk 'BEGIN{ FS="," ; OFS=","} {if ( NR == 11) { x = $3 ; y = $4 } if ( NR == 56 ) { print $1,$2,x,y ; next } { print }}' "${FILE}" > "${FILE}.new"; done
The above leaves the original file as-is and creates a new file extended with .new (original.file.name.new).
 
Old 08-20-2012, 06:46 AM   #6
rchapman
LQ Newbie
 
Registered: Nov 2006
Posts: 8

Original Poster
Rep: Reputation: 0
Fantastic... Thanks .... Your help is really appreciated...
 
  


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
Text editing: Adding a digit/text to the end of a row CHARL0TTE Linux - Newbie 13 07-16-2009 06:44 AM
Notepad++ or similar php coding/text editing tool for linux milasch Linux - Software 4 01-03-2008 01:41 PM
Text Editing between Linux & Windows elfoozo Linux - Newbie 3 07-24-2006 12:07 PM
how to change some text of a certain line of a text file with bash and *nix scripting alred Programming 6 07-10-2006 11:55 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:42 PM.

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