0% found this document useful (0 votes)
5 views5 pages

Sort Unique Find Commands

OS

Uploaded by

Omama Arshad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Sort Unique Find Commands

OS

Uploaded by

Omama Arshad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

sort Command
The sort command in Windows is used to sort lines of text files or standard input in
ascending order by default. It is commonly used to organize data for easier reading and
processing.

 sort sorts lines in a text file or standard input.


 By default, it sorts alphabetically or numerically in ascending order, but you can
customize the sorting order and options.

Syntax:

sort [options] [file]

 file: The input file to sort (optional). If omitted, sort reads from standard input (e.g.,
piping another command).

Options:

 /R: Sorts in reverse order (descending).


 /+n: Skips the first n-1 characters in each line before sorting.
 /L [locale]: Specifies the language/locale to use for sorting.
 /T [path]: Specifies the temporary directory to store data if memory is insufficient.
 /O [file]: Specifies the output file to store the sorted results.

Use:

 You can use sort to organize text data alphabetically or numerically. This is
particularly useful when handling large datasets or logs.

Examples:

1. Sort a Text File:


o Input:

apple
banana
orange
grape

o Command:

sort fruits.txt

o Output:

apple
banana
grape
orange
2. Sort in Reverse Order:
o Command:

sort /R fruits.txt

o Output:

orange
grape
banana
apple

3. Sort Standard Input:


o Command:

echo banana apple grape orange | sort

o Output:

apple
banana
grape
orange

4. Skip First Two Characters in Sorting:


o Input:

01 apple
02 banana
03 orange
04 grape

o Command:

sort /+3 fruits.txt

o Output:

01 apple
04 grape
02 banana
03 orange

2. uniq Command
The uniq command in Windows can be replicated using PowerShell, as Windows doesn't
natively provide uniq like UNIX systems. In PowerShell, Select-Object is used to achieve the
same functionality.
 uniq removes or displays only unique lines in a file or input.
 It is typically used to eliminate duplicate lines in sorted files.

Syntax in PowerShell:

Get-Content [file] | Sort-Object | Get-Unique

Options:

 PowerShell’s Get-Unique doesn't have many flags like uniq in Linux. However, it can
be combined with other commands to filter unique or duplicate content.

Use:

 You can use this combination to remove duplicate lines from sorted files or input.

Examples:

1. Remove Duplicate Lines from a Sorted File:


o Input (sorted_fruits.txt):

apple
apple
banana
grape
orange
orange

o Command (PowerShell):

Get-Content sorted_fruits.txt | Get-Unique

o Output:

apple
banana
grape
orange

2. Display Only Unique Lines from Piped Input:


o Command (PowerShell):

echo "apple", "apple", "banana", "grape", "orange", "orange" | Get-Unique

o Output:

apple
banana
grape
orange
3. find Command
The find command in Windows is used to search for specific text within files or standard
input.

 find is used to search for lines that contain a specific string of text in a file or input.
 It can also display the count of matching lines or return lines that do not match.

Syntax:

find [options] "string" [file(s)]

 string: The string of text to search for.


 file(s): The file or files in which to search for the string.

Options:

 /I: Ignore case during the search.


 /N: Display line numbers along with matching lines.
 /C: Display the count of matching lines instead of the actual lines.
 /V: Display lines that do not contain the specified string.

Use:

 You can use find to quickly locate specific text in files, which is useful when working
with large logs or documents.

Examples:

1. Find a String in a File:


o Input (data.txt):

apple is a fruit.
banana is also a fruit.
orange is a citrus fruit.

o Command:

find "fruit" data.txt

o Output:

apple is a fruit.
banana is also a fruit.
orange is a citrus fruit.

2. Case-Insensitive Search:
o Command:
find /I "FRUIT" data.txt

o Output (case-insensitive match):

apple is a fruit.
banana is also a fruit.
orange is a citrus fruit.

3. Count Matching Lines:


o Command:

find /C "fruit" data.txt

o Output:

---------- DATA.TXT: 3

4. Display Line Numbers with Matching Lines:


o Command:

find /N "fruit" data.txt

o Output:

[1] apple is a fruit.


[2] banana is also a fruit.
[3] orange is a citrus fruit.

5. Find Lines Without the String (/V):


o Command:

find /V "fruit" data.txt

o Output: (No lines, because all contain "fruit")

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy