본문 바로가기

카테고리 없음

Mac Terminal Search Files For Text



  1. Basic Mac Terminal Commands
  2. Search Files For Text String
  3. Linux Search Files For Text
  • When you use Terminal there may arise a situation where you want to save the text or output that you have generated. This can be useful as a backup or a method to record what you have been doing. For example, if you want to save the list of files within a folder, this text can be saved for future reference.
  • In this Linux/Mac terminal tutorial, we will be learning how to use the grep command. The grep command allows us to search files and directories for patterns of text.
  • But if you are in need to search a file with a specific text and want to do it with a gui, gnome-search-tool solves the problem without an index. It was a default app in previous Ubuntu versions, I don't know why they removed it without a replacement.
Active1 year ago

There is a file named RESULTS.txt and I want to open this file in my terminal. (I mean I want to see the file contents be displayed in the terminal and not in some text editor)

How do I do that ?


Terminal

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

16 Answers

In this Linux/Mac terminal tutorial, we will be learning how to use the grep command. The grep command allows us to search files and directories for patterns of text. You can also pipe the output of one command into grep to get certain matches.

For short files:

Mac Terminal Cmds. The Awesome Automation Guide for Macs. UFS/HFS/DOS find Search for files that meet a desired criteria fmt Reformat paragraph text fold Wrap. Commands for finding files in the OS X Terminal The Terminal in OS X is a relatively powerful environment, where you have access to a number of scriptable tools that can help you configure, gather information, and otherwise use your Mac in ways that you cannot otherwise do with a mouse and graphical elements. Search for text in multiple Word files via the Terminal Authored by: Glide on Feb 24, '05 02:14:31AM Once you find what you're looking for you might then want to change the sought after text to something else.

directly shows a text file in the terminal.

For longer files:

lets you scroll and search (/text to searchEnter) in the file; press q to exit.

e.g.


Another alternative is vim.

Once you opened a file with vim you can insert text by typing i, for instance. If you want to save your file use :w (write) or :q (quit) or :wq (for write and quit) or :q! (quit and do not save). Sometimes you need to hit the ESC key to be able to type the commands.

Vim requires some learning, but is widely used and it is very versatile.

Check the community help wiki: https://help.ubuntu.com/community/VimHowto

Vim is an advanced text editor that provides the power of the de-facto Unix editor 'Vi' with a more complete feature set. Vim is often called a 'programmer's editor,' and is so useful for programming that many consider it an entire IDE. It's not just for programmers, though. Vim is perfect for all kinds of text editing, from composing email to editing configuration files.

Mac

all those are best ways and there is one more way to do this & that’s with head command.

and

both will give you the same input.

Head command Explanation:

Generally head command used to print the starting lines of the any text file.we can view the text file with

That will prints the 1st 10 lines of the above text file.

If you want to specific on the number of lines which are to be view then you can use head as

Then in the above text file first 20 lines will be viewed.

If you want to view whole file data with head means then then we can get it by

Hope that above explanation will give you some idea on usage of head.


If the file is rather long, you might want to use

so that you can navigate through it with directional keys.


Another option is:

to print out the last 30 lines of a large file named result.txt.


Another option:

It will show you the last ten lines of your_file. If a process appends something to this file, you see it on your terminal. man tail gives you more on tail.

It's useful to see what happens with a server when you use this command on a log file.

Press Ctrl-C to quit when you are done viewing.


There are a lot of alternatives for doing that:

Some of these programs have a lot of parameters, so check that out with --help after the command..

  • cat filename prints the whole file at once
  • more/less filename similar behaviour for see the file in parts
  • tail filename start reading from the tail of the file
  • grep text filename for filtering results

Hope that some of this works for you..


With a terminal text editor: nano /path/to/file/RESULTS.txt


Lots of good options provided here already, but another option if you need to edit is emacs:

might not need the -nw, depending. You may also have to apt-get install emacs23 or apt-get install emacs24, or if you don't have X or don't want related X dependencies, apt-get install emacs23-nox or apt-get install emacs24-nox.

