So I have a large bunch of .rtfd text files also containing images. What I'd like to do is making the following edits to all of these files:
Delete everything before a certain line of text, which I'll call 'start_line' here.
Delete every line that says 'a_line'.
Delete everything starting from (and including) 'end_line'.
So if the input files look like this:
line1
line2
start_line
line3
a_line
line4
end_line
line5
I want the output files to look like this:
start_line
line3
line4
Please note that the output file does not include end_line, but it does still include start_line.
Also the output files must remain in the .rtfd format and retain all images, as well as their original lay-out.
I am using Mac OSX and do not have much experience with batch text editing. Can this be done using the terminal? Or can you point me to a piece of software that allows operations like this?
Thank you very much!
Edit: The lines I specified earlier can also just be seen as strings of text, so the input file would become:
text1
start_string
text2
unwanted_string
text3
end_string
text4
So the output would be:
start_string
text2
text3
Related
I have numerous files with extension .awesome containing lines like the following:
something =
[51,42,12]
Where something =* is in all the files as well as **[ (numbers vary.)
I would like to get rid of the newline, but don't know how. I came across tr, but worry it would replace all newlines. My files contain multiple newlines that I would like to retain (only change this newline.) I've been able to successfully to find and replace in the past with sed, but am having specifically with the special characters (\n and =.) In addition, I'm reading that sed is line by line and cannot handle something like this.
Any guidance would be appreciated.
GNU sed solution:
Sample test.awesome file contents:
some text
another text
something =
[51,42,12]
text
text
The job:
sed '/something =/{N; s/\n/ /;}' test.awesome
The output:
some text
another text
something = [51,42,12]
text
text
I am trying to add "<br>" to the end of each line in a .log file, and create a HTML file of the results.
I have tried
sed 's/$/<br><br>/' latest.log >> latest.html
After 395 lines, it cuts out. I would just make the .log file a .html file, but the line breaks don't cross over. Sorry if any of this seems weird, I'm fairly new to this.
Well, hard to say bcaus it might be smth wrong with your input file (for example some unwanted white characters).
but you can insert it out the milion ways, the simplest one:
sed 's/.*/&<br><br>/'
do you need to explain it?
I'll just use tags at the beginning of the first line and the ending. Thank you, Walter A.
This is a common issue I have and my solution is a bit brash. So I'm looking for a quick fix and explanation of the problem.
The problem is that when I decide to save a spreadsheet in excel (mac 2011) as a tab delimited file it seems to do it perfectly fine. Until I try to parse the file line by line using Perl. For some reason it slurps the whole document in one line.
My brutish solution is to open the file in a web browser and copy and paste the information into the tab delimited file in TextEdit (I never use rich text format). I tried introducing a newline in the end of the file before doing this fix and it does not resolve the issue.
What's going on here? An explanation would be appreciated.
~Thanks!~
The problem is the actual character codes that define new lines on different systems. Windows systems commonly use a CarriageReturn+LineFeed (CRLF) and *NIX systems use only a LineFeed (LF).
These characters can be represented in RegEx as \r\n or \n (respectively).
Sometimes, to hash through a text file, you need to parse New Line characters. Try this for DOS-to-UNIX in perl:
perl -pi -e 's/\r\n/\n/g' input.file
or, for UNIX-to-DOS using sed:
$ sed 's/$'"/`echo \\\r`/" input.txt > output.txt
or, for DOS-to-UNIX using sed:
$ sed 's/^M$//' input.txt > output.txt
Found a pretty simple solution to this. Copy data from Excel to clipboard, paste it into a google spreadsheet. Download google spreadsheet file as a 'tab-separated values .tsv'. This gets around the problem and you have tab delimiters with an end of line for each line.
Yet another solution ...
for a tab-delimited file, save the document as a Windows Formatted Text (.txt) file type
for a comma-separated file, save the document as a `Windows Comma Separated (.csv)' file type
Perl has a useful regex pattern \R which will match any common line ending. It actually matches any vertical whitespace -- the same as \v -- or the CR LF combination, so it's the same as \r\n|\v
This is useful here because you can slurp your entire file into a single scalar and then split /\R/, which will give you a list of file records, already chomped (if you want to keep the line terminators you can split /\R\K/ instead
Another option is the PerlIO::eol module. It provides a new Perl IO layer that will normalize line endings no matter what the contents of the file are
Once you have loaded the module with use PerlIO::eol you can use it in an open statement
open my $fh, '<:eol(LF)', 'myfile.tsv' or die $!;
or you can use the open pragma to set it as the default layer for all input file handles
use open IN => ':raw:eol(LF)';
which will work fine with an input file from any platform
I'm currently trying to search several .sql files that I have for certain text. All of the .sql files are in the same folder. Here's the command I'm using:
grep 'text to search for'
However, that isn't displaying the lines that contain the text I'm searching for. Is there a way to display those lines and print them to a new text file?
You have to pass an file or file pattern argument, like:
grep 'text to search for' *.sql
Does anyone have or know of a Ruby script that converts text to html?
Example:
I have a file that contains the following text:
Host Name
Info1
Line1
Info2
Line2
I want to have ruby convert it to the following html output
Host Name
Info1
Line1
Info2
Line2
I tried running RedCloth but got the following error:
The program can't start because msvcrt-ruby18.dll is missing
Thanks
Thanks
That depends upon what you mean by "text to HTML." There are several "web text generators" that convert easy-to-read free text with minimal markup (asterisks to indicate bold, double-spaced paragraphs get surrounded in <p> tags, etc). The most common, for Ruby, are Redcloth, which implements Textile free text, and Bluecloth, which implements Markdown.