Google Answers Logo
View Question
 
Q: Edit text file to search/replace using NEDIT ( Answered 5 out of 5 stars,   1 Comment )
Question  
Subject: Edit text file to search/replace using NEDIT
Category: Computers > Programming
Asked by: drcwks-ga
List Price: $10.00
Posted: 05 Apr 2006 10:51 PDT
Expires: 05 May 2006 10:51 PDT
Question ID: 715765
I am using NEDIT. A text editor on a UNIX system. I need to know how
to globally change and Replace some text .

The following is an example of a few lines in the text file.

N55  LIN(370.855,28.920,-200.827,23.337,-0.183,0.000)  ;  *XYZ8544
N56  LIN(370.855,28.920,-200.827,23.337,-0.183,0.000)  ;  *XYZ756
N57  LIN(370.855,28.920,-200.827,23.337,-0.183,0.000)  ;  *XYZ1000
N58  LIN(370.855,28.920,-200.827,23.337,-0.183,0.000)  ;  *XYZ85
N59  LIN(370.855,28.920,-200.827,23.337,-0.183,0.000)  ;  *XYZ855
N60  LIN(370.855,28.920,-200.827,23.337,-0.183,0.000)  ;  *XYZ8
----------------------------text file can go all the way to N9999------------
_------------------------I?m just trying to show a break in the pattern.--------

N1009  LIN(370.855,28.920,-200.827,23.337,-0.183,0.000)  ;  *AXS1111
N1010  LIN(370.855,28.920,-200.827,23.337,-0.183,0.000)  ;  *AXS22
N1011  LIN(370.855,28.920,-200.827,23.337,-0.183,0.000)  ;  *AXS33
N1012  LIN(370.855,28.920,-200.827,23.337,-0.183,0.000)  ;  *AXS44
N1013  LIN(370.855,28.920,-200.827,23.337,-0.183,0.000)  ;  *AXS776
N1014  LIN(370.855,28.920,-200.827,23.337,-0.183,0.000)  ;  *AXS987

The first 1-5 columns need to be globally deleted or replaced with
just spaces. I need all the text between and including the LIN all
the way to the last parenthesis. THEN I NEED to globally delete
all the text AFTER the semi-colon. So as an example the first line
would look like LIN(370.855,28.920,-200.827,23.337,-0.183,0.000)
I guess I would like the semicolon to be deleted also. The text AFTER
the ; is random. 

NOTE: There is a SPACE before and after the semi-colon. I guess 
a search for ; semi-colon space would work as I have other semi-colons elsewhere
in the program that I would not want to modify but those do not have spaces as
the pattern described.

Also. The line numbers start with N1, N2, N3?N9, N10,N11, etc. So you would
Have to establish a pattern to replace or delete those characters too.

Let me know  if you need additional information.

What I am trying to do is compare 2 files using XDIFF and all I care about
are the characters after the Nxxxspace?..and space;*XYZ9878s

Of course I can manually delete the items above by columns holding the
ctrl key down
But that takes too long. I want a simple search and replace ALL command.

Clarification of Question by drcwks-ga on 12 Apr 2006 22:09 PDT
Sorry for the delay. I?m just now able to get access to a unix work station.

I don?t have a lot of UNIX programming experience. Let?s say the text
file I want to modify is called ?file1?  Do I need make a script file
of the terms you described and apply it to ?file1? ?

Request for Question Clarification by maniac-ga on 13 Apr 2006 19:08 PDT
Hello Drcwks,

Would you be willing to consider a command line solution using a
different tool such as ed, sed, or awk?
I would also produce a short shell script to help automate the steps /
reduce the chance of error.

If so, what type of Unix system are you using? Something like the output from
  uname -a
should provide a description of your system. Also provide the output
from a command like
  which ed
(or which sed, which awk) to confirm the other applications exist on your system.

Thanks.
  --Maniac

Clarification of Question by drcwks-ga on 14 Apr 2006 17:18 PDT
Hello Maniac,

The unix OS is IRIX64. I think the version is 6.5. I did a 'man' on the
commands you described and they are available so I'm assumming that
would work. I need the script to work as a stand alone in my
directories. I don't have control of the system files, etc.... But if
the commands work at my level I think everything would work okay.
Would this work on AIX OS too? If not that's okay...IRIX64 are where
the files reside. Let me know if you need any other
information.

Thanks, 

drcwks
Answer  
Subject: Re: Edit text file to search/replace using NEDIT
Answered By: maniac-ga on 16 Apr 2006 16:25 PDT
Rated:5 out of 5 stars
 
Hello Drcwks