And in addition to cat and less as mentioned elsewhere, there is more. More is less, because you see a page at a time and can't scroll via the command itself, but you can scroll with the terminal window, if you have a scrolling terminal window:

If you're in bash, you have something similar to cat by doing:


If you just want to read the file content, go in the file directory and type

If you want to read and edit the text file, from the same directory type

The -w switch in the nano command can be inserted before the file name to prevent wrapping of long lines.


If you need to edit the content of the file i commonly use nano.


or just

vi YourFile

Basic Mac Terminal Commands

use hjkl buttons to move line left/down/up/right, Esc then :q to quit

and you can PageUp/PageDown

you can also edit it here in a stright way

here you'll find more link


The shell programm sed also has an option to print out the contents of a file.

So sed walks through every line and prints it to the terminal. But sed also has editing capabilities. For instance if you want to replace each comma with a dot you can write:


As we seem to be listing all available alternatives of displaying any text file in the terminal, it would be quite fun to introduce pv as technically one valid (but unusual) method, although I would normally use cat instead for most things.

It is in the repositories and so can be installed with sudo apt-get install pv if you don't have it already.

As the man page notes, pv is very often used to

monitor the progress of data through a pipe...pv will copy each supplied FILE in turn to standard output (- means standard input), or if no FILEs are specified just standard input is copied. This is the same behaviour as cat(1).

With pv you can literally print the file to the screen, and choose the rate (-L) at which it appears. The example below uses a high rate (300), but if you choose a low rate such as -L 50, it will appear as if the computer is typing out the file for you.

Needless to say you can increase the rate further (-L 8000), and the command becomes very similar to cat, with the output appearing instantaneously.

For more information see man pv or the Ubuntu manpages online.


Another more exotic answer is to use grep:

The grep command searches for a every character in the file and prints it out. So basically the complete file is printed out.


Why not.

You can also use

It's almost the same as less, but it also supports horizontal scrolling if the file contains long lines - which is really handy.

most is not installed by default, so to use it, you have to first

Text

protected by jokerdinoFeb 28 '13 at 9:27

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged command-linefilestext-editor or ask your own question.

Terminal

Hey,

When you use Terminal there may arise a situation where you want to save the text or output that you have generated. This can be useful as a backup or a method to record what you have been doing. For example, if you want to save the list of files within a folder, this text can be saved for future reference. There are two methods to save out Terminal output. The first is to use the built in menu bar command, the second is to use a Terminal command. Both have there advantages and disadvantages.

Search Files For Text String

Using Terminal’s Menu Command

Linux Search Files For Text

This first method is to use the built in command within Terminal. If you go to Shell > Export Text As you can save the current Terminal output as text.

Terminal has a built in command to save Terminal’s output.

This command will save everything within Terminal since you have started the session. This includes commands, outputs of those command and the small bit of text (the current folder, and user) before the command. This method is useful in saving everything, since you have a complete record. It can also save output text that is constantly updating, such as the top command, which is kind of hard to output with the next trick.

The problem with command, is that it does save everything. Most of the time you don’t need the command you entered 50 lines previous.

Saving Text Within Terminal

Terminal has a method in which you can save the output of a command, such as ls for listing the files within a directory, to a text file. This has the advantage of being very clean in terms of text output and the ability to customise its output.

To save the text output open terminal and write the follow:

[command] > textfile.txt

Note that this will save the text file to the current directory. You can specify a full path for the location of the text file. This command will create a new file each time. So if you repeat the command, it will write out a new file. If you want to append the text to the end of a text file you can use the following command.

[command] >> textfile.txt

This trick has the advantage in that it is very clean, it will only save the output of the command.

The disadvantage of this command is that you have to set up your command each time, which can be tricky if it is long. Plus the text output has to finish, for example it doesn’t work with the top command, since the command will never exit thus the text file cannot be generated.

Conclusion

Each command has its own advantages and disadvantages. I prefer the latter trick as it only saves out what I want. At the end of the day, you can still select the text from within Terminal and copy the text out using Command + C or the menu bar command.


If you want to keep up with the latests post from Mac Tricks And Tips I recommend you subscribe to the RSS Feed.

Related posts:

Where To Next?