Gentoo Archives: gentoo-user

From: Mick <michaelkintzios@×××××.com>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] [OT] awk - sed - grep = tying-myself-up-in-knots
Date: Wed, 10 Feb 2016 22:51:31
Message-Id: 2834718.sMb3gA1obn@dell_xps
In Reply to: Re: [gentoo-user] [OT] awk - sed - grep = tying-myself-up-in-knots by Alexander Kapshuk
1 On Wednesday 10 Feb 2016 20:28:29 Alexander Kapshuk wrote:
2 > On Wed, Feb 10, 2016 at 7:02 PM, Mick <michaelkintzios@×××××.com> wrote:
3 > > I've been struggling to parse/split/substitute some names and numbers
4 > > using a spreadsheet and think that this task may be easier to achieve
5 > > using conventional *nix tools. The problem is I wouldn't know where to
6 > > start.
7 > >
8 > > I have a directory with loads of images. Each image file name has a
9 > > description comprising hyphen-separated words and a part number, also
10 > > hyphen separated; e.g.:
11 > >
12 > > some-description-with-words-012-63099.jpg
13 > >
14 > > The number and length of the words change for each file. The part number
15 > > always has two components separated by a hyphen, but may also change in
16 > > length and acquire more/fewer digits.
17 > >
18 > > I need two outputs:
19 > >
20 > > 1. the description + " (per M²)", like so:
21 > >
22 > > some-description-with-words (per M²)
23 > >
24 > > 2. the part number, but replacing the hyphen with "/", like so:
25 > >
26 > > 012/63099
27 > >
28 > >
29 > > I can list the directory contents and redirect all image file names into a
30 > > txt file. What I am looking for is some additional steps I can pipe it
31 > > through to obtain the two outputs, either in the same file or different
32 > > files. These file(s) are then imported into a spreadsheet template and
33 > > manipulated, before the result is ultimately exported from the
34 > > spreadsheet and uploaded to a server as a CSV file.
35 > >
36 > > Is this parsing, splitting and substitution exercise achievable? Any
37 > > suggestions to try out?
38 > >
39 > > --
40 > > Regards,
41 > > Mick
42 >
43 > To get the desired output in two distinct steps, you could try this:
44 >
45 > echo 'some-description-with-words-012-63099.jpg' |
46 > sed 's!-[0-9][0-9]*.*! (per M²)!'
47 > some-description-with-words (per M²)
48 >
49 > echo 'some-description-with-words-012-63099.jpg' |
50 > sed 's![^0-9][^0-9]*!!; s![.][^0-9]*!!; s!-!/!'
51 > 012/63099
52 >
53 > To get both types of output on the same line separated with white
54 > space, you could try this:
55 >
56 > echo 'some-description-with-words-012-63099.jpg' |
57 > sed '
58 > H
59 > s!-[0-9][0-9]*.*! (per M²)!
60 > x
61 > s![^0-9][^0-9]*!!
62 > s![.][^0-9]*!!
63 > s!-!/!
64 > H
65 > g
66 > y!\n! !
67 > '
68 > some-description-with-words (per M²) 012/63099
69
70
71 Thank you all so much for your responses! Plenty to try out.
72
73 I love this M/L! :-)
74
75 --
76 Regards,
77 Mick

Attachments

File name MIME type
signature.asc application/pgp-signature