Ok, I am familiar with SGI systems running Irix. I at the end of the
answer, I have attached a short shell script (one executable line
actually) that does the conversion you asked for. Using the sample
input you provided, I ran the script to get the output

LIN(370.855,28.920,-200.827,23.337,-0.183,0.000)
LIN(370.855,28.920,-200.827,23.337,-0.183,0.000)
LIN(370.855,28.920,-200.827,23.337,-0.183,0.000)
LIN(370.855,28.920,-200.827,23.337,-0.183,0.000)
LIN(370.855,28.920,-200.827,23.337,-0.183,0.000)
LIN(370.855,28.920,-200.827,23.337,-0.183,0.000)
LIN(370.855,28.920,-200.827,23.337,-0.183,0.000)
LIN(370.855,28.920,-200.827,23.337,-0.183,0.000)
LIN(370.855,28.920,-200.827,23.337,-0.183,0.000)
LIN(370.855,28.920,-200.827,23.337,-0.183,0.000)
LIN(370.855,28.920,-200.827,23.337,-0.183,0.000)
LIN(370.855,28.920,-200.827,23.337,-0.183,0.000)

which appears to be what you've asked for. If this is NOT correct,
please make a clarification request indicating what's not correct and
I'll fix the script. If there are lines that don't match the pattern,
they should be ignored.

To create the script / and run it, I suggest copying the text - as is
- into a file on your system. Using nedit should be fine. I named my
copy drcwks.sh. After saving the file, use the command
  chmod +x drcwks.sh
to make the script executable (use your filename if you saved it with
a different name). To run the script, use a command like:
  ./drcwks.sh drcwks.txt drcwks.new
which will run the script (assuming its in the current directory)
which will convert drcwks.txt and put the output into drcwks.new.

To explain the script, it uses the "sed" command to perform the work.
The -e option (used twice) adds commands to be executed. The first one
  -e '1,$s/.*LIN(/LIN(/'
