user3159380
user3159380

Reputation: 11

putting separate Bash greps into columns?

From the main directory (containing several sub-directories), I want to cd into all directories ending in *bk, and do three things:

$1 = ls *enp
$2 = grep "Start time" *initial.rpt
$3 = grep "Stop time" *ending.rpt

I would like the output to be organized with all 3 components on the same line:

1. Bill.enp    12:00    14:30
2. Barb.enp    15:00    15:30
3. Brad.enp    16:00    17:30
4. Buck.enp    18:00    19:00
5. Burt.enp    19:30    21:00

Any help would be appreciated!

Upvotes: 1

Views: 126

Answers (2)

gboffi
gboffi

Reputation: 25023

Iff the commands

ls *enp
grep "Start time" *initial.rpt
grep "Stop time" *ending.rpt

all give place to the same number of lines and the lines are always in the proper sequence you can use the following script (credit to Etan Reisner for the original mention of process substitution)

for d in *.bk ; do
  if [ -d $d ] ; then
    cd $d
    paste -d\\t <(ls *enp) <(grep "Start time" *initial.rpt) <(grep "Stop time" *ending.rpt)
    cd -
 done

I have used tabs as delimiters for the paste command, the default is a space.

Upvotes: 0

Etan Reisner
Etan Reisner

Reputation: 80931

Using those values you want the paste command.

If those are in three files: paste out1 out2 out3

If those are in three variables: paste <(echo "$s1") <(echo "$s2") <(echo "$s3")

Upvotes: 1

Related Questions