1. Sorting a Text File (sort)
The sort command in Linux is used to arrange the contents of a file in a specific order, either alphabetically or numerically.
Sort Alphabetically:
To sort a file alphabetically (based on the ASCII values of characters), you can use the following command:
sort filename.txt- This sorts the lines of the file
filename.txtin ascending alphabetical order.
Example:
File names.txt:
John
Alice
bob
Eve
Command:
sort names.txtOutput:
Alice
Eve
John
bob
By default, sort uses case-sensitive sorting (uppercase letters come before lowercase letters).
Sort Numerically:
To sort numerically (e.g., when sorting numbers in a file), use the -n option:
sort -n filename.txt- This sorts numbers in ascending numerical order.
Example:
File numbers.txt:
23
5
78
2
15
Command:
sort -n numbers.txtOutput:
2
5
15
23
78
If you want to sort the file in reverse order, you can add the -r flag:
sort -n -r filename.txt2. Pasting Two Files Together (paste)
The paste command is used to merge or join two files line by line.
Basic Usage:
If you want to paste the contents of two files side by side, use:
paste file1.txt file2.txt- This command will combine the contents of
file1.txtandfile2.txtby joining corresponding lines.
Example:
File file1.txt:
John
Alice
Bob
File file2.txt:
25
30
22
Command:
paste file1.txt file2.txtOutput:
John 25
Alice 30
Bob 22
Using a Custom Delimiter:
By default, paste separates the columns with a tab, but you can use the -d option to specify a custom delimiter:
paste -d "," file1.txt file2.txtOutput:
John,25
Alice,30
Bob,22
3. Splitting a File into Multiple Files (split)
The split command divides a file into multiple smaller files.
Basic Usage:
To split a file into smaller chunks, use:
split filename.txt- By default, this splits the file into files with 1000 lines each (or fewer, for the last file).
This will create files named xaa, xab, xac, and so on.
Specifying Number of Lines per File:
You can specify how many lines each output file should contain. For example, to split filename.txt into files with 10 lines each, use:
split -l 10 filename.txtThis will create files like xaa, xab, etc., each containing 10 lines from the original file.
Naming the Output Files:
If you want to specify the prefix for the output files, you can use:
split -l 10 filename.txt output_prefix_This will create files named output_prefix_aa, output_prefix_ab, and so on.
4. Extracting Certain Fields from Text File Records (cut)
The gmailcut command is used to extract specific columns or fields from a file.
Basic Usage:
If you have a text file with delimited fields (e.g., comma-separated values), you can extract specific columns using the -f option:
cut -d "," -f 1,3 filename.txt-dspecifies the delimiter (in this case, a comma).-fspecifies which fields (columns) to extract. You can provide multiple fields, such as1,3to extract the first and third fields.
Example:
File data.txt:
John,25,Male
Alice,30,Female
Bob,22,Male
Command:
cut -d "," -f 1,3 data.txtOutput:
John,Male
Alice,Female
Bob,Male
Extracting by Character Position:
If you want to extract characters from specific positions in each line, you can use the -c option:
cut -c 1-5 filename.txtThis extracts the characters from positions 1 to 5 in each line.
Example:
File text.txt:
123456789
abcdefghi
987654321
Command:
cut -c 1-5 text.txtOutput:
12345
abcde
98765
Summary of Commands:
sort: Sorts lines of text files. Use-nfor numerical sorting.paste: Merges two or more files side by side.split: Splits a file into smaller parts.cut: Extracts specific fields or characters from each line in a file.