It is currently Tue, 17 May 2022 02:49:01 GMT

Author Message
 extract text from log file
I need to extract the time from a log file and format it in a form
like  00:00

The file looks like this:

xxxxxxxxxxx xxxxxxxx xxxxxxxxx xxxxxx   FILE_ABC_200107017_220065.log

What I need to extract is the 2200 and format it to 22:00 .  Yes this
is the time for the log and it will change for every file.  I'm new to
Unix and not sure what or how to use  awk, sed, or grep ???

Much thanks in advance  !!!!


 Sat, 03 Jan 2004 16:31:51 GMT   
 extract text from log file

I use gawk and assume:
- you have always 5 columns separated by whitespace;
- the last field is always separated by three "_"
- the time ist always in HHMM format

Then you could write the following script "extract_time.awk":

{  split($5, split_array, "_")
   raw_time = split_array[4]
   formatted_time = substr(raw_time, 1, 2) ":" substr(raw_time, 3, 2)
   print $0 " " formatted_time

The next question is: where do you want the formatted output. The
script appends the formatted time at the end of each line. Change
the line with "print" according to your needs.

Then call the script with:
gawk -f extract_time.awk logfile > outfile

hans juergen mueller

 Sat, 03 Jan 2004 20:41:27 GMT   
 extract text from log file
Thanks very much!!
Not only will this do the job for me, but I will know what to use that
next time.!!

Thanks again

On Tue, 17 Jul 2001 21:41:27 +0900 (KST), (Hans

 Sat, 03 Jan 2004 22:50:35 GMT   
 extract text from log file

    sed 's/.*\(..\)\(..\)....../\1:\2/' logfile
assuming the hour part is always 2 digits.

 Sat, 03 Jan 2004 22:47:11 GMT   
 extract text from log file
cat file.txt |sed 's/^.*\(..\)\(..\)..\.log^/\1:\2/'

Which should (untested) do what you're after, so long as the line in your
file always ends in hhmmss.log

If you want an explanation I suggest you read the regular expressions
section in "Unix Power Tools"

Hope this helps,


 Sun, 04 Jan 2004 03:20:34 GMT   
   [ 5 post ] 

Similar Threads

1. Extracting lines from a text file that match a certain criteria to another text file

2. Script to extract portions of text from a text file

3. Extracting text from a file

4. Postscript file corrupted - extracting text/patching

5. How to extract columns from a text file?

6. Question: Extracting text from a file.

7. Extract specific text from file using sed

8. Extract Records from a text file

9. Urgent, need script or command to extract lines from a huge text file

10. Extracting lines from inside a text file (question for sed expert)

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software