How to search for a particular string in a text file using java?

Edit text file to search/replace using NEDIT

  • 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.

  • Answer:

    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

drcwks-ga at Google Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.