Find the answer to your Linux question:
Page 1 of 5 1 2 3 4 5 LastLast
Results 1 to 10 of 43
Hello Again everyone! I hope everyone is enjoying the day. I am sure this is possible to do but I want to ask and try to understand how. Can I ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2007
    Location
    Houston
    Posts
    75

    Awk to convert a date into a word form


    Hello Again everyone!

    I hope everyone is enjoying the day.

    I am sure this is possible to do but I want to ask and try to understand how. Can I use awk to write a file that has 4 columns so that the 4 column goes from a date to what the name of the day is?

    For example I have a file that looks something like this:

    Code:
    Info A\long path\user Info3 Sept. 25, 2007 13:59:02
    Info4 B\Long path with info spaced\user  Info6 Oct. 13, 2007 12:01:25
    etc

    I know how to take today's date and make it in a day but I do not know how to make it so that the date can be completely converted. Also remove the time. As you can see that second coloumn will have spaces.

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    What do you mean by "so that the date can be completely converted"?

    Can you give us an example of an input line and how you'd like the output line to appear?

    And also give us some details of the kind of system you're running on? (Those backslashes are interesting.)

  3. #3
    Just Joined!
    Join Date
    Oct 2007
    Location
    Houston
    Posts
    75
    Quote Originally Posted by wje_lf View Post
    What do you mean by "so that the date can be completely converted"?

    Can you give us an example of an input line and how you'd like the output line to appear?

    And also give us some details of the kind of system you're running on? (Those backslashes are interesting.)
    I have taken some medicine that makes me sleepy so if my thoughts are not clear I am sorry.

    Currently the file is a lot longer then this. I run an awk command to remove several columns that I don't need using:

    Code:
     gawk -F"\t" '{ print $1,"\t" $2,"\t" $4,"\t" $6 | "sort" }' schedule >sclean2
    This gives me the results you see below. Except the \ is actually a / so I will correct that in the what I see part. Now this document can have over 3,000 lines.

    What I see:
    Code:
     
    INFO         INFO   A/Path to a directory/admin     10/23/07 11:52:28 PM
    INFO2         INFO3   A/Path to a directory/user     10/20/07 11:52:28 PM
    What I would like to see:
    Code:
    INFO         INFO   A/Path to a directory/admin     Tuesday
    INFO2         INFO3   A/Path to a directory/user     Saturday
    I don't want the date in MM/DD/YY format or the time. I need to know the day. I will be using this tool to make a schedule of activities. Then using it to write the activities and make it so they can be compared.

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192

    wanted: an awk expert

    Ok, here's what I gather from what you've said so far. Please post a correction if I'm wrong.

    Your input file contains 6 fields per line, and the fields are delimited by tab characters, so there are 5 tab characters per line.

    The final field in each line is a date/time. The date/time can be in one of two formats:
    1. 10/23/07 12:59:37 PM, meaning 23 September 2007; or
    2. Sept. 23, 2007 12:59:37 PM

    You want the output to contain fields 1, 2, 4, and 6. But for field 6, you want just Sunday, Monday, Tuesday, and so on.

    Correct?

    Ok, I'm no awk expert, so I googled this stuff. I found some answers that looked promising, but I couldn't make them work, and I don't have the time to become an awk expert at this point.

    So.

    Are there any awk experts out there who want to jump in and help?

    If I don't hear anything for a while, I'll come back, and I'll give you a solution, and believe me, the solution will be as ugly as sin. But it will work according to the requirements I've set out above.

    "For awhile" might mean a few hours, after dinner and watching Downfall, an award-winning film about the final days of the Bu^H^HThird Reich. But if I'm too sleepy after that, it'll be the morning.

  5. #5
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    i believe you have GNU date ..
    Code:
    awk 'BEGIN{OFS=FS="\t"}
    {
     cmd="date +%A --date=\047"$6"\047"
     cmd | getline day
     print $1,$2,$4,day
    }' schedule
    however if you want to do everything in gawk, a snippet example:
    Code:
    awk 'BEGIN{
       d = "10/23/07 11:52:28 PM"
       n=split(d,t) #can use substr too!
       m=split(t[1],a,"/")
       mth=a[1]
       day=a[2]
       year=a[3]
       j=split(t[2],b,":")
       hr=b[1]
       min=b[2]
       sec=b[3]
       str="20"year" "mth" "day" "hr" "min" "sec" "ampm   
       thedate= mktime(str)
       print strftime("%A",thedate)   
    }'

  6. #6
    Just Joined!
    Join Date
    Oct 2007
    Location
    Houston
    Posts
    75
    Hi Ghostdog

    Your code returned the day for that line. But it did not leave the rest of the text present. Which I do need the reminder of the text in the other fields. It appears it went to the last line. There is about 3,000 lines that I need the date applied to (each line at the end has the date).

    wje_lf:

    I understand and we all have our strength and weaknesses. It does not have to be awk to do this if it is something easier to do it in. I am open for any help that can be provided. I would be thankful for insight that anyone has. I am learning and there is no one right way to do something. I figured awk would make it easier to do.

  7. #7
    Just Joined!
    Join Date
    Oct 2007
    Location
    Houston
    Posts
    75
    Quote Originally Posted by ghostdog74 View Post
    i believe you have GNU date ..
    Code:
    awk 'BEGIN{OFS=FS="\t"
     cmd="date +%A --date=\047"$6"\047"
     cmd | getline day
     print $1,$2,$4,day
    }' schedule
    however if you want to do everything in gawk, a snippet example:
    Code:
    awk 'BEGIN{
       d = "10/23/07 11:52:28 PM"
       n=split(d,t) #can use substr too!
       m=split(t[1],a,"/")
       mth=a[1]
       day=a[2]
       year=a[3]
       j=split(t[2],b,":")
       hr=b[1]
       min=b[2]
       sec=b[3]
       str="20"year" "mth" "day" "hr" "min" "sec" "ampm   
       thedate= mktime(str)
       print strftime("%A",thedate)   
    }'
    That bottom script is helpful. However, I have to pull the date from the file with the text and replace the date. This script appeared to do it based on my input.

  8. #8
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    i have amended the top script.
    As for the bottom script, it is just an example of how you can get the full day name in GNU awk. depending on how you get the date/time stamp, substitute "d" variable with whatever.
    I expect you to get your hands dirty playing with awk...

  9. #9
    Just Joined!
    Join Date
    Oct 2007
    Location
    Houston
    Posts
    75
    Hmm intersting. I am going to get my reference guide out. It is intersting how you are doing this.

    does cmd tell awk to execute a shell command?

    It looks like this is only looking at one line and not them all. Is it possible to do them all? If so just tell me yes or no. I would like try and figure it out first.

  10. #10
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by Korelis View Post
    Hmm intersting. I am going to get my reference guide out.
    which reference guide? is it this one ?
    It is intersting how you are doing this.
    does cmd tell awk to execute a shell command?
    no, its just a variable ! i just construct my command line and assign it to a variable, called "cmd". The actual execution of the external shell program (date ...) is through the system() function in awk.
    It looks like this is only looking at one line and not them all. Is it possible to do them all? If so just tell me yes or no. I would like try and figure it out first.
    I wouldn't know wat you are talking about , unless you provide more info. which one you are talking about, how your schedule file looks like, etc etc ...

Page 1 of 5 1 2 3 4 5 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •