Tuesday, 21 January 2025

sed, grep ,find Command Questions

  1. 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
      
  2. Delete lines matching a pattern:

    • Delete all lines containing the word "DEBUG".
    • Answer:
      sed '/DEBUG/d' file.txt
      
  3. 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
      
  4. 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
      
  5. Extract lines between two patterns:

    • Extract lines between "START" and "END".
    • Answer:
      sed -n '/START/,/END/p' file.txt
      
  6. Replace tabs with spaces:

    • Replace all tabs in a file with 4 spaces.
    • Answer:
      sed 's/\t/    /g' file.txt
      
  7. Modify a file in place:

    • Change "FAILED" to "SUCCESS" directly in the file.
    • Answer:
      sed -i 's/FAILED/SUCCESS/g' file.txt
      
  8. Print line numbers of matching patterns:

    • Print line numbers of lines containing "ERROR".
    • Answer:
      sed -n '/ERROR/=' file.txt
      
  9. Remove trailing spaces:

    • Remove trailing spaces from all lines in a file.
    • Answer:
      sed 's/[[:space:]]*$//' file.txt
      
  10. 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)

  1. Search recursively in subdirectories:

    • Find the word "FAILED" in all files in /logs.
    • Answer:
      grep -r 'FAILED' /logs
      
  2. Match exact word:

    • Find lines with the exact word "Job".
    • Answer:
      grep -w 'Job' file.txt
      
  3. Display matching lines with line numbers:

    • Find "ERROR" and show line numbers.
    • Answer:
      grep -n 'ERROR' file.txt
      
  4. Exclude specific files:

    • Search for "SUCCESS" in all .log files but exclude files containing "debug".
    • Answer:
      grep --exclude='*debug*.log' 'SUCCESS' *.log
      
  5. Find lines starting with a specific word:

    • Find lines that start with "INFO".
    • Answer:
      grep '^INFO' file.txt
      
  6. Search case-insensitively:

    • Search for "error" without case sensitivity.
    • Answer:
      grep -i 'error' file.txt
      
  7. Search and count occurrences:

    • Count occurrences of "Job Finished".
    • Answer:
      grep -c 'Job Finished' file.txt
      
  8. Search in multiple files:

    • Find "ERROR" in file1.log and file2.log.
    • Answer:
      grep 'ERROR' file1.log file2.log
      
  9. Context around matches:

    • Show 3 lines before and after each match for "ERROR".
    • Answer:
      grep -C 3 'ERROR' file.txt
      
  10. Inverse search:

    • Show all lines that do not contain "DEBUG".
    • Answer:
      grep -v 'DEBUG' file.txt
      

find (File Locator)

  1. Find files by name:

    • Find all .log files in /var/log.
    • Answer:
      find /var/log -name "*.log"
      
  2. Find files by size:

    • Find files larger than 1GB.
    • Answer:
      find / -type f -size +1G
      
  3. Find files modified recently:

    • Find files modified in the last 7 days.
    • Answer:
      find /path/to/dir -type f -mtime -7
      
  4. Find empty files:

    • Find all empty files in /data.
    • Answer:
      find /data -type f -empty
      
  5. Find files with specific permissions:

    • Find files with 777 permissions.
    • Answer:
      find / -type f -perm 777
      
  6. Delete specific files:

    • Delete all .tmp files older than 30 days.
    • Answer:
      find /path/to/dir -name "*.tmp" -type f -mtime +30 -exec rm {} \;
      
  7. Find and execute commands:

    • Find .log files and compress them.
    • Answer:
      find /logs -name "*.log" -exec gzip {} \;
      
  8. Search by file type:

    • Find all directories in /var.
    • Answer:
      find /var -type d
      
  9. Find recently accessed files:

    • Find files accessed in the last 2 days.
    • Answer:
      find /path/to/dir -atime -2
      
  10. Find files with partial names:

    • Find files containing "log" in their names.
    • Answer:
      find / -name "*log*"
      

awk (Text Processing)

  1. Print specific columns:

    • Print the 2nd and 4th columns from a file.
    • Answer:
      awk '{print $2, $4}' file.txt
      
  2. Filter lines by a condition:

    • Print lines where the value in column 3 is greater than 100.
    • Answer:
      awk '$3 > 100' file.txt
      
  3. Count occurrences of a word:

    • Count occurrences of "ERROR" in a file.
    • Answer:
      awk '/ERROR/ {count++} END {print count}' file.txt
      
  4. Sum values in a column:

    • Calculate the sum of column 5.
    • Answer:
      awk '{sum += $5} END {print sum}' file.txt
      
  5. 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
      
  6. 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
      
  7. Format output:

    • Align columns with custom spacing.
    • Answer:
      awk '{printf "%-10s %-20s %-10s\n", $1, $2, $3}' file.txt
      
  8. Replace column values:

    • Replace "ERROR" with "OK" in column 4.
    • Answer:
      awk '$4 == "ERROR" {$4 = "OK"} 1' file.txt
      
  9. Process CSV files:

    • Print the second field of a CSV file.
    • Answer:
      awk -F, '{print $2}' file.csv
      
  10. 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

how to create dml dynamically in Ab-initio

 $[ begin let int i = 0; let string(int) complete_list = "emp_nm,billable,age,designation,location"; let string(int) file_content ...