Find the answer to your Linux question:
Results 1 to 6 of 6
Hi, I don't know too much of bash scripting and I'm trying to develop a bash script to do this operations: I have a lot of .txt files in the ...
  1. #1
    Just Joined!
    Join Date
    Jan 2011
    Posts
    1

    Using sed to find, convert and replace lines

    Hi,

    I don't know too much of bash scripting and I'm trying to develop a bash script to do this operations:

    I have a lot of .txt files in the same directory.
    Every .txt file follows this structure:
    file1.txt:
    Code:
    <name>first operation</name>
    <operation>21</operation>
    <StartTime>1292435633</StartTime>
    <EndTime>1292435640</EndTime>
    
    <name>second operation</name>
    <operation>21</operation>
    <StartTime>1292435646</StartTime>
    <EndTime>1292435650</EndTime>
    I want to search every <StartTime> line and convert it to standard date/time format (not unix timestamp) but preserving the structure <StartTime>2010-12-15 22:52</StartTime>, for example. This could be a function of search/replace, using sed? I think I could use these function that I found: date --utc --date "1970-01-01 $1 sec" "+%Y-%m-%d %T"

    I want to to do the same with <EndTime> tag.

    I should do this for all *.txt files in a directory.

    I tried using sed but with not wanted results. As I said I don't know so much of bash scripting so any help would be appreciated.

    Thank you for your help!

    Regards
    Last edited by MikeTbob; 01-09-2011 at 04:02 PM. Reason: Added Code Tags

  2. #2
    Just Joined!
    Join Date
    Feb 2007
    Posts
    14
    I agree that sed is probably the tool to use for this and you probably won't need to use a date function. Could you let me know the current format that the date / time is currently in? From your mail I'm guessing yyyymmddhhmm, but your sample doesn't make this clear

  3. #3
    Just Joined!
    Join Date
    Feb 2007
    Posts
    14
    Sorry, you seem to have unix timestamps there. I think in that case, some awk would be a better idea. Here's a line to cut and paste to do what I think you want:

    Code:
    awk -F"-" 'BEGIN { FS = "[<>]" } $3 ~ /[[:digit:]][[:digit:]][[:digit:]]/ { sub($3, strftime("%Y-%m-%d",$3)); print } $3 !~ /[[:digit:]][[:digit:]][[:digit:]]/ {print }' input_file > output_file
    where input_file is the file you have and output_file is the file that awk will create with a human readable date (and the rest of the data) in
    Last edited by MikeTbob; 01-09-2011 at 04:02 PM. Reason: Added Code Tags

  4. #4
    Just Joined!
    Join Date
    Feb 2007
    Posts
    14
    With the time added by changing the strftime to: "%Y-%m-%d %H:%M"

  5. #5
    Just Joined!
    Join Date
    Dec 2010
    Posts
    16
    date can convert the seconds since epoch to something more meaningful:

    Code:
    $ date -d @1292435646 +'%Y-%m-%d %H:%M:%S'
    2010-12-15 18:54:06
    
    Last edited by MikeTbob; 01-09-2011 at 04:03 PM. Reason: Added Code Tags

  6. #6
    Just Joined!
    Join Date
    Dec 2010
    Posts
    16
    Code:
    for l in $(cat file1.txt); do if [ $(echo "$l" | egrep -i '<StartTime>'\|'<EndTime>') ]; then sec=$(echo $l | sed 's/.*>\([0-9]*\)<\/.*/\1/'); startDate=$(date -d @${sec} +'%Y-%m-%d %T'); echo "$(echo $l | awk -F$sec '{ print $1 }')${startDate}$(echo $l | awk -F$sec '{ print $2 }')"; else echo "$l"; fi; done
    Last edited by MikeTbob; 01-09-2011 at 04:03 PM. Reason: Added Code Tags

Posting Permissions

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