Finding multiple strings in a text file and deleting the entire line
Get-Content c:\output-Copy.txt | Where-Object {$_ -notmatch 'something1', 'something2', 'something3', 'something4' } | Set-Content temp.txt
Above is the program that when it has 1 argument in the -notmatch part will find the specific string and delete the entire line. I need to be able to use multiple strings for my search, I am totally drawing a blank on this and I know its something REALLY
simple but my brain is fried. The above code will work IF i only leave 'something1' and delete the rest. I need to find MULTIPLE matches and remove them all at once, what is a good way to accomplish this.
You can use an alternating regex, like this:
Get-Content c:\output-Copy.txt |
Where-Object {$_ -notmatch 'something1|something2|something3|something4' } |
Set-Content temp.txt
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
Similar Messages
-
Copy one text file to another text file and delete last line
Hi all wonder if someone can help, i want to be able to copy one text file to another text file and remove the last line of the first text file. So currently i have this method:
Writer output = null;
File file = new File("playerData.xml");
try {
output = new BufferedWriter(new FileWriter(file, true));
output.write("\t" + "<player>" + "\n");
output.write("\t" + "\t" + "<playerName>" + playerName + "</playerName>" + "\n");
output.write("\t" + "\t" + "<playerScore>" + pointCount + "</playerScore>" + "\n");
output.write("\t" + "\t" + "<playerTime>" + minutes + " minutes " + seconds + " seconds" + "</playerTime>" + "\n");
output.write("\t" + "</player>" + "\n");
output.write("</indianaTuxPlayer>" + "\n");
output.close();
System.out.println("Player data saved!");
catch (IOException e) {
e.printStackTrace();
}However each time the method is run i get the "</indianaTuxPlayer>" line repeated, now when i come to read this in as a java file i get errors becuase its not well formed. So my idea is to copy the original file, remove the last line of that file, so the </indianaTuxPlayer> line and then add this to a new file with the next data saved in it. So i would end up with something like this:
<?xml version="1.0" encoding="UTF-8"?>
<indianaTuxPlayers>
<player>
<playerName>Default Player</playerName>
<playerScore>0</playerScore>
<playerTime>null minutes null seconds</playerTime>
</player>
<player>
<playerName>Default Player</playerName>
<playerScore>0</playerScore>
<playerTime>null minutes null seconds</playerTime>
</player>
<player>
<playerName>Default Player</playerName>
<playerScore>0</playerScore>
<playerTime>null minutes null seconds</playerTime>
</player>
</indianaTuxPlayers>
However after all day searching the internet and trying ways, i have been unable to get anything working, could anyone give me a hand please?I would go the XML route too, but for fun, open a file as a BufferedWriter and do this:
void copyAllButLastLine(File src, BufferedWriter tgt) throws IOException {
BufferedReader in = new BufferedReader(new FileReader(src));
try {
String previous= in.readLine();
for (String current = null; (current = in.readLine()) != null; previous=current) {
tgt.write(previous);
tgt.newLine();
} finally {
in.close();
} -
How can i read the text files and buffer the data in Vector?
hi. I have been running into this problem for days, but with no luck and losing right direction.
The problem is : I am trying to read a text file and buffer the data into a
Queue for each user.
the sample text file is as below:( 1st column is timestamp, 2nd is user_id, 3rd is packet_id, 4th is packet_seqno, 5th is packet_size)
0 1 1 1 512
1 2 1 2 512
2 3 1 3 512
3 4 1 4 512
4 5 1 5 512
5 6 1 6 512
6 7 1 7 512
7 8 1 8 512
8 9 1 9 512
9 10 1 10 512
10 1 2 11 512
11 2 2 12 512
12 3 2 13 512
13 4 2 14 512
14 5 2 15 512
15 6 2 16 512
16 7 2 17 512
17 8 2 18 512
18 9 2 19 512
19 10 2 20 512
20 1 3 21 512
21 2 3 22 512
22 3 3 23 512
23 4 3 24 512
24 5 3 25 512
25 6 3 26 512
26 7 3 27 512
27 8 3 28 512
28 9 3 29 512
29 10 3 30 512
30 1 4 31 512
31 2 4 32 512
32 3 4 33 512
33 4 4 34 512
34 5 4 35 512
35 6 4 36 512
36 7 4 37 512
37 8 4 38 512
38 9 4 39 512
39 10 4 40 512
40 1 5 41 512
41 2 5 42 512
42 3 5 43 512
43 4 5 44 512
44 5 5 45 512
45 6 5 46 512
46 7 5 47 512
47 8 5 48 512
48 9 5 49 512
49 10 5 50 512
50 1 6 51 512
51 2 6 52 512
52 3 6 53 512
53 4 6 54 512
54 5 6 55 512
55 6 6 56 512
56 7 6 57 512
57 8 6 58 512
58 9 6 59 512
59 10 6 60 512
60 1 7 61 512
61 2 7 62 512
62 3 7 63 512
63 4 7 64 512
64 5 7 65 512
65 6 7 66 512
66 7 7 67 512
67 8 7 68 512
68 9 7 69 512
69 10 7 70 512
70 1 8 71 512
71 2 8 72 512
What I wanna do is to read all the data above and buffer them in a queue for each user( there are only 10 users in total).
I already created a class called Class packet:
public class packet {
private int timestamp;
private int user_id;
private int packet_id;
private int packet_seqno;
private int packet_size;
/** Creates a new instance of packet */
public packet(int timestamp,int user_id, int packet_id,int packet_seqno, int packet_size)
this.timestamp = timestamp;
this.user_id=user_id;
this.packet_id=packet_id;
this.packet_seqno=packet_seqno;
this.packet_size=packet_size;
}then I wanna to create another Class called Class user which I can create a queue for each user (10 users in total) to store type packet information. the queue for each user will be in the order by timestamp.
any idea and sample code will be appreciated.Doesn't sound too hard to me. Your class User (the convention says to capitalize class names) will have an ArrayList or Vector in it to represent the queue, and a method to store a Packet object into the List. An array or ArrayList or Vector will hold the 10 user objects. You will find the right user object from packet.user_id and call the method.
Please try to write some code yourself. You won't learn anything from having someone else write it for you. Look at sample code using ArrayList and Vector, there's plenty out there. Post in the forum again if your code turns out not to behave. -
How to read the source file and copy the entire content to the target Database field
Hi All,
PI system extracts the actual business content from the incoming attachment file and copy the entire format to the target Database field which is a blob data type field.
Sender Adapter: SOAP
Receiver Adapter: JDBC
JDBC Structure
Can we achived the above requirement by using UDF with out Java Mapping?
Thanks,
Mahi.Ok, in this case yo can use the conten converion of the file adapter. Therefore you first need to create a Datatype in ESR with the required structure
Then in your file adapter you need to use this datatype and its required fields:
The Datatype then can be used as usual within you mapping.
regards
Christian -
In "Notes", typing a hyphen deletes the entire line of text
Has anybody encounterd goofiness in Notes? I am frequently experiencing problems where Notes is deletes my typing. This happens often at random times but if I switch to a different line on the page it will let me type. Typing a hyphen will ALWAYS delete the entire line of text.
Perplexed... has been going on for months. Did not used to happen....Excellent suggestion, Peter.
Tabbing proved to be a little dodgy, simply because my paragraphs are set to Justify with Last Line Aligned Left, and so depending on where I am in a line of text, I'd have to get really deep into tinkering with my tab settings. But just using the space bar and tweaking the character styles yielded fine results.
Thanks!
(Now if only ID6 would print correctly when duplexing on my HP 8100 on letter-sized paper…) -
How to read the contents of a text file and populate the data in a table ?
Hello All,
Can anyone advise on how to acheieve the above ? I am trying to read in a text file (CSV) and have the contents populated to the respective UI elements in a table. Any help is greatly appreciated.
from
Kwok WeiHi,
Let us consider you have list of names(Seperated by delimeter) in a text file and you want to display in a table.
1. Create Context Node "Names" and context attribute "Name"
2. Create Table and bind to the above context.
3.Write the following code in the "Init method.
try{
FileReader f =new FileReader("");
BufferedReader r=new BufferedReader(f);
String names=r.readLine();
Vector Names=new Vector();
// Use Tokenizer and store all the names i a vector//
for(int i=0;i<Names.size();i++){
IPrivate<<VieName>>.INameElement ele=wdContext.createNameElement();
ele.set<<Name>>( Names.get(i).toString());
wdContext.NodeName().addElement(ele);
Regards, Anilkumar
Message was edited by: Anilkumar Vippagunta -
Issue : Read a text file and print the same
Hi, My requirement is to read a text file and print it the same way.
import java.io.BufferedReader;
import java.io.FileReader;
public class CatFile {
public static void main(String[] args) throws Exception
FileReader file = new FileReader("D:/Test/Allfiles.txt");
BufferedReader reader = new BufferedReader(file);
String text = "";
String line = reader.readLine();
while (line != null)
text += line;
line = reader.readLine();
System.out.println(text);
The text file i used contains
A
B
C
but my output is ABC.
What change should be made to print it the same way in the txt file ?Hi EJP,
I modified the code based on your suggestion and now its working as expected. Thanks
Modified code :
import java.io.BufferedReader;
import java.io.FileReader;
public class CatFile {
public static void main(String[] args) throws Exception
FileReader file = new FileReader("D:/Test/Allfiles.txt");
BufferedReader reader = new BufferedReader(file);
String text = "";
String line = reader.readLine();
while (line != null)
System.out.println(line);
line = reader.readLine();
text += line;
} -
Reading from a text file and displaying the contents with in a frame
Hi All,
I am trying to read some data from a text file and display it on a AWT Frame. The contents of the text file are as follows:
pcode1,pname1,price1,sname1,
pcode2,pname2,price2,sname1,
I am writing a method to read the contents from a text file and store them into a string by using FileInputStream and InputStreamReader.
Now I am dividing the string(which has the contents of the text file) into tokens using the StringTokenizer class. The method is as show below
void ReadTextFile()
FileInputStream fis=new FileInputStream(new File("nieman.txt"));
InputStreamReader isr=new InputStreamReader(fis);
char[] buf=new char[1024];
isr.read(buf,0,1024);
fstr=new String(buf);
String Tokenizer st=new StringTokenizer(fstr,",");
while(st.hasMoreTokens())
pcode1=st.nextToken();
pname1=st.nextToken();
price1=st.nextToken();
sname1=st.nextToken();
} //close of while loop
} //close of methodHere goes my problem: I am unable to display the values of pcode1,pname1,price1,sname1 when I am trying to access them from outside the ReadTextFile method as they are storing "null" values . I want to create a persistent string variable so that I can access the contents of the string variable from anywhere with in the java file. Please provide your comments for this problem as early as possible.
Thanks in advance.If pcode1,pname1,price1,sname1 are global variables, which I assume they are, then simply put the word static in front of them. That way, any class in your file can access those values by simply using this notation:
MyClassName.pcode1;
MyClassName.pname1;
MyClassName.price1;
MyClassName.sname1 -
Searching a text file and printing the line if found!
Hi, Im fairly new to JAVA and have been googling this for hours and havent found any results that
have helped me...
Im looking to search a text file for a value that is in a that is delimited by colons
eg 45787 : 398948 : 398884 : 398984
IF that value is found I need to print this entire line and move it into a string.
I can work out the delimit part, the problem for me is a)searching the text file
and b) tell java to print that line only??
Any help would be very very much appreciated
Cheers
Jamespublic void printCourses() throws Exception
stuinput[] courseObjArray = new stuinput[10]; //Make sure this is the same as the ammount in the text
Scanner sc = new Scanner(new File("testfile.txt"));
int i=0;
boolean found = false;
while(sc.hasNextLine())
//Delimit this line with scanner
Scanner sc1 = new Scanner(sc.nextLine());
sc1.useDelimiter(":");
courseObjArray[i] = new stuinput(sc1.next(),sc1.next());
i++;
}//while
for(int i1 = 0;i1 < courseObjArray.length;i1++)
System.out.println(courseObjArray[i1].courseCode);
System.out.println(courseObjArray[i1].courseName);
System.out.println(courseObjArray[i1].courseName);
System.out.println("-----------------");
String courseCode2 = "courseObjArray[i1].courseCode";
if (courseCode2 == "12345")
System.out.println("working");
else{System.out.println("Not Found");}
}hmmm just wondering why could this method always return a "Not Found"....if the text file had a whole bunch of values like this
12345:Course1
12344:Course2
12343:Course3
isnt it surposed to return 1 "Working"?? because there is a 12345 in on of the line, in the semi colon delimited line with 2 values, one being the course code, one being the course name??
Given that there is 10 lines (from Array Object) -
Convert XML to text file and sending the same via Mail receiver adapter
Dear All,
I have a requirement like....
I am getting a XML file as a result of mapping and it needs to be converted to a TEXT file and
the converted file needs to be sent to a mail address in text format.
Any body has idea how to achieve this?
Thanks in advance for your help.
Regards
Hari>
HP CReddy wrote:
> Dear All,
>
> I have a requirement like....
>
> I am getting a XML file as a result of mapping and it needs to be converted to a TEXT file and
> the converted file needs to be sent to a mail address in text format.
>
> Any body has idea how to achieve this?
>
> Thanks in advance for your help.
>
> Regards
> Hari
use the StructXML2Plain or XML2Plain from the standard MessageTransformBean
Add the module to you Mail adapter before the mail adapters standard module itself
Ref: http://help.sap.com/saphelp_nw04/helpdata/en/24/4cad3baabd4737bab64d0201bc0c6c/content.htm -
Any way to embed a text file and retain the link?
Hi everyone,
I am embedding the contents of a text file using "Insert > Choose...". This works wonderfully, but it appears to just insert the contents and gives me no way to update the text if the original text file changes. In Word, I can insert the text file and have it retain the link, then I have the ability to update the field (or all files in the document) with updated wording from my source files.
In case you're wondering, I'm exploring using Pages for authoring the user guides for my company's software. We include command usage statements in our documentation and we have a workflow that runs each command, capturing it's help output into a text file, then we include this in the user guide document. Obviously, having the ability to update the user guide document with updated usage syntax is a required piece of functionality. I would like to use Pages instead of Word, but I'm not sure it can do this.
Thanks in advance!
BrianNo, Pages can't do it. It doesn't keep a link to the original text document. You have to replace the text when you have made changes.
-
Want to update a text file and reflect the same on iview immediately
Dear friends,
I have a text file in a repository path
I could read each line and do some task.
My query is for each line task, i would like to update a file (for me it is log file) and reflec the same on a iview in the same page
sample code appreciated
regards
KanthaIf the user backs up their device to either iCloud or Mac/PC, they can restore the app data from that backup.
There's no need to do what you intend. -
How to output strings to an text file and excel file
Hi guys,
I am writing a simple application taht process some string inputs from user using a simple GUI. The GUI consists of a series of
textfields which the user can enter names, age, addresses....etc
Once they complete filling up that GUI form, they click SUBMIT. All the values will then be read. These strings are then required to be output to 2 files
1. To a text file which I can open it and read it anytime I wish.
2. To an excel file which follows a specific format. That is, all names will be written to column B, all ages will be written to column C...etc
The programme is expected to keep running allow the user to enter details of multiple persons(some 300 sets of data of different persons) until he clicks on End program.
Please advise how I can output the strings to
1. Text file
2. Excel file.
Many many thanks. I need this for one of my project which is due so so soon...... :((
Regards
David1. Text file
See link to "Documentation - Tutorials" on the left
side of this page.
2. Excel file
Dont try to write the real excel format (if you want
to do it soon).
- write data to a plain text file.
- Use tab stops for separation of values.
- Name file as excel file. (*.xls)
If you double click this file, excel will import data
and insert it to a table in the right order by
itself.
Excel can save this as real .xls now.
Anyone here with a better idea? (Try to learn by
myself)good thing to know for the excel tip :)
thx -
How to find a string in a text file
i'll have a text file in the following format and from this this type of file i've to get source URL i.e in the following file http://www.consolidatorshopper.com/patheo/Mall/prices.asp
and also i've to get POSTdata querystring i.e in the following file from cosolidateorname...... to &conid6=7
hope you people understood my problem
i'll be very thankful if u people help me
BEGIN HEADER
source:http://www.consolidatorshopper.com/patheo/Mall/prices.asp
POSTdata:consolidatorname=IF&AgentID=37344&SubAgentID=31030&LoginType=SUBAGNT&LoginConfirm=yes&ManageAccount=&CustomAccount=6&PALPowerSearch=2&ShowConsolidatorsOnPricesPage=0&PALHomePage=http://www.consolidatorshopper.com&PMarkup=0&PALLoginText=<B>Specific%20Consolidator%20Search:</B><BR>%20Click%20on%20the%20desired%20region%20on%20the%20map.<BR><BR><B>Power%20Search:</B><BR>To%20search%20all%20consolidators%20simultaneously%20click%20on%20the%20button%20below.%20However,%20not%20all%20consolidators%20currently%20participate%20in%20the%20Power%20Search.&PALrelativeLoginURL=&PALrelativeBookingURL=&[email protected]&SiteID=3&MallOwnerMarkupFlat=&conidselected=16&powersearch=2&iscontinentselected=1&Continent=USDomestic&Source=1&CRSName=AP&[email protected]&ConsolidatorURL=http://www.patheo.com&ConsolidatorPhone=714-677-0929&ConsolidatorFax=714-908-7110&ConsolidatorFullName=Patheo&noofconselected=7&amadeusconidstring=&flagamadeus=1&PowerSearchConsolidatorstring=&PublishedCRS=&APPseudoCity=&SBPseudoCity=&AMLogin=&assword=&AMCorpID=&AMCatNo=&ConsolidatorFares=1&PublishedFares=0&PublishedConsolidator=0&PreferencedConsolidator=&Arciata=&dep_from=MIA&dep_to=LON&ret_from=LON&ret_to=MIA&dep_month=08&dep_day=08&dep_year=2003&dep_weekday=Thu&txtLengthOfStay=7&ret_month=08&ret_day=15&ret_year=2003&ret_weekday=Thu&dep_time=12P&ret_time=12P&triptype=RoundTrip&simpleclass=Economy&srchAirline=&adultno=1&childno=0&infantno=0&conid0=11&conid1=97&conid2=1&conid3=86&conid4=5&conid5=94&conid6=7
BEGIN INFORMATION
TimeStamp:datetime:date()
BEGIN ACTION
startafter:<tr valign=middle bordercolor=#ffffff>endat:</table>
pattern:<font face='Arial, Helvetica, sans-serif' size='2'>[^<]*
$1:SYMBOL:TEXT
$2:INDEX:TEXT
$3:CLOSING:FLOAT
$4:CHANGE:FLOAT:StripHTMLTags()
$5:CHANGEPCT:FLOAT:StripHTMLTags()
BEGIN DOHi, this is my fourth post to forum.java.sun.com and my final one for the day, so I can allow feedback to emerge on my compliance or lack thereof with the conventions of the board.
Fortunately, I have picked up the skill of telepathy and so am able to answer your question. :)
In your processLine(String line) method or equivalent, you can do something like this:
if (line.indexOf("source:") == 0) {
source = line.substring(6, line.length());
} else if (line.indexOf("POSTdata:") == 0) {
postdata = line.substring(9, line.length()); -
Search a string with in some file and get the name of file
i want to search a string in some files that are in a given directory and return the name of file having searched string.I am doing this with grep command as given below
import java.io.*;
public class linux_java {
public static void main(String[] args) {
try {
String command = "find . | xargs grep -l Resolv /data2/opt/jakarta-tomcat-4.1.24/webapps/Oa/BOG/*.txt";
final Process process = Runtime.getRuntime().exec(command);
OutputStream os= process.getOutputStream();
System.out.println("out put stream= " +os);
PrintStream stdIn = new PrintStream(new BufferedOutputStream(process.getOutputStream()), true);
System.out.println("Return code = " + stdIn);
} catch (Exception e) {
e.printStackTrace();
i dont know how to get the name of file that are return by the execution of command through process object. Please send the code for the samethanks for your suggestion....
i change the code but now it is giving error as /usr/bin/find incomplete syntax....but i am giving the right syntax....
please confirm whether the syntax is correct...
import java.io.*;
import java.util.*;
public class linux_java {
public static void main(String[] args) {
try {
//String command = "ls -alt";
String command = "find /data2/opt/jakarta-tomcat-4.1.24/webapps/Oa/BOG -type f -exec grep -sl 'Aggarwal' {} \\; 2>/dev/null";
///grep -l System test/*java
System.out.println(" the command is"+command);
final Process process = Runtime.getRuntime().exec(command);
InputStream is = process.getErrorStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line="";
System.out.println(" the BufferedReader is"+br.readLine());
while ((line = br.readLine()) != null) {
System.out.println(" the files are"+line);
} catch (Exception e) {
e.printStackTrace();
}
Maybe you are looking for
-
How do I transfer a complete MiniDV to my Mac?
MacBook Pro, Maverick, iMovie 10 I am not into videos but I promised friends to help them transfer their MiniDV tapes to an external harddrive. I played around a bit only to realize that it was not really obvious (at least to me) how to set up iMovie
-
Upgrade from Palm V To T3 Tungsten
Can anyone tell me if it is at all possible to Upgrade from the Palm V to the Tungsten T3, and maintain both on windows XP with out any problems?... As I have incounted many, I recently installed the Palm Tungsten T3, and now the Palm V has lost man
-
Exchange server goes down... Export EDB into PST?
Can some body help me.....how can i import my EDB file into Outlook file format Please help me....
-
I am trying to register my new copy of Lightroom 5
Why am I thwarted?????
-
Nokia X3-02 is turned off without command after de...
My cel model X3-02 have many problems afterI update to version 7.15 the device software with Npkia OVI Update. Please who knows about return to old version, it´s better to me. Thanks