sed Command Reference
Displaying Specific Lines
To print the 10th line of /etc/passwd:
sed -n '10p' /etc/passwd
To print lines 8 through 15 of /etc/passwd:
sed -n '8,15p' /etc/passwd
To print line 8 and the next 5 lines of /etc/passwd:
sed -n '8,+5p' /etc/passwd
Pattern Matching
To print lines starting with 'nginx' in /etc/passwd:
sed -n '/^nginx/p' /etc/passwd
To print from a line starting with 'root' to a line starting with 'ftp' in /etc/passwd:
sed -n '/^root/,/^ftp/p' /etc/passwd
Combined Line Numbers and Patterns
To print from line 8 to the line containing '/sbin/nologin' in /etc/passwd:
sed -n '8,/\/sbin\/nologin/p' /etc/passwd
To print from the line containing '/bin/bash' to line 5 in /etc/passwd:
sed -n '/\/bin\/bash/,5p' /etc/passwd
Configuration File Processing Script
Objective
Develop a script to process a configuration file similar to my.cnf that:
- Counts the number of sections in the file
- For each section, counts the total number of configuration parameters
Expected Output
1: client 2
2: server 12
3: mysqld 12
4: mysqld_safe 7
5: embedded 8
6: mysqld-5.5 9
Implementation
Function to Extract All Section Names
To find all section names in the configuration file:
sed -n '/\[.*\]/p' config_file | sed -e 's/\[/\g' -e 's/\]//g'
The '.' matches any character, '*' represents zero or more characters, and '-e' allows multiple sed commands to be chained.
Configuration Processor Script
#!/bin/bash
CONFIG_FILE="/path/to/config.cnf"
# Function to retrieve all section names
get_all_sections() {
echo $(sed -n '/\[.*\]/p' "$CONFIG_FILE" | sed -e 's/\[/\g' -e 's/\]//g')
}
# Function to count items in a specific section
count_items_in_section() {
# Find content between [section] and the next [.*]
# Filter out comment lines and empty lines
items=$(sed -n '/\['"$1"'\],/\[.*\]/p' "$CONFIG_FILE" | \
grep -v "^$" | grep -v "^#" | grep -v "\[.*\]" | wc -l)
echo $items
}
section_number=0
for section in $(get_all_sections)
do
section_number=$((section_number + 1))
item_count=$(count_items_in_section "$section")
echo "$section_number: $section $item_count"
done
Function Details
get_all_sections Function
This function identifies all section headers in the configuration file by:
- Using sed to find all lines containing section headers (enclosed in [ ])
- Removing the brackets using sed substitution commands
- Returning the section names
count_items_in_section Function
This function counts the configuration items in a specified section by:
- Extracting the content between the specified section header and the next section header
- Filtering out empty lines, comment lines (starting with #), and section headers
- Counting the remaining lines using word count utility