I want to filter out the date and time from this line in a file. How to do this using sed command.
on Tue Apr 19 00:48:29 2011
I want to filter out the date and time from this line in a file. How to do this using sed command.
on Tue Apr 19 00:48:29 2011
Are you expecting this..?
sed '/on Tue/s/[^ ]* [^ ]* \(.*\) [^ ]*$/\1/' inputfile > outfile
The problem is the date,time,day is not fixed. They will change.I need to remove date and time also.
Can it be done like if it finds this kind of time format in a file ,it replaces the whole of it.
input file:
abcd
sadfgfg
on Tue Apr 19 00:48:29 2011
sghjkll
on Wed Mar 20 01:48:29 2011
aaabb223344
output file:
abcd
sadfgfg
on
sghjkll
on
aaabb223344
I want this as my output.
Please use code tags icon while posting the input file/output file/commands etc for better readability.
If the date is of pattern in post#1 then try
sed '/^.. ... .../s/ .*//' inputfile > outfile
#!/bin/bash
# tested on bash 4
while read -r line
do
case "$line" in
*Mon\ *|*Tue\ *|*Wed\ *|*Thu\ *|*Fri\ *|*Sat\ *|*Sun\ *)
[[ $line =~ (.*)((Mon|Tue|Wed|Thu|Fri|Sat|Sun).*2011)(.*) ]]
line="${BASH_REMATCH[1]} ${BASH_REMATCH[4]}"
;;
esac
echo "$line"
done < file
Try this
awk '{ if($0 ~ /[Tue|Wed|Thur|Fri|Sat|Sun|Mon]/) {print $1}else{print}}' file
regards,
Ahamed
@michaelrozar17:- Its not working. It is removing "on Tue" not the rest of the part.
Guys i need sed command..
whatever @michaelrozar17 has posted is working for me.
Which is your OS? Solaris? If so, use /usr/xpg4/bin/awk
regards,
Ahamed
Hey @michaelrozar17 thanks, Its working, but there is another issue. If the file is like this it doesnt work
input file:
abcd
sadfgfg
on Tue Apr 19 00:48:29 2011
sghjkll
on Wed Mar 20 01:48:29 2011
aaabb223344
anhs asadh on Wed Mar 20 01:48:29 2011
Your fault, please provide better examples of your input file when asking questions.
Thank you.
Assuming the timestamp in the file has the word on before it
sed '/.. ... ... [0-9][0-9]/s/\(.* *on\) .*/\1/' inputfile > outfile
Hey @michaelrozar17 can it be dependent on the time ie(00:12:17) as it will solve anoher purpose to me. may be somewhat like this....
sed '/... ... [0-9][0-9] [0-9][0-9]/:[0-9][0-9]/:[0-9][0-9]/s/