LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Multiple input into seq command from pipe / single column unique numbers (https://www.linuxquestions.org/questions/programming-9/multiple-input-into-seq-command-from-pipe-single-column-unique-numbers-880578/)

geehog 05-13-2011 01:44 PM

Multiple input into seq command from pipe / single column unique numbers
 
Have this script which is reading in a series of files, one at a time with while-do-done loop, each file goes through various greps/awk's where this info is then saved to various files for later use. i.e....

Script is being run on Linux Red Hat,
#! /bin/bash

...etc ..
etc..

while (( somme > 0 )); do


declare -i sme=$(( $dmlmax - $dml ))

fne="01_????-""0$dml"".txt"

if test -f $fne; then

grep "Elap Time =" $fne | awk '{print $3}' > elp.txt
grep "on Nole" $file_name | awk '{print $3, $9)}' > nole.txt
....etc..
....etc...
grep ....

...
done

In one of the grep/awk's the output (currently) are 2 columns (min max), i.e....| awk '{print $1, $2}' | sort -u which outputs (e.g.)

1 3
2 2
33 35
40 40
and on ...
..and on...
...


The number of "min max" pairs varies from file to file. Want to output a single column of unique numbers from the min max pairs & get the number of them for input to a file...i.e...

.....| sort -u | <PROCESS> | sort -u | wc | awk '{print $1}' > z.txt

where <PROCESS> is some process/technique that will generate a single column of integers (increment of 1) to pipe into the next one (sort -u)

i.e. (example from above)
1
2
3
2
33
34
35
40

Have tried command seq - only works for single pair input i.e.
.|awk '{print $1, $2}' | sort -u | tail -1 | seq `awk '{print $1, $2}'`

Is there any command like seq etc which will output a single column based on a input of min max numbers (increment 1) to pipe onwards to next command?

Couling 05-13-2011 03:58 PM

I'm a little unshore what you're asking for.

It sounds like you want to flatten out two columns into 1:

a b
c d

into

a
b
c
d


If so then may I suggest that awk is your friend here.
It's a full on programming language in a single command.
Code:

awk '{print $1; print $2}'
Its worth looking up some tutorials on awk is so often used for one stage in a pipe when it will do the task of the whole pipe.
If you can't find anything shorter then: http://www.gnu.org/software/gawk/manual/gawk.html#Regex

For example you often see this in scripts:
grep foo | awk '{print $1}'

Where the developer really ment:
awk '/foo/ {print $1}'

geehog 05-13-2011 10:26 PM

Hi Couling,

No - I have a range of numbers i.e.
1 5
2 3
4 4
34 36
etc...

The above are Ranges (min max values): 1 through to 5, 2 through to 3, 4 through to 4, 34 through to 35 & so on...

Need to generate all numbers in these sequences (integers) .. 1,2,3,4,5 2,3 4 34,35 in a single column listing
1
2
3
4
5

2
3
4

34
35

Will then put this through a sort -u to remove duplicate numbers. With this new column list, will then use wc command to get the total count of individual number.

Issue with seq command is that it (appears) to only accept 1 row of numbers (i.e. unable to input multiple values of min max) to generate integer sequence numbers.

grail 05-13-2011 11:46 PM

Well I don't think you need the several different commands you are using, but as you hide some of the commands being piped in it is hard to know.
This would seem to solve what you have given us:
Code:

awk '{for(i=$1; i <= $2; i++)if(! (i in _))_[i]++}END{for(x in _)print x}' | sort -n
I am with Couling thought that using a useless grep that awk can already do serves no purpose except to make the code look convoluted.

geehog 05-14-2011 12:18 AM

Thanks Grail - is now solved. New to scripting - hence code is not as shorten as can be, plus also reading in various lines & sorting/filtering them through to the particular lines /items needed to get the pairs of min max numbers.


All times are GMT -5. The time now is 05:33 AM.