linux - Splitting out timestamp/key/value pairs from bash -


hi have file full of data; time stamps beginning of line. need break down file , print each line individually. how can accomplish using bash , (if needed) standard unix tools (sed, awk, etc)?

the time stamp field goes 08:30:00:324810: onward .. example 17:30:00:324810: . number of field following time stamp varies; there 1 x number of fields . need find time stamp format , insert page break.

08:30:00:324810: usg_07y  bidyield=1.99788141 bid=99.20312500 08:30:00:325271: usg_07y askyield=1.98578274 ask=99.28125000 08:30:00:325535: usg_10y  ask=0.00000000 08:30:01:324881:  usg_07y  bidyield=2.02938740 askyield=1.97127853 bid=99.00000000 ask=99.37500000 08:30:01:377021: usg_05y  bid=0.00000000 ask=0.00000000  

thanking u in advance matt

it trivial. read file array, find timestamp, output newline before it:

#!/bin/bash  set -f                          # inhibit globbing (filename expansion) declare -i cnt=0                # simple counter  a=( $(<"$1") )                  # read file array in "${a[@]}";          # each word in file     if [ "$cnt" -gt 0 ];   # test counter > 0         # if last char ':', output newline before word         [ ${i:(-1):1} = ':' ] && printf "\n%s" "${i}" || printf " %s" "$i"     else         printf "%s" "$i"        # if first word, print.     fi     ((cnt++)) done printf "\n" 

use/output:

$ bash parsedtstamp.sh filename.txt 08:30:00:324810: usg_07y bidyield=1.99788141 bid=99.20312500 08:30:00:325271: usg_07y askyield=1.98578274 ask=99.28125000 08:30:00:325535: usg_10y ask=0.00000000 08:30:01:324881: usg_07y bidyield=2.02938740 askyield=1.97127853 bid=99.00000000 ask=99.37500000 08:30:01:377021: usg_05y bid=0.00000000 ask=0.00000000 

i added counter var output newline if not first word.


alternate version avoids temporary array storage (for large files)

while there no limit on array size in bash, if find parsing million line files, better avoid storing lines in memory. can accomplished processing lines read file. way of doing same thing without using array intermediate storage:

#!/bin/bash  set -f                              # inhibit globbing (filename expansion) declare -i cnt=0                    # simple counter  # read each line in file while read -r line_entries || [ -n "$line_entries" ];     in $line_entries;      # each word in line (no quotes word splitting)         if [ "$cnt" -gt 0 ];   # test counter > 0             # if last char ':', output newline before word             if [ ${i:(-1):1} = ':' ];                 printf "\n%s" "${i}"             else                 printf " %s" "$i"             fi         else             printf "%s" "$i"        # if first word, print.         fi         ((cnt++))                   # increment counter     done done <"$1"  printf "\n" 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -