簡體   English   中英

如何使用Shell腳本和sed刪除文本文件中的字符串后綴?

[英]How to remove the suffix of string in text file using shell script and sed?

我有一個名為file.txt的文本文件,其中包含以下內容。

customer 
#line1-customer
security-team
#line1-security 

我只想使用shell腳本刪除此文件內客戶下的#line1號#

我的劇本

#!/bin/bash
string1="#line1-customer"
prefix=#

cat file.text | while read LINE
do
  if [ "$LINE" == "$string1" ]; then
  sed -i 's/"$LINE"/"$LINE#"$prefix""/g' file.text
done

執行后,未發現錯誤。 但是我檢查了file.txt,它仍然是相同的(#未被刪除)

我如何實現我的目標?

如果我理解正確,則要刪除該行的第一個字符,該string1從變量string1包含的模式開始。

如果是這樣,則不需要bash腳本,一個sed腳本就足夠了:

string1="#line1"
sed -i "/^${string1}/s/.\(.*\)$/\1/" file.txt

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM