Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

linux - How to replace '>> $log_file 2>&1' from all files

In my files commands are like :

File1.sh:

log_file='20210101'
echo -e "
 Output is 
" >> $log_file 2>&1

File2.sh

echo "Something"
log_file='20210102'
hive -e -f my_file_name.hql >> $log_file 2>&1

File3.sh

cat myfile
echo "something"
ehco "new line"
log_file='2021-01-01_23_59_59'
hive -e "Select * from tables' >> $log_file 2>&1

I am looking for a command to remove the line where defined variable 'log_file' from all the files AND remove only '>> $log_file 2>&1' part from all the files

Output in files:

File1.sh: echo -e " Output is "

File2.sh

echo "Something"
hive -e -f my_file_name.hql

File3.sh

cat myfile
echo "something"
ehco "new line"
hive -e "Select * from tables'

I tried multiple ways with sed but nothing worked, few of them are :

I tried : sed -e 's/*[>> $]log_file//g' test.sh sed -e 's/*[>> $]log_file[ 2>&1]//g' test.sh sed -e 's/>> $log_file 2>$//' test.sh sed -e 's/">> $log_file 2>&1"/""/g' test.sh


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This sed command is a two-step process, and makes a backup of all files (just in case); on my machine (and with the 3 samples you posted) it does the right thing:

sed -i.bak -e '/^log_file=/d' -e 's/>> $log_file 2>&1//g' File*

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...