looks for all characters up to and including LIN( on all lines and
replaces it with LIN(. The second one
  -e '1,$s/).*$/)/'
looks for the first parenthensis and all characters to end of line on
all lines and replaces it with ). The single quotes are used to ensure
that sed gets the search string just as provided on the line (with no
interpretation by the shell program).

The $1 will be replaced by the first parameter provided to the shell
script (drcwks.txt in my example) and $2 will be replaced by the
second parameter provided to the shell script (drcwks.new in my
example). The way the sed command is formed, $1 is the input file and
$2 is the output file.

I hope this script fully meets your needs. If not, or if you have
problems making it work, please make a clarification request so I can
make it right. Good luck with your work.

  --Maniac

(the short shell script follows)
#!/bin/sh

sed -e '1,$s/^.*LIN(/LIN(/' -e '1,$s/).*$/)/' $1 > $2

Request for Answer Clarification by drcwks-ga on 20 Apr 2006 19:42 PDT
Hello Maniac,

Sorry once again for the delay. Just getting back on the system.

Sorry I didn?t show a better example of what I needed. Your script did just
what I said I was wanting but I really need just one more step. I?m copying another
example that shows what your script did. It did delete ALL the random characters 
After the semi-colon in the far right columns which is needed on ALL lines.

I failed to show that there are other lines between the LIN line and
what I?m after on the
Far left columns is to strip the OTHER Nxxx lines so the text is
pushed to the far left
like you did on the LIN line. Once again once the line numbers start
at N1 the could
increment all the way to N9999. Those are the characters I need to have removed
from the entire text file. I?m pasting below what your program
returned and added some
comments where I broke further into the program. As you can see the
LIN lines are just
what I needed but once again I need the other lines stripped free of
the Nxxx characters.

Let me know if you need a better example.I DID copy and paste what I
would like the results to be.

Thanks,

drcwks

Example follows??what your program returned.......

PART1
CUT1
DRILL4
N1 START DRILL
N2 PUSH4
N3 REV002
N4 STOP;NOTE RUN AT 100%
N5 STOP ;NOTE. TURN ON COOLANT
N6 SSS()
N7 XXX()
N8 OOO()
N9 START2(0)
N10 SPINDLE()
N11 DRIVETO(368)
LIN(368.660,0.000,-207.735,0.000,0.000,0.000)
LIN(368.660,0.000,-206.985,0.000,0.000,0.000)
N14 S(1,1,1)
N15 ;STOP(1,1)
LIN(368.660,0.000,-206.735,0.000,0.000,0.000)
N17 PART2(1)
N18 ;STOP(0,0)
N19 START(0,0,0)
LIN(368.660,0.000,-207.735,-0.002,0.000,0.000)
-----------------then later in the program-------------
--------------As you can see I still need to have N1, N2, N3--N19,
N54, N56,N60-N76 replaced with no spaces where N54 line would just
read DRILL(0).
line N56 would just read DRILL(0). N58 line would just read UPSET(0)....etc, etc.
Your script DID delete the random number AFTER the
-DRILL(0) so that part will still work.  Before the line read DRILL(0) ;xyz745
so the random numbers after the semi-colon on ALL the lines---not just
on the LIN lines were deleted,  which is exactly what I need.


N54 DRILL(0)
LIN(370.855,28.920,-200.827,23.337,-0.183,0.000)
N56 DRILL(0)
LIN(369.755,28.922,-200.830,23.322,-0.179,0.000)
N58 UPSET(0)
LIN(368.525,28.924,-200.834,23.306,-0.174,0.000)
N60 UPSET5(4)
LIN(368.525,27.260,-201.526,21.900,-0.151,0.000)
N62 DRILL()
LIN(367.295,28.925,-200.837,23.292,-0.168,0.000)
N64 USPET1(0)
LIN(366.065,28.926,-200.840,23.279,-0.162,0.000)
N66 SPINDLE3
LIN(364.965,28.928,-200.843,23.268,-0.157,0.000)
N68 LOCATE1(0)
LIN(364.024,28.929,-200.845,23.260,-0.153,0.000)
N70 DRILL_SLOW(0)
LIN(363.084,28.930,-200.848,23.251,-0.150,0.000)
N72 SPEED(0)
LIN(362.143,28.931,-200.850,23.244,-0.147,0.000)
N74 STOP(5)
LIN(361.202,28.932,-200.852,23.236,-0.144,0.000)
N76 CUT
LIN(360.262,28.933,-200.854,23.228,-0.141,





Here?s the output I need?. 

PART1
CUT1
DRILL4
START DRILL
PUSH4
REV002
STOP;NOTE RUN AT 100%
STOP ;NOTE. TURN ON COOLANT
SSS()
XXX()
OOO()
START2(0)
SPINDLE()
DRIVETO(368)
LIN(368.660,0.000,-207.735,0.000,0.000,0.000)
LIN(368.660,0.000,-206.985,0.000,0.000,0.000)
S(1,1,1)
;STOP(1,1)
LIN(368.660,0.000,-206.735,0.000,0.000,0.000)
PART2(1)
;STOP(0,0)
START(0,0,0)
LIN(368.660,0.000,-207.735,-0.002,0.000,0.000)

DRILL(0)
LIN(370.855,28.920,-200.827,23.337,-0.183,0.000)
DRILL(0)
LIN(369.755,28.922,-200.830,23.322,-0.179,0.000)
UPSET(0)
LIN(368.525,28.924,-200.834,23.306,-0.174,0.000)
UPSET5(4)
LIN(368.525,27.260,-201.526,21.900,-0.151,0.000)
DRILL()
LIN(367.295,28.925,-200.837,23.292,-0.168,0.000)
USPET1(0)
LIN(366.065,28.926,-200.840,23.279,-0.162,0.000)
SPINDLE3
LIN(364.965,28.928,-200.843,23.268,-0.157,0.000)
LOCATE1(0)
LIN(364.024,28.929,-200.845,23.260,-0.153,0.000)
DRILL_SLOW(0)
LIN(363.084,28.930,-200.848,23.251,-0.150,0.000)
SPEED(0)
LIN(362.143,28.931,-200.850,23.244,-0.147,0.000)
STOP(5)
LIN(361.202,28.932,-200.852,23.236,-0.144,0.000)
CUT
LIN(360.262,28.933,-200.854,23.228,-0.141,

Clarification of Answer by maniac-ga on 23 Apr 2006 18:54 PDT
Hello Drcwks,

Try the following shell script instead. The change in the first
substitution matches:
 - the letter N
 - zero or more digits (0-9)
 - one or more spaces

I ran this on the (reconstructed) example you provided and it appears
to work OK. Let me know if you need further help.

  --Maniac

#!/bin/sh

sed -e '1,$s/^N[0-9]*  *//' -e '1,$s/).*$/)/' $1 > $2
drcwks-ga rated this answer:5 out of 5 stars and gave an additional tip of: $4.00
Works Great! Just what I needed. Thanks! Researcher is quick to
respond.Very knowledgeable.

Comments  
Subject: Re: Edit text file to search/replace using NEDIT
From: davidsj-ga on 05 Apr 2006 14:43 PDT
 
Do a regular expression (grep / regex) replace all for (with no line breaks):

^[^ ]+ +(LIN\([^)]+\)).+$

replacing with:

\1

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy