On Mon, Oct 23, 2023 at 01:21:43PM +0530, Pankaj Jangid wrote:
> Another way to do it is,
> 
> cat "${IFL}"| cut -d "/" -f7-|sed 's/.description//'
> 
> because you already know the prefix, you can count the fields. So "-f7-"
> i.e. 7 onwards. Then use sed to remove the extension.

Whoops!  I completely missed the requirement of removing ".description"
in my original answer.

Here's a corrected version:


#!/bin/bash

inputfile=${1-/whatever}
prefix='[info] 123XYZ: 
/media/user/1234qwer/Algebraic_Geometry04/20231022040239/'

while IFS= read -r line; do
    line=${line#"$prefix"}
    printf '%s\n' "${line%.description}"
done < <(grep -F -- "$prefix" "$inputfile")


Pankaj's answer is not wrong, but it dropped the "grep" which filters
the lines containing the desired prefix.  I'd also add an $ anchor to
the sed regex, and escape the . with backslash so that it becomes
literal.  This answer also requires counting the fields by hand each
time the prefix changes, which may or may not be a concern.

Reply via email to