text and file utility in windows - text

I need to do things like: taking the first x lines of text file and save it into another text file, what kind of text utilities can I use in windows?

Use a decent text editor like Notepad++ or Vim.
If you aren't afraid of using the command line, I'd suggest taking a look at Gnuwin32, which is a port of many useful *nix utilities for Windows.
It contains heavyweight such as Sed, Awk, Grep etc., which are more than suited for any kind of text surgery.

if you want to write a batchfiles that extracts the first 10 lines of file myInputFile.txt to myOutputFile.txt use
head.exe --lines=10 myInputFile.txt > myOutputFile.txt
head.exe is one of severeal GnuUtilities for MsWindows.

Related

Find & replace code across all files in a folder in Linux

I have been using Notepad++ for Windows when I want to find a certain text across all files in a given folder. This was extremely useful for debugging my MatLab code because one project entails tens of MatLab files.
I could also replace all texts into another across all files in a folder.
I could also replace things like /r/n which means line replacement.
Now I must need to work on a Linux server. Notepad++ couldn't be installed on the university server I am allotted to. And as far as I tried, Sublime Text couldn't find and replace things like /r/n
What option do I have on Linux?
Sublime Text supports replacement of line breaks, but you have to select "regular expression" (Alt+R) and you have to enter the line break correctly (\n, not /r).
The most useful method if find and replace. (Ctrl+H)
However, another possibility is the multiple selection as in the example number one of the official website: https://www.sublimetext.com/
How it works:
You select the word you want to replace, press Ctrl+D (which will select the same word with the same name). Continue pressing Ctrl+D until you selected every word you want to replace.
Once it is done, you will have a multiple selection, which means that every character will be write at each word selected position.
I invite you to check the example on their website, it is a very good illustration.
You can use Kate, it is fine replacement for Notepad++.
Kate "Search and Replace" feature is very powerful.

How can I use vim in a pipeline to colorize text?

I'd like to have a command I can insert into a command pipeline that adds color escapes to its input according to vim's syntax highlighting capabilities.
For example:
cat somefile.js | vim - <???> | less
The resulting text would be that of somefile.js, but colorized according to how the current vim configuration would do it in-editor.
It occurs to me that this must be possible. I agree that the example up there isn't what a sane man might call exactly useful, but that doesn't mean the idea never is.
I think your idea has one basic flaw: that nobody ever thought about allowing such a thing.
Clearly vim is capable of doing syntax highlighting. But I'll bet you an ice cream cone that if you can manage to get vim to stream text through and process it, that you won't like the results.
Consider what happens when you pipe text through more (or less if you prefer). When it goes to the terminal, these programs display one screenful and wait for you to hit the space bar. But if you redirect stdout to some other place than the terminal, these programs notice this and simply copy their input to their output unchanged.
If vim doesn't notice that you are piping text through, it is likely to send cursor-movement commands that you probably don't want in your output. If vim does notice, it is likely to just pass the text, and not syntax-color it. Only if vim does do the syntax-coloring but does not inject cursor-movement stuff will your idea work.
You could try it. Here's an answer that discusses piping stuff through vim:
Execute a command within Vim from the command line
But I say why not pipe your text through a program that was designed and intended to have text piped through it? Pygments can colorize every major programming language and markup format.
http://pygments.org/
The major advantage I see for your idea: you can customize the way vim does syntax coloring, get it the way you want it, and then also use vim to process your text. But it's probably not that hard to customize Pygments, and it might even be satisfactory out of the box, in which case it would definitely be the easiest way to go. And Pygments not only has ANSI sequence output, it also has HTML output, RTF, LaTeX, etc. So if you get Pygments working the way you want it to, it should be able to output whatever output format you need; vim will only have the ANSI sequence one.
There's a Perl module called Text::VimColor that I've heard will do kinda what you're looking for.
http://search.cpan.org/dist/Text-VimColor/
But let me ask this: Why do want it to go through less? Why not use vim as a perfectly good file viewer? view - will read from standard input in read-only mode.
https://gist.github.com/echristopherson/4090959
Via https://superuser.com/a/554531/7198.
Tried on /etc/passwd and it works surprisingly well!
This might be what you're after
cat filename.sh | vim - -c 'syntax on; syn=bash'
This is ugly, but you could alias this:
alias vim.sh="vim -c 'syntax on; syn=bash'"
Then use like this:
cat filename.sh | vim.sh -
Use vimcat !
wget -O /usr/local/bin/vimcat "https://www.vim.org/scripts/download_script.php?src_id=23422"
chmod 755 /usr/local/bin/vimcat
vimcat /etc/passwd
See also: https://www.vim.org/scripts/script.php?script_id=4325

