Gentoo Archives: gentoo-user

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

Replies

Subject Author
Re: [gentoo-user] [OT] awk - sed - grep = tying-myself-up-in-knots Mick <michaelkintzios@×××××.com>