如何匹配指定模式删除/var/log/script1/日志文件内容并回写至原文件?
Let's walk through how to safely clean up those unwanted stack trace entries from your log files in /var/log/script1/. First off, always back up your files before making any edits—this is a must to avoid accidental data loss.
Step 1: Create a Backup of All Log Files
First, set up a backup directory and copy all your logs there to keep a safe copy:
mkdir -p /var/log/script1/backup/ cp /var/log/script1/*.log* /var/log/script1/backup/
Step 2: Choose the Right Command for Your Needs
There are two common scenarios here—pick the one that fits exactly what you want to do:
Scenario 1: Delete Entire Lines Containing "at java.util.conc..."
If you want to remove the full stack trace line that includes this pattern (the usual case for these log entries), use this batch command to process all files:
for file in /var/log/script1/*.log*; do sed -i '/at java\.util\.conc/d' "$file" done
- Quick breakdown:
sed -i: Edits the file in-place (directly modifies the original). If you're on macOS/BSD, usesed -i.bakinstead—this creates a backup of each file with a.baksuffix before making changes./at java\.util\.conc/d: The regex matches any line containing the exact stringat java.util.conc(we escape the.because it's a special character in regex), anddtells sed to delete that line entirely.
Scenario 2: Remove Only the "at java.util.conc..." Substring (Keep the Rest of the Line)
If you want to keep the line but just strip out the stack trace fragment itself, use this substitution command:
for file in /var/log/script1/*.log*; do sed -i 's/at java\.util\.conc[^[:space:]]*//g' "$file" done
- Quick breakdown:
s/pattern/replacement/g: Sed's substitution command. Thegensures we replace all occurrences in a line, not just the first one.at java\.util\.conc[^[:space:]]*: Matchesat java.util.concfollowed by any non-space characters (this captures the full stack trace entry likeat java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)).//: Replaces the matched text with nothing (effectively deleting it).
Step 3: Test First (Critical!)
Before running the batch command on all files, test it on one log to confirm it does what you expect:
# Test Scenario 1 (delete full lines) sed '/at java\.util\.conc/d' /var/log/script1/controller.log | head -20 # Test Scenario 2 (remove only the substring) sed 's/at java\.util\.conc[^[:space:]]*//g' /var/log/script1/controller.log | head -20
This will show you the first 20 lines of the modified output without touching the original file. Tweak the regex if needed based on what you see.
内容的提问来源于stack exchange,提问作者Akshay

