i need to replace a portion of a line of text with another line of text ex:
07/24/2012 06:30:00 <--what i start with
07/24/2012 06:30:00 Name=weather <---is what i need it to look like
every day the date changes and i have about 20 of these lines to change every day, whats the easiest way to do this using a bat file, i want to be able to run it and it would open the file, change what needed to be changed then spit out the changed text file in another location. there are hundreds of lines in this text file that need to stay in the new one and not change only about 20 or so need to be changed. i dont need it to loop at all since every time i need to edit the file the text needed to change will be exactly the same and it will need to change the same number of lines each time. thanks in advance
One way using sed:
sed -e "s/\(.*\)/\1 Name=weather/" file.txt > /your/new/location/newfile.txt
Perhaps you should update your question, to include example input and expected output. But the above line should get you started.
Related
I have a medium-sized tab-delimited .txt file - about 40k lines. When I import to Openrefine, line 406 puts all the rest of the content - the whole 40,000 lines, into a single cell in column 13 of that line.
I've tried grep-serching the invisibles in two different text editors (Sublime Text 2 & TextWrangler), and everything looks like it should.
I've also tried using Excel to convert to csv, and that actually works, but:
it's an inelegant workaround,
it has trouble with diacriticals, and
I don't want to spend ay more time resolving it in Excel anyway
I tried excepting the offending line with 10 lines on either side, and that throws the same problem.
Here are those 21 lines, copied directly from TextWrangler. (I can copy from Terminal output if that makes any difference.)
Any help, as always, is very much appreciated!!
Solved It! Well, sort of. It turns out that Column 13 had text that included double-quotes within the text itself (In other words, not having to do with delimiters at all).
For now, I'm just going to remove those quotes in the entire file, which does work - I tested it. **I'd rather figure out how to keep the quotes as part of the text. Tried escaping them with /, but that didn't work.
Thanks SO Community. Especially #Ettore.
I see. The problem is related to the quotations marks. Try importing your file by unchecking "Quotation marks are used to enclose cells containing column separators".
The empty columns in my screenshot are due to the fact that your file sometimes has two or three tabs as a separator. You can delete them easily after import using "reorder / remove columns"
I have access to a small portion of a code file, when I get an exception, the line number in the exception will refer to the entire code file, rather than just the section I have access too.
This means I'll get a error message for line 300, which is actually line 5 in my code file. The starting number varies depending on the file I'm working on.
To get around this at the moment, I just insert the relevant number of blank lines so that my line 1 lines up with where it will in the parent file.
I'd like to know if there is a way to get the line numbers in Sublime to start at something other than 1.
That way I'd be able to set the first line number in my file to the actual line number it will be when its inserted into the parent file.
The line numbers in Sublime are based on the number of lines that are actually in the current view. There is no method in the API to alter their appearance, unfortunately.
I have a question about how to automize the process of copying contents from a .srt file onto a .xls file.
I want to make sure that the content in the .srt file could be pasted into the corresponding columns of the .xls (e.g. the time-in to the B column; time-out to the C column; subtitles to the E column.)
In order to avoid manually copying and pasting, is there a way to script this process? Any ideas?
Thank you very much in advance! :)
UPDATE: I just found that Subtitle Edit can save .srt as csv, which will be able to change the file into an Excel file. That's handy! But there's another problem, I need to copy the content from this csv to another Excel template, which has a different structure, so I can't directly copy and paste the values in the csv. I'm working on how to make this easier...
Can't post images for now, but the situation is that while each time-in text in the srt converted csv file takes up one row, the time-in text in the Excel template takes up two rows, so I can't directly copy and paste all the texts from one excel file to the other. Is there any easier ways to do this? Thank you!
In a script, you can use perl to do the substitution:
perl -0777 -pe 's/\n([^\n])/\t$1/g; s/ --> /\t/g' input.srt | \
perl -ne 's/^\t//; print unless /^$/' > output.csv
For this sample input
1
00:00:01,478 --> 00:00:04,020
Srt sample
2
00:00:05,045 --> 00:00:09,545
<i>italic</i> font
3
00:00:09,378 --> 00:00:13,745
<b>bold</b> font
4
00:00:14,812 --> 00:00:16,144
Multi
Line
you get the following output:
1 00:00:01,478 00:00:04,020 Srt sample
2 00:00:05,045 00:00:09,545 <i>italic</i> font
3 00:00:09,378 00:00:13,745 <b>bold</b> font
4 00:00:14,812 00:00:16,144 Multi Line
Regarding the command: There are two chained perl commands
The first one does the hard work: replaces newlines and arrows with tabs (keeping double newline as one newline).
The second one only does some cleaning, it removes tabs from line beginnings and removes redundant empty lines.
In Vim I would use:
:%s/\(.\)$\n\|-->/\1\t/g | :g/^$/d | :%s=\s\+$==
I know still not a script but now it should be easy to import it in Excel :-)
It means find the line ending with character and substitute it with that character and tabulator or find characters --> and substitute them with tabulator, and then delete empty lines, and at last remove white spaces at the end of the lines.
***Process Date From:
01/05/2012 0:00
Group;Member
Status:****
Rcp Cd Health Num Rcp Name Rcp Dob
1042231 1 MARIA TOVAR DIAS 14-Feb-05
1042256 2 KHALID KHAN 04-Mar-70
1042257 3 SAMREEN ISMAT 25-Mar-80
1042257 5 SAMREEN ISMAT 25-Mar-80
1042257 4 SAMREEN ISMAT 25-Mar-80
I want my Powerbuilder datawindow Save As text look like this Bold text are the additional text want to add and rest is the current save as text result.
Text files cannot contain formatting. There's no way to get bold text in a plain text file. I suggest adding the text to your datawindow header band (bolded, with an expression to make sure it only displays on the first page), then saving the results as HTML.
Well, you didn't mention which version of PB you are using, so I'll assume a recent one in which case you have some better options such as SaveAsAscii and/or SaveAsFormattedText which offer more flexibility in displaying column headers, computed fields, etc.
If you want to add the top section, I would add one or more additional dummy columns (or computed fields) to your dataobject for the additional data. Then either populate the dummy columns manually after retrieve, or via expression in computed field. You could put all of it in one computed field that wraps, or use four different ones (e.g. process_date_label, process_datetime, group_status, status).
The two newer versions of SaveAs will work better for you as they display column header values instead of the column header name. SaveAsAscii came out pretty early somewhere around version 7 of PowerBuilder. SaveAsFormattedText is relatively new and came out somewhere around PB version 11 and it is a lot like SaveAsAscii but it lets you choose file encoding.
If you need more explicit detail let me know but I am sure you can get something to work using SaveAsAscii and extra columns.
Pseudo code
Do the SaveAs to temp file
Open the temp file for read in line mode
Open output file for write (replace) in line mode
Write your additional text lines to the output file (note: you can include CRLF to
write multiple lines at once)
Loop:
Read line from temp file
If EOF exit loop. Note: 0 is not EOF, -100 is EOF
Write line to output file
Close temp file, output file
Delete temp file
Alright, this maybe the simplest (or the stupidest) question, but I just got to know...
Suppose I have a text file containing account no. and balance. I am writing a program to search the file using entered account no. and update the balance field in the same text file with a new balance.
I am finding it surprisingly difficult to do so using file streams. The problem is that I am trying to overwrite the balance string in the said text file with a new balance string.
So, if the balance is 1000 (4 digits), I can overwrite it with another 4 digit string. But, if the new balance string is more than 4 digits, it is overwriting the data after the balance field (it is a simple text file mind you...).
For example, if the text file contains
Acc. No. balance
123456 100
123567 2500
The fields are separated by TAB '\t' character, and next record is separated by a newline '\n'. If I enter new deposit of 200000 for account 123456, the fwrite() function overwrites the data in the text file as...
Acc. No Balance
123456 2001003567 2500
You can notice that the '\n' after the balance field, and 2 digits from the next accounts' acc. no. is overwritten.
Of course, no-one wants that to happen :)
What I need is a way to insert text in that file, not just overwrite it. There are many results of doing this using Java, python or even SED, but nothing using FILE streams.
Please share your thoughts... thanks.
You'll have to move all data after the insertion point a few bytes up first. That's what Java, sed or python do as well, if they aren't writing a temporary file to begin with.
If you really want to manage your data in a plain text file:
While you are reading the file in, write a modified version of your data into a temporary file, then delete the original file and rename the temp file to the original filename. But be carefull that no other process accesses the same file concurrently.
Database systems were invented for such purposes. So I recommend to manage your data in a database table and dynamically create a text report when needed.