General TC > Programming & Scripting - Unofficial
Help Requested - remove superfluous directory names from file list
Juanito:
As I'm hopeless at scripting, help would be appreciated with a script to remove superfluous directory names from an extension file list.
As an example, to go from this:
--- Code: ---/usr
/usr/local
/usr/local/include
/usr/local/include/zbuff.h
/usr/local/include/zdict.h
/usr/local/include/zstd.h
/usr/local/include/zstd_errors.h
/usr/local/lib
/usr/local/lib/pkgconfig
/usr/local/lib/pkgconfig/libzstd.pc
--- End code ---
..to this:
--- Code: ---/usr/local/include/zbuff.h
/usr/local/include/zdict.h
/usr/local/include/zstd.h
/usr/local/include/zstd_errors.h
/usr/local/lib/pkgconfig/libzstd.pc
--- End code ---
polikuo:
It looks like these lines were generated via "find" command.
How about:
--- Code: ---find /usr -not -type d
--- End code ---
I'm having dinner at the moment, I'll try to script with this as soon as possible.
Juanito:
They were generated with the “unsquashfs - l -d ' '“ command
mocore:
pipe to awk like
--- Code: ---echo "$data" | awk '/\./{print $0}'
--- End code ---
To select lines containing '.' dot char !
--- Quote ---echo "$data" | awk '/\./{print $0}'
/usr/local/include/zbuff.h
/usr/local/include/zdict.h
/usr/local/include/zstd.h
/usr/local/include/zstd_errors.h
/usr/local/lib/pkgconfig/libzstd.pc
--- End quote ---
How ever this will not work with
-files with no '.ext' extention
-or folders containng a '.' char ...
polikuo:
--- Quote from: Juanito on November 17, 2017, 03:23:11 AM ---They were generated with the “unsquashfs - l -d ' '“ command
--- End quote ---
Save this script as "strip-path.sh"
--- Code: ---#!/bin/sh
OUTPUT_DIR=/tmp/tcz-list
mkdir -p $OUTPUT_DIR
strip_path() {
awk 'BEGIN {FS="\n";RS=""} {
print $NF
for (i=NF;i>1;i--) {
if ($i !~ $(i - 1)) print $(i - 1)
}
}' < /dev/stdin
}
for TCZ in $@; do
unsquashfs -l $TCZ | grep 'squashfs-root/' | cut -d '/' -f 2- | strip_path > ${OUTPUT_DIR}/"$(basename $TCZ)".list
done
--- End code ---
To run the script
--- Code: ---./strip-path.sh /etc/sysconfig/tcedir/optional/zstd*.tcz
--- End code ---
Results
--- Quote ---tc@box:/tmp/tcz-list$ ls
zstd-dev.tcz.list zstd.tcz.list
tc@box:/tmp/tcz-list$ cat zstd-dev.tcz.list
usr/local/lib/pkgconfig/libzstd.pc
usr/local/include/zstd_errors.h
usr/local/include/zstd.h
usr/local/include/zdict.h
usr/local/include/zbuff.h
tc@box:/tmp/tcz-list$ cat zstd.tcz.list
usr/local/bin/zstdmt
usr/local/bin/zstdless
usr/local/bin/zstdgrep
usr/local/bin/zstdcat
usr/local/bin/unzstd
--- End quote ---
Note that they're in reverse order.
If you're not OK with that, you can use "tac" command from "coreutils.tcz" to flip it back or this "sed" one-liner.
--- Code: ---sed '1!G;h;$!d'
--- End code ---
Navigation
[0] Message Index
[#] Next page
Go to full version