Filter date and time form a file using sed command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Filter date and time form a file using sed command
# 1  
Old 04-20-2011
Filter date and time form a file using sed command

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
# 2  
Old 04-20-2011
Are you expecting this..?
Code:
sed '/on Tue/s/[^ ]* [^ ]* \(.*\) [^ ]*$/\1/' inputfile > outfile

# 3  
Old 04-20-2011
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.
# 4  
Old 04-20-2011
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
Code:
sed '/^.. ... .../s/ .*//' inputfile > outfile


Last edited by michaelrozar17; 04-20-2011 at 06:08 AM.. Reason: added ^
# 5  
Old 04-20-2011
Code:
#!/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

# 6  
Old 04-20-2011
Try this

Code:
awk '{ if($0 ~ /[Tue|Wed|Thur|Fri|Sat|Sun|Mon]/) {print $1}else{print}}' file

regards,
Ahamed
# 7  
Old 04-20-2011
@michaelrozar17:- Its not working. It is removing "on Tue" not the rest of the part.

Guys i need sed command..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Including Hash / in sed command filter

Hello All, I want to print data in between two lines in a file sample.txt through more or cat command on the screen. For that I am using below sed command to give the BEGIN and END text. Content of sample.txt server01:~ # cat /proc/mdstat Hello this is a text message 1 Hello this is a... (5 Replies)
Discussion started by: Xtreme
5 Replies

2. Linux

Filter log file contents between date

Hi, Could you please provide me command to filter contents between date in a log file? Say for example, in a log file I want to capture contents between date May 01 from 5am to 9 am. OS -- Linux Regards, Maddy (1 Reply)
Discussion started by: Maddy123
1 Replies

3. Shell Programming and Scripting

History of all the users in single file with command , date . time , ip and user

HTML Code: archive_history() { HISTORYOLD=${HISTFILE}.archive CURTIME=`date` CURTTY=`tty` IP=$(echo $SSH_CLIENT | awk '{print $1}') if ; then echo "#-${HOSTNAME}-- ${CURBASHDATE} - ${CURTIME} ($CURTTY) ${USER} ${IP}----" >> $HISTORYOLD history... (0 Replies)
Discussion started by: rehantayyab82
0 Replies

4. Shell Programming and Scripting

History of all the users in single file with command , date . time , ip and user

HTML Code archive_history() { HISTORYOLD=${HISTFILE}.archive CURTIME=`date` CURTTY=`tty` IP=$(echo $SSH_CLIENT | awk '{print $1}') if ; then echo "#-${HOSTNAME}-- ${CURBASHDATE} - ${CURTIME} ($CURTTY) ${USER} ${IP}----" >> $HISTORYOLD history... (2 Replies)
Discussion started by: rehantayyab82
2 Replies

5. Shell Programming and Scripting

Problem with filter data using sed command

Hi, I am using the following command(sed) to get the key/value pair from the string String="{ "test":"test message", "testmessage":"subscription is active, charge successfully} " }" status=$( echo $String | sed -e 's/^.*\("testmessage":*\).*$/\1/') echo $status i am getting this... (2 Replies)
Discussion started by: nanthagopal
2 Replies

6. Shell Programming and Scripting

how to filter file for specific date

Hi My OS is solaris 64 bit 10, I have many files in a logs directory where we recive 40-50 logs every day. i have last 20 days file present in this directory. I want to move each day file to a particulaar directory whose name is appended with the date of file. eg Code: 1.txt file... (1 Reply)
Discussion started by: guddu_12
1 Replies

7. Shell Programming and Scripting

find command to filter specific type of files older than certain date.

Hi I need to find the list of files in a directory and to do some specific operations based on the type of files. suppose in a directory am having .dat , .log, .err, .rej file types. i need to filter out .dat and .log only which are older than six months. i used the below query but the... (2 Replies)
Discussion started by: msathees
2 Replies

8. Shell Programming and Scripting

extract last modified time of file in form of YYYYMMDD

Hi All, I want to extract last modified, seen in ls -l command, of a file and then convert it into "YYYYMMDD" format. Any help regarding would be helpful Thanks in advance... (3 Replies)
Discussion started by: itsme_maverick
3 Replies

9. Shell Programming and Scripting

Processing a log file based on date/time input and the date/time on the log file

Hi, I'm trying to accomplish the following and would like some suggestions or possible bash script examples that may work I have a directory that has a list of log files that's periodically dumped from a script that is crontab that are rotated 4 generations. There will be a time stamp that is... (4 Replies)
Discussion started by: primp
4 Replies

10. Shell Programming and Scripting

Help needed - Replacing all date & time occurrences in a file with a string using Sed

Hi, I am new to using Sed. I have a file containg lines like the following: INFORM----Test.pc:168:10/11/05 12:34:26 > some text goes here.. TRACE-----Test.pc:197:10/11/05 12:34:26 > some text goes here.. My requirement is to replace 10/11/05 12:34:26 with a string <RUNDATE> (including <... (4 Replies)
Discussion started by: Hema_M
4 Replies
Login or Register to Ask a Question