On Wed, Feb 10, 2016 at 7:02 PM, Mick <michaelkintz...@gmail.com> wrote: > I've been struggling to parse/split/substitute some names and numbers using a > spreadsheet and think that this task may be easier to achieve using > conventional *nix tools. The problem is I wouldn't know where to start. > > I have a directory with loads of images. Each image file name has a > description comprising hyphen-separated words and a part number, also hyphen > separated; e.g.: > > some-description-with-words-012-63099.jpg > > The number and length of the words change for each file. The part number > always has two components separated by a hyphen, but may also change in length > and acquire more/fewer digits. > > I need two outputs: > > 1. the description + " (per M²)", like so: > > some-description-with-words (per M²) > > 2. the part number, but replacing the hyphen with "/", like so: > > 012/63099 > > > I can list the directory contents and redirect all image file names into a txt > file. What I am looking for is some additional steps I can pipe it through to > obtain the two outputs, either in the same file or different files. These > file(s) are then imported into a spreadsheet template and manipulated, before > the result is ultimately exported from the spreadsheet and uploaded to a > server as a CSV file. > > Is this parsing, splitting and substitution exercise achievable? Any > suggestions to try out? > > -- > Regards, > Mick
To get the desired output in two distinct steps, you could try this: echo 'some-description-with-words-012-63099.jpg' | sed 's!-[0-9][0-9]*.*! (per M²)!' some-description-with-words (per M²) echo 'some-description-with-words-012-63099.jpg' | sed 's![^0-9][^0-9]*!!; s![.][^0-9]*!!; s!-!/!' 012/63099 To get both types of output on the same line separated with white space, you could try this: echo 'some-description-with-words-012-63099.jpg' | sed ' H s!-[0-9][0-9]*.*! (per M²)! x s![^0-9][^0-9]*!! s![.][^0-9]*!! s!-!/! H g y!\n! ! ' some-description-with-words (per M²) 012/63099