Replace a word only in specific lines:
- Replace "ERROR" with "WARNING" only in lines 10 to 20.
- Answer:
sed '10,20s/ERROR/WARNING/g' file.txt
-
Delete lines matching a pattern:
- Delete all lines containing the word "DEBUG".
- Answer:
sed '/DEBUG/d' file.txt
-
Insert a line after a specific pattern:
- Insert "Processing complete" after every line containing "Job Finished".
- Answer:
sed '/Job Finished/a\Processing complete' file.txt
-
Replace nth occurrence of a word:
- Replace the 3rd occurrence of "Job" in each line with "Task".
- Answer:
sed 's/Job/Task/3' file.txt
-
Extract lines between two patterns:
- Extract lines between "START" and "END".
- Answer:
sed -n '/START/,/END/p' file.txt
-
Replace tabs with spaces:
- Replace all tabs in a file with 4 spaces.
- Answer:
sed 's/\t/ /g' file.txt
-
Modify a file in place:
- Change "FAILED" to "SUCCESS" directly in the file.
- Answer:
sed -i 's/FAILED/SUCCESS/g' file.txt
-
Print line numbers of matching patterns:
- Print line numbers of lines containing "ERROR".
- Answer:
sed -n '/ERROR/=' file.txt
-
Remove trailing spaces:
- Remove trailing spaces from all lines in a file.
- Answer:
sed 's/[[:space:]]*$//' file.txt
-
Replace using a variable:
- Replace "PLACEHOLDER" with the value of a variable
$var.
- Answer:
sed "s/PLACEHOLDER/$var/g" file.txt
Replace a word only in specific lines:
- Replace "ERROR" with "WARNING" only in lines 10 to 20.
- Answer:
sed '10,20s/ERROR/WARNING/g' file.txt
Delete lines matching a pattern:
- Delete all lines containing the word "DEBUG".
- Answer:
sed '/DEBUG/d' file.txt
Insert a line after a specific pattern:
- Insert "Processing complete" after every line containing "Job Finished".
- Answer:
sed '/Job Finished/a\Processing complete' file.txt
Replace nth occurrence of a word:
- Replace the 3rd occurrence of "Job" in each line with "Task".
- Answer:
sed 's/Job/Task/3' file.txt
Extract lines between two patterns:
- Extract lines between "START" and "END".
- Answer:
sed -n '/START/,/END/p' file.txt
Replace tabs with spaces:
- Replace all tabs in a file with 4 spaces.
- Answer:
sed 's/\t/ /g' file.txt
Modify a file in place:
- Change "FAILED" to "SUCCESS" directly in the file.
- Answer:
sed -i 's/FAILED/SUCCESS/g' file.txt
Print line numbers of matching patterns:
- Print line numbers of lines containing "ERROR".
- Answer:
sed -n '/ERROR/=' file.txt
Remove trailing spaces:
- Remove trailing spaces from all lines in a file.
- Answer:
sed 's/[[:space:]]*$//' file.txt
Replace using a variable:
- Replace "PLACEHOLDER" with the value of a variable
$var. - Answer:
sed "s/PLACEHOLDER/$var/g" file.txt
grep (Global Regular Expression Print)
-
Search recursively in subdirectories:
- Find the word "FAILED" in all files in
/logs.
- Answer:
grep -r 'FAILED' /logs
-
Match exact word:
- Find lines with the exact word "Job".
- Answer:
grep -w 'Job' file.txt
-
Display matching lines with line numbers:
- Find "ERROR" and show line numbers.
- Answer:
grep -n 'ERROR' file.txt
-
Exclude specific files:
- Search for "SUCCESS" in all
.log files but exclude files containing "debug".
- Answer:
grep --exclude='*debug*.log' 'SUCCESS' *.log
-
Find lines starting with a specific word:
- Find lines that start with "INFO".
- Answer:
grep '^INFO' file.txt
-
Search case-insensitively:
- Search for "error" without case sensitivity.
- Answer:
grep -i 'error' file.txt
-
Search and count occurrences:
- Count occurrences of "Job Finished".
- Answer:
grep -c 'Job Finished' file.txt
-
Search in multiple files:
- Find "ERROR" in
file1.log and file2.log.
- Answer:
grep 'ERROR' file1.log file2.log
-
Context around matches:
- Show 3 lines before and after each match for "ERROR".
- Answer:
grep -C 3 'ERROR' file.txt
-
Inverse search:
- Show all lines that do not contain "DEBUG".
- Answer:
grep -v 'DEBUG' file.txt
Search recursively in subdirectories:
- Find the word "FAILED" in all files in
/logs. - Answer:
grep -r 'FAILED' /logs
Match exact word:
- Find lines with the exact word "Job".
- Answer:
grep -w 'Job' file.txt
Display matching lines with line numbers:
- Find "ERROR" and show line numbers.
- Answer:
grep -n 'ERROR' file.txt
Exclude specific files:
- Search for "SUCCESS" in all
.logfiles but exclude files containing "debug". - Answer:
grep --exclude='*debug*.log' 'SUCCESS' *.log
Find lines starting with a specific word:
- Find lines that start with "INFO".
- Answer:
grep '^INFO' file.txt
Search case-insensitively:
- Search for "error" without case sensitivity.
- Answer:
grep -i 'error' file.txt
Search and count occurrences:
- Count occurrences of "Job Finished".
- Answer:
grep -c 'Job Finished' file.txt
Search in multiple files:
- Find "ERROR" in
file1.logandfile2.log. - Answer:
grep 'ERROR' file1.log file2.log
Context around matches:
- Show 3 lines before and after each match for "ERROR".
- Answer:
grep -C 3 'ERROR' file.txt
Inverse search:
- Show all lines that do not contain "DEBUG".
- Answer:
grep -v 'DEBUG' file.txt
find (File Locator)
-
Find files by name:
- Find all
.log files in /var/log.
- Answer:
find /var/log -name "*.log"
-
Find files by size:
- Find files larger than 1GB.
- Answer:
find / -type f -size +1G
-
Find files modified recently:
- Find files modified in the last 7 days.
- Answer:
find /path/to/dir -type f -mtime -7
-
Find empty files:
- Find all empty files in
/data.
- Answer:
find /data -type f -empty
-
Find files with specific permissions:
- Find files with
777 permissions.
- Answer:
find / -type f -perm 777
-
Delete specific files:
- Delete all
.tmp files older than 30 days.
- Answer:
find /path/to/dir -name "*.tmp" -type f -mtime +30 -exec rm {} \;
-
Find and execute commands:
- Find
.log files and compress them.
- Answer:
find /logs -name "*.log" -exec gzip {} \;
-
Search by file type:
- Find all directories in
/var.
- Answer:
find /var -type d
-
Find recently accessed files:
- Find files accessed in the last 2 days.
- Answer:
find /path/to/dir -atime -2
-
Find files with partial names:
- Find files containing "log" in their names.
- Answer:
find / -name "*log*"
Find files by name:
- Find all
.logfiles in/var/log. - Answer:
find /var/log -name "*.log"
Find files by size:
- Find files larger than 1GB.
- Answer:
find / -type f -size +1G
Find files modified recently:
- Find files modified in the last 7 days.
- Answer:
find /path/to/dir -type f -mtime -7
Find empty files:
- Find all empty files in
/data. - Answer:
find /data -type f -empty
Find files with specific permissions:
- Find files with
777permissions. - Answer:
find / -type f -perm 777
Delete specific files:
- Delete all
.tmpfiles older than 30 days. - Answer:
find /path/to/dir -name "*.tmp" -type f -mtime +30 -exec rm {} \;
Find and execute commands:
- Find
.logfiles and compress them. - Answer:
find /logs -name "*.log" -exec gzip {} \;
Search by file type:
- Find all directories in
/var. - Answer:
find /var -type d
Find recently accessed files:
- Find files accessed in the last 2 days.
- Answer:
find /path/to/dir -atime -2
Find files with partial names:
- Find files containing "log" in their names.
- Answer:
find / -name "*log*"
awk (Text Processing)
-
Print specific columns:
- Print the 2nd and 4th columns from a file.
- Answer:
awk '{print $2, $4}' file.txt
-
Filter lines by a condition:
- Print lines where the value in column 3 is greater than 100.
- Answer:
awk '$3 > 100' file.txt
-
Count occurrences of a word:
- Count occurrences of "ERROR" in a file.
- Answer:
awk '/ERROR/ {count++} END {print count}' file.txt
-
Sum values in a column:
- Calculate the sum of column 5.
- Answer:
awk '{sum += $5} END {print sum}' file.txt
-
Find the maximum value in a column:
- Find the maximum value in column 3.
- Answer:
awk 'BEGIN {max=0} $3 > max {max=$3} END {print max}' file.txt
-
Add a header to output:
- Add "ID Name Salary" as a header.
- Answer:
awk 'BEGIN {print "ID Name Salary"} {print $1, $2, $3}' file.txt
-
Format output:
- Align columns with custom spacing.
- Answer:
awk '{printf "%-10s %-20s %-10s\n", $1, $2, $3}' file.txt
-
Replace column values:
- Replace "ERROR" with "OK" in column 4.
- Answer:
awk '$4 == "ERROR" {$4 = "OK"} 1' file.txt
-
Process CSV files:
- Print the second field of a CSV file.
- Answer:
awk -F, '{print $2}' file.csv
-
Conditional processing:
- Print "High" if column 3 > 100, otherwise "Low".
- Answer:
awk '{if ($3 > 100) print "High"; else print "Low"}' file.txt
Print specific columns:
- Print the 2nd and 4th columns from a file.
- Answer:
awk '{print $2, $4}' file.txt
Filter lines by a condition:
- Print lines where the value in column 3 is greater than 100.
- Answer:
awk '$3 > 100' file.txt
Count occurrences of a word:
- Count occurrences of "ERROR" in a file.
- Answer:
awk '/ERROR/ {count++} END {print count}' file.txt
Sum values in a column:
- Calculate the sum of column 5.
- Answer:
awk '{sum += $5} END {print sum}' file.txt
Find the maximum value in a column:
- Find the maximum value in column 3.
- Answer:
awk 'BEGIN {max=0} $3 > max {max=$3} END {print max}' file.txt
Add a header to output:
- Add "ID Name Salary" as a header.
- Answer:
awk 'BEGIN {print "ID Name Salary"} {print $1, $2, $3}' file.txt
Format output:
- Align columns with custom spacing.
- Answer:
awk '{printf "%-10s %-20s %-10s\n", $1, $2, $3}' file.txt
Replace column values:
- Replace "ERROR" with "OK" in column 4.
- Answer:
awk '$4 == "ERROR" {$4 = "OK"} 1' file.txt
Process CSV files:
- Print the second field of a CSV file.
- Answer:
awk -F, '{print $2}' file.csv
Conditional processing:
- Print "High" if column 3 > 100, otherwise "Low".
- Answer:
awk '{if ($3 > 100) print "High"; else print "Low"}' file.txt
No comments:
Post a Comment