Skip to content
Advertisement

How to replace ‘>> $log_file 2>&1’ from all files

In my files commands are like :

File1.sh:

log_file='20210101'
echo -e "n Output is n" >> $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 "n Output is n"

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

Advertisement

Answer

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*
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement