2021-06-18

Get file content without comment

#Select all content, paste it into linux shell
#__________[CONFIG]:BEGIN
FILE_NAME="/opt/script/file-uncommented-line.sh"
now1="$(date +'%Y.%m.%d-%H.%M.%S.%3N')"
chattr -i -f $FILE_NAME
cp -v $FILE_NAME $FILE_NAME-[$now1].bk

cat <<EOF > $FILE_NAME
#!/bin/bash
#!/bin/sh -e
###################################
#FILE_NAME: $FILE_NAME
#Author: qwerty
#LastUpdate: #$now1
###################################
#Usage: file-uncommented-line.sh <filename>
###################################CONTENT:BEGIN
now1="\$(date +'%Y.%m.%d-%H.%M.%S.%3N')"
TEMP_LOC="/opt/temp"; mkdir -p \$TEMP_LOC

FileWithFullPath="\$1"
FileNameOnly="\${FileWithFullPath##*/}"
FileName_WithoutExtension="\${FileNameOnly%.*}"
FileExtensionOnly="\${FileNameOnly##*.}"

echo "FileWithFullPath         : \$FileWithFullPath"
echo "FileNameOnly             : \$FileNameOnly"
echo "FileName_WithoutExtension: \$FileName_WithoutExtension"
echo "FileExtensionOnly        : \$FileExtensionOnly"

echo "=========================="
echo "REMOVE HASH COMMENT: DONE"
grep -v '^ *#' \$FileWithFullPath > \$TEMP_LOC/\$FileName_WithoutExtension"_RemovedHash".\$FileExtensionOnly
sleep 2


echo "=========================="
echo "REMOVE EMPTY LINES: DONE"
sed '/^\$/d' \$TEMP_LOC/\$FileName_WithoutExtension"_RemovedHash".\$FileExtensionOnly > \$TEMP_LOC/\$FileName_WithoutExtension"_RemovedEmpyLines".\$FileExtensionOnly
sleep 2


echo "=========================="
echo "RESULT: "
ls -l \$TEMP_LOC/\$FileName_WithoutExtension*
###################################CONTENT:END
#THE-END
EOF

cat $FILE_NAME
chmod +x $FILE_NAME
chattr +i -f $FILE_NAME
echo "FILE_NAME: $FILE_NAME"; echo ""
#__________[CONFIG]:END



#RESULT:
root@host /opt/temp# file-uncommented-line.sh /etc/MailScanner/MailScanner.conf 
FileWithFullPath         : /etc/MailScanner/MailScanner.conf
FileNameOnly             : MailScanner.conf
FileName_WithoutExtension: MailScanner
FileExtensionOnly        : conf
==========================
REMOVE HASH COMMENT: DONE
==========================
REMOVE EMPTY LINES: DONE
==========================
RESULT: 
-rw-r--r--. 1 root root 14609 Jun 18 14:22 /opt/temp/MailScanner_RemovedEmpyLines.conf
-rw-r--r--. 1 root root 14983 Jun 18 14:22 /opt/temp/MailScanner_RemovedHash.conf
root@host /opt/temp# 

No comments:

Post a Comment