I want to extract something close to the list in a shell script.

Asked 2 years ago, Updated 2 years ago, 116 views

There is a list like this.This list is entered from standard input.

-06
26
56
-07
26
54
-08
27
55
-10
01
-11
01
-12
01
-13
01
52
-14
52

Then use the date command to get the time and store it in the variable.

Then search for the nearest time on the list first.
Next, I would like to get three times from there and return them.

For example, 8:30 would return 550855 10011101 を in the array.
"Also, if it is 4pm, there is nothing more recent, so I would like to return the ""990626 990656 990726"" with 99 added to the beginning of the number after searching from 6pm the next day."This is to prevent other data from being processed and sorted in order of proximity, and the next day's data from being earlier than the same day's data if the data from the same day's data.

Sorry for the inconvenience, but I appreciate your cooperation

linux bash

2022-09-30 17:39

3 Answers

I wrote sed, awk, and /bin/sh for now.
However, once the text processing is somewhat complicated, I think it would be better to write in a more sophisticated scripting language (perl, python, ruby, etc.) instead of a shell script.

#!/bin/sh

# 1. create today table
TMP_FILE=`mktemp`
awk-F'-'BEGIN {OFS=""} { if($2!="") hour=$2; else print hour,$1}'->${TMP_FILE}

# 2. create today and tomorrow table
TMP_FILE2 = `mktemp`
cat${TMP_FILE}>>${TMP_FILE2}
sed-es/^/99/g${TMP_FILE}>${TMP_FILE2}

# 3.get current time
NOW = `date+%H%M`
# NOW = 0830 # for test
# NOW = 1600 # for test

# 4.search3 items after NOW
awk "{if(\$0>=\"$NOW\")print\$0}"<${TMP_FILE2}|head-3

# 5.clean up tmp files
if [-f${TMP_FILE2}]; then
    rm${TMP_FILE2}
fi

if [-f${TMP_FILE}]; then
    rm${TMP_FILE}
fi


2022-09-30 17:39

If AWK is OK with you, would it be like this?

catlist.txt|awk'
{
    # read input file and generate list of time
    if(gsub("^-","){
        hh = $0
    } else{
        list[++i] = hh$0;
    }
}
END{
    # get current time
    "date+%H%M" | getline
    date = $0

    # add tomorrow's list
    for(j=1;j<=i;j++){
        list[j+i] = "99" list[j]
    }

    # search in the list
    for(j=1;j<=2*i;j++){
        if(date<=list[j]){
            # found and print 3 items
            for(k=j;k<j+3;k++){
                print list [k]
            }
            exit
        }
    }
}'


2022-09-30 17:39

You can write sed in one line.

It's a little troublesome to compare the strings of time, but it's not difficult if you can change your mind that you just need to delete all of the characters below to find the above.
For example, to find 830 or higher, remove [^8-9].. and 8[^3-9. and 83[^0-9] and the remaining number will be the answer.

HHMM=`date+%H%M`;H0=${HHMM:0:1};H1=${HHMM:1:1};M0=${HHMM:2:1};M1=${HMM:3:1}

sed'-e:a;N;${;:b;s/-\([0-2][0-9]\)\(\n[^-]*\n\)\([0-5][0-9]\n\)/-\1\2\1\3/g;tb;s/-\([0-2][0-9]\)\n\([0-5][0-9]\)/\1\2/g;h;s/\(^\|\n\)/\199/g;x;s/$/\n/;s/[^'$H0'-9]...\n//g;s/'$H0'[^'$H1'-9]..\n/g;s/'$H0$H1'[^'$M0'-9].\n//g;s/'$H0$H0$M0'$M0'[$M0'$M0'$M0'$M0;\n;\n;\n;\n


2022-09-30 17:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.