如何使用 sed 或 awk 從“ [19/Mar/2020:05:57:09 ”字串中刪除“ [ ”符號?

如何使用 sed 或 awk 從“ [19/Mar/2020:05:57:09 ”字串中刪除“ [ ”符號?

如何去除[從 ”[19/Mar/2020:05:57:09“使用sedawk

我需要[從日誌檔案的日期和時間中刪除“”。

答案1

這裡有一些解決方案:

# Remove with tr and write changes to second file
tr -d '[' < file > file_edited

# Remove with sed and write changes to second file
sed 's/\[//' file > file_edited

# Remove with sed and edit the file inplace!
sed -i 's/\[//' file

請注意,tr解決方案將刪除[檔案中的所有內容,sed解決方案將刪除每行的第一行[。如果這不完全是您想要的,您需要編輯您的問題並提供更多詳細資訊。

答案2

您也可以使用cut[如果您確信它將出現在字串的開頭,則刪除[並將輸出寫入文件tee

cut -b2- input_file | tee output_file

相關內容