Vim quickfix, using an existing file?

There is a lot of documentation about vim's quickfix but I would like to know:
How I can I use a text file (or copy+paste from a terminal), and put this into a quick-fix list in vim?
All the docs I found so far assume that you run make or vimgrep, but in this case I just want to use a text file created elsewhere.
You're looking for :cfile / :cgetfile:
:cf[ile][!] [errorfile] Read the error file and jump to the first error.
The file you're reading naturally must be in a format that can be parsed by the 'errorformat' option.
One way to do it is to change makeprog to cat from your text file, pretty sure there's a better way to do it though.

run perl script on file I'm editing with vim (macvim)

I have been using bluefish to edit text that are to be published in html. Bluefish has an external filter function that allows me to call on scripts that I have written in perl to "filter" the text I am editing and format them basically using regex.
Having started exploring vim and macvim, I find the program to be very powerful and worth learning. I just would like to be able to use those scripts I already have without having to rewrite them as vim plugins. I have spent the past 2 hours searching but answers seem to be only for running perl as an external command or incorporating perl inside vim scripts.
Just to be clear: I want to get perl scripts I already have to act on text that I am presently editing inside vim/macvim, either the whole text or (better) selected text only.
You can use
:%!command
for example
:%!sort
to sort the whole file being edited.
If you have a range selected it will be added automatically and you complete the command
:'<,'>!command

How can I view log files in Linux and apply custom filters while viewing?

I need to read through some gigantic log files on a Linux system. There's a lot of clutter in the logs. At the moment I'm doing something like this:
cat logfile.txt | grep -v "IgnoreThis\|IgnoreThat" | less
But it's cumbersome -- every time I want to add another filter, I need to quit less and edit the command line. Some of the filters are relatively complicated and may be multi-line.
I'd like some way to apply filters as I am reading through the log, and a way to save these filters somewhere.
Is there a tool that can do this for me? I can't install new software so hopefully it's something that would already be installed -- e.g., less, vi, something in a Python or Perl lib, etc.
Changing the code that generates the log to generate less is not an option.
Use &pattern command within less.
From the man page for less
&pattern
Display only lines which match the pattern; lines which do not
match the pattern are not displayed. If pattern is empty (if
you type & immediately followed by ENTER), any filtering is
turned off, and all lines are displayed. While filtering is in
effect, an ampersand is displayed at the beginning of the
prompt, as a reminder that some lines in the file may be hidden.
Certain characters are special as in the / command:
^N or !
Display only lines which do NOT match the pattern.
^R Don't interpret regular expression metacharacters; that
is, do a simple textual comparison.
Try the multitail tool - as well as letting you view multile logs at once, I'm pretty sure it lets you apply regex filters interactively.
Based on ghostdog74's answer and the less manpage, I came up with this:
~/.bashrc:
export LESSOPEN='|~/less-filter.sh %s'
export LESS=-R # to allow ANSI colors
~/less-filter.sh:
#!/bin/sh
case "$1" in
*logfile*.log*) ~/less-filter.sed < $1
;;
esac
~/less-filter.sed:
/deleteLinesLikeThis/d # to filter out lines
s/this/that/ # to change text on lines (useful to colorize using ANSI escapes)
Then:
less logfileFooBar.log.1 -- applies the filter applies automatically.
cat logfileFooBar.log.1 | less -- to see the log without filtering
This is adequate for now but I would still like to be able to edit the filters on the fly.
see the man page of less. there are some options you can use to search for words for example. It has line editing mode as well.
There's an application by Casstor Software Solutions called LogFilter (www.casstor.com) that can edit Windows/Mac/Linux text files and can easily perform file filtering. It supports multiple filters as well as regular expressions. I think it might be what you're looking for.

Resources