0

I have many files on UNIX and wish to fetch number in that file associated with a specified pattern.

Most of the file will have a unique pattern in file like below

some text abc
some text abc
some text abc
(3 rows)

I want to print only number 3 using awk.

The number could vary from file to file, it could be 35644 or any numeric value as well.

I want to find that number using awk not sed or grep as HP unix doesn't support advanced sed/grep features.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Minsec
  • 35
  • 6
  • Are the lines consecutive, or can there be different lines among them? – choroba Jan 23 '18 at 10:22
  • no need of advanced sed/grep for such simple case (basic sed would be enough). *I want to print only number* - only number OR a filename also? – RomanPerekhrest Jan 23 '18 at 10:23
  • The last row will be `(number rows)` text always. Lines in file may vary but there will be always a pattern like `(number rows)`. – Minsec Jan 23 '18 at 10:23
  • @RomanPerekhrest I'm on HP unix where sed -i isn't support, hence I don't want to go for adanced grouping in sed as it doesn't work. – Minsec Jan 23 '18 at 10:26

3 Answers3

0

Does the following work for you?

sed -e 's/(\([0-9][0-9]*\) rows)/\1/;t;d'

If the replacemenet of the (N rows) by N was successful, t goes to the end of the script, otherwise, the d removes the line (i.e. all lines not containing the expression will be skipped).

choroba
  • 231,213
  • 25
  • 204
  • 289
0

As you don't want (or you can't) use simple sed approaches, here's awk solution:

Sample input files:

$ head file[123]
==> file1 <==
some text abc
some text abc
some text abc
(3 rows)

==> file2 <==
some text abc
some text abc
some text abc
some text abc
some text abc
(5 rows)

==> file3 <==
some text abc
some text abc
(2 rows)

$ for f in file[123]; do awk 'END{ print FILENAME, substr($1, 2) }' "$f"; done
file1 3
file2 5
file3 2

sed solution (in my variation) would look as:

for f in file[123]; do echo -n "$f "; sed -n '$ s/[() ]\|rows//gp' "$f"; done
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
0

Perhaps with this awk

awk -v RS='(' 'END{print $1}' infile
ctac_
  • 2,413
  • 2
  • 7
  • 17