Strange problem with cut, colrm, awk and sed: cannot cut characters from pipe stream

I created a script to list all the files in the directory and below it. I wanted to add some feedback for progression using pv, because I usually use it from the root directory.

The problem is a search that always includes fractional seconds in its time output (% TT), but I don't want to write down so many details.

If I write a script to do everything in one pass, I get the correct output. But if I use intermediate files for evaluation during the "second" pass, the result is changed and I do not understand why.

This version gives the correct result:

#!/bin/bash

find -printf "%11s %TY-%Tm-%Td %TT %p\n" 2> /dev/null |
# - Remove the fractional seconds from the time
# before:       4096 2011-01-19 22:43:51.0000000000 .
# after :       4096 2011-01-19 22:43:51 .
colrm 32 42 |
pv -ltrbN "Enumerating files..." |
# - Sort every thing by filename
sort -k 4

But sorting can take a lot of time, so I tried something like this to have a bit more feedback:

#!/bin/bash

TMPFILE1=$(mktemp)
TMPFILE2=$(mktemp)

# Erase temporary files before quitting
trap "rm $TMPFILE1 $TMPFILE2" EXIT

find -printf "%11s %TY-%Tm-%Td %TT %p\n" 2> /dev/null |
pv -ltrbN "Enumerating files..." > $TMPFILE1
LINE_COUNT="$(wc -l $TMPFILE1)"

#cat $TMPFILE1 | colrm 32 42 |                   #1
#cat $TMPFILE1 | cut -c1-31,43- |                #2
#cut -c1-31,43- $TMPFILE1 |                      #3
#sed s/.0000000000// $TMPFILE1 |                 #4
awk -F".0000000000" '{print $1 $2}' $TMPFILE1 |  #5
pv -lN "Removing fractional seconds..." -s $LINE_COUNT > $TMPFILE2

echo "Sorting list by filenames..." >&2
cat $TMPFILE2 |
sort -k 4

5 "" . ".0000000000" .

- , ?

. .

+3
2

-printf ( , GNU find 4.4.2):

find -printf "%11s %TY-%Tm-%Td %.8TT %p\n"

"HH: MM: SS".

, , :

, № 1-5 , , wc ( ). pv, wc . , stdin. , , , ( , ).

:

LINE_COUNT=$(wc -l < "$TMPFILE1")

:

< $TMPFILE1 colrm 32 42 |                   #1 No need for cat

colrm 32 42 < $TMPFILE1 |                   #1

< $TMPFILE1 cut -c1-31,43- |                #2

cut -c1-31,43- < $TMPFILE1 |                #2

sed s/\.0000000000// $TMPFILE1 |            #4 The dot should be escaped
+3

, , " " ... , , . , , , , , - , script.

- , " wc -l $TMPFILE..."

, , .

, .

0

Source: https://habr.com/ru/post/1787602/


All Articles