4

I want to be able to manually go through many files using less *.txt and then proceeding through the files using :n. If I determine that a file needs to be processed, then I want to be able to give a command that will, for example, send the current file name to stderr, so that once I am done going through all the files, I have a list of filenames to be processed. Is something like this possible? Is there another tool that is better for this purpose?

2
  • how do you decide? is it arbitrary or subjective, or is it based on objective fact(s) that can be searched for with grep or an awk or perl script?
    – cas
    Sep 24, 2015 at 4:00
  • The decision is based on factors that would take developing new technology to determine automatically. And besides, going through it by hand is valuable for me to become better acquainted with the data that I am using for machine-learning. Sep 24, 2015 at 8:40

3 Answers 3

3

You can setup your own less special command key. Create file ~/.lesskey and place in it, at the start, the 2 lines:

#command
ok shell echo % >>/tmp/list\n

Then give the command

lesskey

to compile the file. Run your less command on your files and when you want to save the filename type the 2 chars ok. You will see echoed:

!echo myfilename >>/tmp/list
!done  (press RETURN)

which saves the current filename (%) into /tmp/list. Press return to continue. I chose the command ok arbitrarily. You can use any single character or character sequence you like. You can append the :n command to the end of the shell line in ~/.lesskey to also move on to the next file.


If you don't have lesskey I ran it on the above file and passed it through base64 as it contains some binary. Perhaps you can try using this:

echo 'AE0rR2MaAG9rAJtlY2hvICUgPj4vdG1wL2xpc3QKOm4AZQAAdgAAeEVuZA==' | base64 -d >~/.less
3
  • This looks fantastic, but there is no lesskey on my OS X 10.8.5. Sep 24, 2015 at 17:06
  • bad luck. it seems to be mentioned by the less manpage here, so I don't know why they forgot it.
    – meuh
    Sep 24, 2015 at 17:19
  • 1
    I added the binary output to my answer. It might work.
    – meuh
    Sep 24, 2015 at 17:33
2

One way is to write a script that will do things with the least keystrokes, but if you have no time to write and thoroughly test a script, depending on what version of less you have, yes it is possible to use less.

Tested with less version 444, here is a sample walkthrough.

First, suppose you have four files a.txt b.txt c.txt d.txt

$ less *.txt

a.txt looks fine, so you view next using :n

b.txt looks bad, so to mark it you type:

! echo % | tee -a bad.txt

Less will respond:

b.txt
!done  (press RETURN)

You continue :n.

c.txt looks ok. You continue with :n

d.txt looks bad too, but you don't have to retype every single keystroke again, just ! and up-arrow, it will auto fill last command, and save d.txt to bad.txt

Then you q to quit less.

Now you can review the saved list:

$ cat bad.txt
b.txt
d.txt

Explanation

  • The ! shell command runs of course a shell command, where % is the current file
  • using tee is optional, just to have an additional indicator after you run the command, of what filename you just saved.
  • -a appends, otherwise would overwrite and your bad.txt just has a single record
  • you could have also just ! echo % >> bad.txt to append to bad.txt
0

When I need to do something like this, I'll use a tabbed terminal with less in one tab and vi in an adjacent tab.

When I get to a file I want to tag, I'll double-click on the filename at the bottom of the less screen, switch to the vi tab, middle-click paste it and hit ENTER, then switch back to the less tab.

If there's more than a couple of dozen files (i.e. it's likely to take me more than a few minutes), I'll try to figure out some way of automating the selection process with one or more of the usual tools: grep, awk, perl, etc.

grep for simple searches, awk or perl for more complicated ones.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .