Lädt...

🔧 Linux: Search, Filter and Output


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

On some occasions, one may need to filter through the contents of large files. For this, Linux has several commands to read the files and filter their contents simultaneously. Let's go through some examples.

Reading through a file in the command line

When reading through a file using the cat command, you usually see the last line of the file displayed before the next command prompt, requiring you to scroll backward to view the previous content.

With the more and less commands, the content fits on the screen, and you can scroll down to see the rest of it.

Try the following to see the difference.

cat /etc/passwd
cat /etc/passwd | more
cat /etc/passwd | less

Sometimes you may only want to read the beginning or end of a large file. For that, you can use the head or tail command, which will display the first 10 lines and the last 10 lines, respectively. Of course, you can specify a different number of lines with their options.

Use the man command to see the rest of the options.

Searching and filtering contents in a file

Using the grep command, one can search for matching strings or patterns in the contents of a file.

# displays matching lines containing the string "bin" in the file
cat /etc/passwd | grep "bin"
# displays matching lines that don't contain the string "false"
cat /etc/passwd | grep -v "false"
# Show all lines that match the regex pattern
cat /etc/ssh/sshd_config | grep -E "^[^#]"

Outputting the results of a file

Linux manages input and output operations using references or identifiers called file descriptors. The file descriptors for the input data stream are 0, for output, 1, and for error, 2.

Suppose you want to store the output in a file. (By default, the standard output (STDOUT - FD1) is redirected to the file, even if you don't specify the file descriptor 1.)

find /etc/ -name *.conf 1>results.txt
find /etc/ -name *.conf > results.txt

The file will be created if it doesn't exist.

Suppose you want to discard all the errors into a null device before storing or displaying the results.

find /etc/ -name shadow 2>/dev/null > results.txt

Here you specify the file descriptor 2 for the standard error (STDERR - FD2)

You can also log the error to a file.

find /etc/ -name shadow 2> stderr.txt

If you want to append to the already existing file, use >> instead of >

find /etc/ -name shadow 2>> stderr.txt
...

🔧 Linux: Search, Filter and Output


📈 30.77 Punkte
🔧 Programmierung

🔧 Environment Output Variables: Easy and Secure Output Piping


📈 25.94 Punkte
🔧 Programmierung

🐧 Issue with sound output "Dummy Output." on Omen Trancend 14 laptop.


📈 24.64 Punkte
🐧 Linux Tipps

🔧 PHP: The first rule of the output buffering is you do not talk about the output buffering


📈 24.64 Punkte
🔧 Programmierung

🕵️ go-ipfs up to 0.7.x Console Output escape output


📈 24.64 Punkte
🕵️ Sicherheitslücken

🔧 1. "Python Program to Filter CSV Rows and Write Output to New File"


📈 22.7 Punkte
🔧 Programmierung

🐧 Combining blue light filter with a grayscale filter results in a better and more pleasant night light mode


📈 19.46 Punkte
🐧 Linux Tipps

🐧 A command-line fuzzy finder that can be used to search through any terminal output


📈 18.17 Punkte
🐧 Linux Tipps

🔧 Playwright: Filter Visible Elements With locator.filter({ visible: true })


📈 18.16 Punkte
🔧 Programmierung

🔧 AWS: Metric Filter vs. Subscription Filter


📈 18.16 Punkte
🔧 Programmierung

🔧 Codepen logo with filter (filter challenge) by tidycoder


📈 18.16 Punkte
🔧 Programmierung

🔧 CS50 PSet Filter(more) : filter.c


📈 18.16 Punkte
🔧 Programmierung

📰 Snapchat Baby Filter 2024: Ways to Get Baby Face Filter on Snapchat


📈 18.16 Punkte
📰 IT Security Nachrichten

🕵️ Medium CVE-2022-34972: So filter shop by project So filter shop by


📈 18.16 Punkte
🕵️ Sicherheitslücken

🕵️ libMirage 3.2.2 CSO Filter filter-stream.c Heap-based memory corruption


📈 18.16 Punkte
🕵️ Sicherheitslücken

🕵️ ZoneMinder up to 1.32.3 filter.php filter[Query][terms][0][val] cross site scripting


📈 18.16 Punkte
🕵️ Sicherheitslücken

🕵️ ZoneMinder up to 1.32.3 filter.php filter[AutoExecuteCmd] cross site scripting


📈 18.16 Punkte
🕵️ Sicherheitslücken

🔧 Full-Text Search vs. Semantic Search: Exploring Advanced Search Technologies


📈 17.56 Punkte
🔧 Programmierung

🔧 Fuzzy Search vs. Vector Search: Exploring Modern Search Technologies


📈 17.56 Punkte
🔧 Programmierung

🔧 Fuzzy Search vs. Semantic Search: Unraveling Advanced Search Technologies


📈 17.56 Punkte
🔧 Programmierung

🔧 Fuzzy Search vs Lexical Search: Understanding Modern Search Techniques


📈 17.56 Punkte
🔧 Programmierung

🔧 Swirl Search: Open Source Enterprise Search 🔍 to Securely 🔐 Search your Data.


📈 17.56 Punkte
🔧 Programmierung

🕵️ Portal Search search.php search cross site scripting


📈 17.56 Punkte
🕵️ Sicherheitslücken