Why is string processing used?
String processing is commonly used to filter and extract specific parts of output from commands such as ifconfig, ip a, etc.
-
headCommand
Theheadcommand is used to display the first few lines of a file. By default, it shows the first 10 lines.head 1.txt # Displays the first 10 lines of 1.txt head -n 5 1.txt # Displays the first 5 lines of 1.txt head -n 3 1.txt # Displays the first 3 lines of 1.txt head -c 50 1.txt # Displays the first 50 bytes of 1.txt
You can also display the first few lines from multiple files:
head 1.txt messages /var/log/centos.rep # Displays first 10 lines from all three files -
tailCommand
Thetailcommand is used to display the last few lines of a file. It shows the last 10 lines by default.tail 1.txt messages passwd centos.rep # Displays the last 10 lines of each file tail -f /var/log # Displays the last 10 lines of log files and updates when new entries appear
-
wcCommand
Thewccommand counts the lines, words, and bytes in a file.wc 1.txt # Output: lines words bytes wc -l 1.txt # Counts the number of lines wc -w 1.txt # Counts the number of words wc -c 1.txt # Counts the number of bytes
-
Counting for Multiple Files
wc * # Counts lines, words, and bytes for all files ls -1 # Lists files in the directory wc -l # Counts the number of files
-
Basic Sorting
sort test1.txt # Sorts lines alphabetically -
Additional Sort Options
-
Sort in numeric order (for numbers)
sort -h test1.txt # Sorts numbers in ascending order -
Sort in reverse order
sort -r test1.txt # Sorts lines in reverse order -
Sort randomly
sort -R test1.txt # Sorts lines randomly -
Remove duplicates
sort test1.txt | uniq -c # Removes duplicates and shows their count
-
-
Sorting by Specific Columns
sort -k 2 test1.txt # Sorts by the second column -
Handling Delimited Files
sort -t ',' -k 2 test1.txt # Sorts by the second column of a comma-separated file
-
Sorting by Month
sort -M test1.txt # Sorts by month names
- Monitor the output of a command periodically
watch -n 5 date # Displays the current date every 5 seconds watch -n 1 date # Displays the current date every second
-
Basic Search
grep searchword filename # Finds occurrences of 'searchword' in the file grep "root" /etc/passwd # Finds 'root' in the /etc/passwd file
-
Search in Multiple Files
grep root /etc/passwd /etc/shadow # Finds 'root' in both files -
Ignore Case (Case-insensitive search)
grep -i root messages # Finds both 'root' and 'Root' -
Search for Multiple Words
grep -E "(session|root|mounting)" var/log/messages # Finds 'session', 'root', or 'mounting'
-
Search for Words in the Same Line
grep "session" var/log/messages | grep root # Finds lines containing both 'session' and 'root'
-
Exclude a Word (Using
-v)grep "session" var/log/messages | grep -v root # Finds lines with 'session' but not 'root'
-
Search for a Word at the Beginning or End of a Line
- Word at the beginning of a line:
grep "^root" var/log/messages # Finds lines where 'root' appears at the start
- Word at the end of a line:
grep "root.$" var/log/messages # Finds lines where 'root' appears at the end
- Word at the beginning of a line:
-
Search by Date
grep "^2sep" var/log/messages | grep root # Finds logs for 'root' on 2nd September
-
Find Single Character After a Word
grep 'roo.' filename # Finds 'roo' followed by any single character
-
Find Multiple Characters After a Word
grep 'roo..' filename # Finds 'roo' followed by two characters
-
Find Empty Lines
grep '^$' filename # Finds empty lines
-
Exclude Lines Starting with
#(e.g., Comments)grep -v '^#' ssd_config # Excludes lines starting with #
-
Find Alphanumeric Characters
grep "[[:alnum:]]" filename # Finds all alphanumeric characters
-
Search for a Pattern from Another File
grep -f domain.txt url.txt # Searches for domains from domain.txt in url.txt
-
Extract Columns from CSV File
cut -d ',' -f 2 my-csv.csv # Extracts the second column (e.g., names) cut -d ',' -f 1,3 my-csv.csv # Extracts columns 1 and 3 cut -d ',' -f 1-3 my-csv.csv # Extracts columns 1 to 3
-
Replace Commas with Spaces
cut my-csv.csv | tr ',' ' ' # Replaces commas with spaces
-
Find Specific IP Address
ifconfig | grep 'inet' | cut -d ' ' -f 9 # Extracts the IPv4 address
-
Combine Files Column-wise
paste name.txt surname.txt # Joins columns from two files paste -d ' ' name.txt surname.txt # Joins with space delimiter
-
Save Output to a New File
paste -d ' ' name.txt surname.txt > fullname.txt # Saves the output in fullname.txt