<space>10.99.9.99<space>2017-03-27 23:44:49 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#Date: 2017-03-27 23:44:49
2017-03-27 23:44:49 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
/bin/awk '{
if ( 1 == NR)
{
printf( "#Date: %s %s\n", $2, $3);
}
for ( Loc=2; Loc<=NF; Loc++)
{
printf( "%s ", $Loc);
}
printf( "\n");
}' /tmp/log.txt
START_NANO_SECONDS=''`/bin/date "+%N"`''
#REQUIRED_COMMANDS.....
END_NANO_SECONDS=''`/bin/date "+%N"`''
echo "TIME TAKEN BY CURRENT SCRIPT: "''`/usr/bin/expr $END_NANO_SECONDS - $START_NANO_SECONDS`''
/bin/sed -n 1p /tmp/log.txt | /bin/sed "s/ *[0-9]*\.[0-9]*\.[0-9]*\.[0-9]* *\([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]\).*/#Date: \1/;"
/bin/sed "s/ *[0-9]*\.[0-9]*\.[0-9]*\.[0-9]* *//;" /tmp/log.txt
3. Using /bin/sed (1 time)/bin/awk '{
if ( 1 == NR)
{
printf( "#Date: %s %s\n", $2, $3);
}
}' /tmp/log.txt
/bin/sed "s/ *[0-9]*\.[0-9]*\.[0-9]*\.[0-9]* *//;" /tmp/log.txt
unalias ls >/dev/null 2>&1
unset -f ls
ls ()
{
echo Always use full path for ls command
echo Julio Jose, Just FYI.
Ret=12
return $Ret
}
ls -latr /root
echo $? will be twelve
#Un defining that ls function
unset -f ls
#!/bin/ksh
#+-----------+----------------+------------------------------------------------------------------+
#|dd/mmm/yyyy|Name |Svn History |
#+-----------+----------------+------------------------------------------------------------------+
#|31/Mar/2017|Murugesan Dinesh|Remove string from server log file. |
#+-----------+----------------+------------------------------------------------------------------+
#Function Disp_Log_Msg
# If no parameter passed
# Script reading default Log file /tmp/Server.log
# else
# Script taking all parameter "$@" as Log file.
# Disp_Log_Msg function display the log message excluding the first column
# Removing first column due to presence of IP Address.
unset -f Disp_Log_Msg
Disp_Log_Msg ()
{
if test 0 -eq $#
then
INPUT_LOG_FILE="/tmp/Server.log"
else
INPUT_LOG_FILE="$@"
fi
if test -f "$INPUT_LOG_FILE"
then
OUTPUT_DIR=''`/bin/dirname $INPUT_LOG_FILE`''
while [ 1 ]
do
OUTPUT_LOG_FILE="$OUTPUT_DIR/Server_"''`/bin/date "+%d_%b_%Y_%S_%N"`''".log"
if test -f "$OUTPUT_LOG_FILE"
then
/bin/sleep 1;
else
break
fi
done
# INPUT_LOG_FILE_NUM_OF_LN
export INPUT_LOG_FILE_NUM_OF_LN=''`/bin/awk 'END { print NR}' "$INPUT_LOG_FILE"`''
/bin/awk '{
INPUT_LOG_FILE_NUM_OF_LN=ENVIRON["INPUT_LOG_FILE_NUM_OF_LN"];
}
{
if ( 1 == NR)
{
printf( "#Date: %s %s\n", $2, $3);
}
for ( Loc=2; Loc<NF; Loc++)
{
printf( "%s ", $Loc);
}
if ( NR != INPUT_LOG_FILE_NUM_OF_LN)
{
printf( "%s\n", $Loc);
}
else
{
printf( "%s", $Loc);
}
}' "$INPUT_LOG_FILE" > "$OUTPUT_LOG_FILE"
if test -f "$OUTPUT_LOG_FILE"
then
echo "Input log file: $INPUT_LOG_FILE"
echo "Output log file: $OUTPUT_LOG_FILE"
fi
else
echo "$INPUT_LOG_FILE No such file"
Ret=1
return $Ret
fi
}
Disp_Log_Msg $@
.......
then
set OUTPUT_DIR="$INPUT_LOG_FILE.$$"
.....
#!/bin/bash
INPUT_LOG_FILE="/tmp/Server.log"
export OUTPUT_DIR="$INPUT_LOG_FILE.$$"
echo OUTPUT_DIR $OUTPUT_DIR
echo INPUT_LOG_FILE $INPUT_LOG_FILE
if test ! -d "$OUTPUT_DIR"
then
echo "Need to create the output directory /bin/mkdir $OUTPUT_DIR"
echo "I feel this is not the right way."
fi
sample output here:MurugesanDinesh@TEL /home/murugesandins [ 0 ]
$ ./Julio_jose.sh
OUTPUT_DIR /tmp/Server.log.7728
INPUT_LOG_FILE /tmp/Server.log
Need to create the output directory /bin/mkdir /tmp/Server.log.7728
I feel this is not the right way.
MurugesanDinesh@TEL /home/murugesandins [ 0 ]
$
#!/bin/ksh
#+-----------+----------------+------------------------------------------------------------------+
#|dd/mmm/yyyy|Name |Svn History |
#+-----------+----------------+------------------------------------------------------------------+
#|31/Mar/2017|Murugesan Dinesh|Remove string from server log file. |
#+-----------+----------------+------------------------------------------------------------------+
#Function Disp_Log_Msg
# If no parameter passed
# Script reading default Log file /tmp/Server.log
# else
# Script taking all parameter "$@" as Log file.
# Disp_Log_Msg function display the log message excluding the first column
# Removing first column due to presence of IP Address.
unset -f Disp_Log_Msg
Disp_Log_Msg ()
{
if test 0 -eq $#
then
INPUT_LOG_FILE="/xx/xx/xx/xx/xx.log"
else
INPUT_LOG_FILE="$@"
fi
if test -f "$INPUT_LOG_FILE"
then
set OUTPUT_DIR=''`$INPUT_LOG_FILE.$$`''
while [ 1 ]
do
OUTPUT_LOG_FILE="$OUTPUT_DIR/Server_"''`/bin/date "+%d_%b_%Y_%S_%N"`''".log"
if test -f "$OUTPUT_LOG_FILE"
then
/bin/sleep 1;
else
break
fi
done
# INPUT_LOG_FILE_NUM_OF_LN
export INPUT_LOG_FILE_NUM_OF_LN=''`/bin/awk 'END { print NR}' "$INPUT_LOG_FILE"`''
/bin/awk '{
INPUT_LOG_FILE_NUM_OF_LN=ENVIRON["INPUT_LOG_FILE_NUM_OF_LN"];
}
{
if ( 1 == NR)
{
printf( "#Date: %s %s\n", $2, $3);
}
for ( Loc=2; Loc<NF; Loc++)
{
printf( "%s ", $Loc);
}
if ( NR != INPUT_LOG_FILE_NUM_OF_LN)
{
printf( "%s\n", $Loc);
}
else
{
printf( "%s", $Loc);
}
}' "$INPUT_LOG_FILE" > "$OUTPUT_LOG_FILE"
if test -f "$OUTPUT_LOG_FILE"
then
echo "Input log file: $INPUT_LOG_FILE"
echo "Output log file: $OUTPUT_LOG_FILE"
fi
else
echo "$INPUT_LOG_FILE No such file"
Ret=1
return $Ret
fi
}
Disp_Log_Msg $@
$ /bin/cp -ip /bin/bash /bin/ksh
$ ./log2.sh
/xx/xx/xx/xx/xx.log No such file
$ echo $?
1
$ /bin/date
Tue May 16 10:02:29 IST 2017
$ /usr/bin/file ./log2.sh
./log2.sh: Korn shell script, ASCII text executable
/usr/bin/od -bc ./log2.sh | /bin/egrep "\\\r"
echo $?
#!/bin/ksh
#+-----------+----------------+---------------------------------------------------------------------+
#|dd/mmm/yyyy|Name | Svn History |
#+-----------+----------------+---------------------------------------------------------------------+
#|24/May/2017|Murugesan Dinesh| Changed INPUT_LOG_FILE to have /tmp/Server.log |
#| Changed OUTPUT_DIR to point to source directory (/tmp) of input file|
#| Also displaying output in current terminal. |
#| Removed following variable in current update. |
#| INPUT_LOG_FILE_NUM_OF_LN |
#|31/Mar/2017|Murugesan Dinesh| Remove string from server log file. |
#+-----------+----------------+---------------------------------------------------------------------+
#Function Disp_Log_Msg
# If no parameter passed
# Script reading default Log file /tmp/Server.log
# else
# Script taking all parameter "$@" as Log file.
# Disp_Log_Msg function display the log message excluding the first column
# Removing first column due to presence of IP Address.
unset -f Disp_Log_Msg
Disp_Log_Msg ()
{
if test 0 -eq $#
then
INPUT_LOG_FILE="/tmp/Server.log"
else
INPUT_LOG_FILE="$@"
fi
if test -f "$INPUT_LOG_FILE"
then
OUTPUT_DIR=''`/bin/dirname $INPUT_LOG_FILE`''
if test "." = "$OUTPUT_DIR"
then
OUTPUT_DIR="$PWD"
fi
while [ 1 ]
do
OUTPUT_LOG_FILE="$OUTPUT_DIR/Server_"''`/bin/date "+%d_%b_%Y_%S_%N"`''".log"
if test -f "$OUTPUT_LOG_FILE"
then
/bin/sleep 1;
else
break
fi
done
echo "Current output:"
echo "------------------------:"
/bin/awk '{
if ( 1 == NR)
{
printf( "#Date: %s %s\n", $2, $3);
}
for ( Loc=2; Loc<NF; Loc++)
{
printf( "%s ", $Loc);
}
printf( "%s\n", $Loc);
}' "$INPUT_LOG_FILE" 2>&1 | /usr/bin/tee -a "$OUTPUT_LOG_FILE"
echo "------------------------:"
if test -f "$OUTPUT_LOG_FILE"
then
echo "Input log file: $INPUT_LOG_FILE"
echo "Output log file: $OUTPUT_LOG_FILE"
fi
else
echo "$INPUT_LOG_FILE No such file"
Ret=1
return $Ret
fi
}
Disp_Log_Msg $@
Related sample output here:$ ./29012555.sh
Current output:
------------------------:
#Date: 23:44:49 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
23:44:49 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
------------------------:
Input log file: /tmp/Server.log
Output log file: /tmp/Server_24_May_2017_49_677778500.log
$ /bin/cat /tmp/Server_24_May_2017_49_677778500.log
#Date: 23:44:49 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
23:44:49 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
$ ./29012555.sh /root/Server.log
Current output:
------------------------:
#Date: 23:44:49 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
23:44:49 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
------------------------:
Input log file: /root/Server.log
Output log file: /root/Server_24_May_2017_19_936798800.log
$ /bin/cat /root/Server_24_May_2017_19_936798800.log
#Date: 23:44:49 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
23:44:49 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
$
Test the updated code and let us know if getting expected output for the same.
Open in new window
Assumes spaces separating the IP and the date/time will always be 2 fields.Obviously, you need to modify the variable to set the name of the file.