Linuxsuite 6
Linuxsuite 6
sort -f names.txt
• echo "10" > numbers.txt
• echo "3" >> numbers.txt
• echo "25" >> numbers.txt
• echo "1" >> numbers.txt
• echo "100" >> numbers.txt
sort -n numbers.txt
• Reverse order
sort -r names.txt
• uniq [OPTION] [INPUT [OUTPUT]]
Key Options
•-c : Prefixes each line with the number of
occurrences.
•-d : Only displays duplicate lines.
•-u : Only displays unique lines.
•-i : Ignores case differences when comparing lines.
•-f : Skips a specified number of fields before
determining uniqueness.
•-s : Skips a specified number of characters before
determining uniqueness.
•-w : Limits comparison to a specific number of
characters in each line.
•--help : Displays help information about the uniq
command.
Suppose fruit.txt
• apple
• apple
• banana
• banana
• apple
• cherry
• cherry
• cherry
• uniq fruits.txt
• uniq removes duplicates only if they are adjacent.
uniq -w 5 short_words.txt
To save the results of removing duplicate lines in a new file unique_fruits.txt:
uniq fruits.txt unique_fruits.txt
• echo "Apple" > fruits.txt
• echo "Banana" >> fruits.txt
• echo "Banana" >> fruits.txt
• echo "Cherry" >> fruits.txt
• echo "Cherry" >> fruits.txt
• echo "Cherry" >> fruits.txt
• echo "Grapes" >> fruits.txt
• echo "Lemon" >> fruits.txt
uniq fruits.txt
• echo "Apple" > fruits.txt
• echo "Banana" >> fruits.txt
• echo "Cherry" >> fruits.txt
• echo "Banana" >> fruits.txt
• echo "Cherry" >> fruits.txt
• uniq removes only consecutive duplicate lines. If you want to count
the number of occurrences of each unique line, you can use the -c
option:
• Uniq -c fruits.txt
• Other alternative
• sort fruits.txt | uniq
• Display Duplicate Lines Only
uniq –d
• display only the lines that occur exactly once:
uniq –u
• grep -i Case-Insensitive Search
grep -i "apple" fruits.txt
grep -v Invert Match
grep -v "banana" fruits.txt
grep -f Search with Patterns from a File
• # Create patterns.txt with patterns
• echo "apple" > patterns.txt
• echo "banana" >> patterns.txt
Print lines 1 to 3:
Substitute All Occurrences (g flag): Replace all occurrences of "line" with "sentence
Insert (i) Command: Insert a new line before the second line.
Append (a) Command: Append a new line after the fourth line.
Print (p) Command: Print only the second and fourth lines.
Read (r) Command: Insert the content of another file (file2.txt) after line 3.
• sed 's/apple/orange/g' fruits.txt
• Delete line
• sed '/banana/d' fruits.txt
• Add or Append Text:
• sed 's/apple/Delicious &/g' fruits.txt
• Insert Text:
For example, to insert "This is the header" before the first line
• sed '1i\This is the header' file.txt
• Print Specific Lines:
sed -n '5,10p' file.txt
Basic Structure:
Key Concepts:
•Fields: Columns of each line, accessed using $1, $2, … $NF (last field).
•Records: Each line is a record, referred to as $0.
Examples and Use Cases:
1 Alice 25
2 Bob 30
3 Charlie 22
awk 'BEGIN { print "ID Name Age" } { print $1, $2, $3 } END { print "End of file" }' data.txt
4. Field Separator:
If the input uses a delimiter other than space or tab, use the -F option to specify the field separator.
1,Alice,25
2,Bob,30
3,Charlie,22
John 78 85 92
Alice 88 90 85
Bob 65 70 75
awk '{ avg = ($2 + $3 + $4) / 3; print $1, avg }' marks.txt
awk '{ for(i=1; i<=NF; i++) count[$i]++ } END { for(word in count) print word, count[word] }' words.txt
awk '{ expenses[$1] += $2 } END { for(item in expenses) print item, expenses[item] }' expenses.txt