You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何匹配指定模式删除/var/log/script1/日志文件内容并回写至原文件?

Solution to Remove "at java.util.conc..." Entries from Log Files

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, use sed -i.bak instead—this creates a backup of each file with a .bak suffix before making changes.
    • /at java\.util\.conc/d: The regex matches any line containing the exact string at java.util.conc (we escape the . because it's a special character in regex), and d tells 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. The g ensures we replace all occurrences in a line, not just the first one.
    • at java\.util\.conc[^[:space:]]*: Matches at java.util.conc followed by any non-space characters (this captures the full stack trace entry like at 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:20:57