1

I have seen this question a few times, but the solutions I cannot get to work.

I have the following command:

printf '%s\n' "${fa[@]}" | xargs -n 3 bash -c 'cat *-$2.ss | sed -n 11,1p ; echo $0 $1 $2;'

where

printf '%s\n' "${fa[@]}"

O00238 115 03
O00238 126 04

and cat *-$2.ss gives:

   1 D C   0.999  0.000  0.000
   2 L C   0.940  0.034  0.012
   3 H C   0.971  0.005  0.015
   4 P C   0.977  0.005  0.009
   5 T C   0.970  0.009  0.018
   6 L C   0.977  0.006  0.011
   7 P C   0.864  0.027  0.014
   8 P C   0.966  0.018  0.011
   9 L C   0.920  0.038  0.039
  10 K C   0.924  0.043  0.039
  11 D C   0.935  0.036  0.035
  12 R C   0.934  0.023  0.053
  13 D C   0.932  0.022  0.046
  14 F C   0.878  0.041  0.088
  15 V C   0.805  0.031  0.198
  16 D C   0.834  0.039  0.108
  17 G C   0.882  0.019  0.071
  18 P C   0.800  0.031  0.132
  19 I C   0.893  0.039  0.070
  20 H C   0.823  0.024  0.179
  21 H C   0.920  0.026  0.070
  22 R C   0.996  0.001  0.002

running the command then produces

  11 D C   0.935  0.036  0.035
O00238 115 03
  11 K C   0.449  0.252  0.270
O00238 126 04

Even lines are the output of sed -n 11,1p, odd lines the output of echo $0 $1 $2

How do I pair the output on the same line i.e.

  11 D C   0.935  0.036  0.035 O00238 115 03
  11 K C   0.449  0.252  0.270 O00238 126 04

I have tried:

printf '%s\n' "${fa[@]}" | xargs -n 3 bash -c 'cat *-$2.ss | {sed -n 11,1p ; echo $0 $1 $2;} | tr "\n" " "'

as suggested here: Concatenate in bash the output of two commands without newline character

however I get

O00238: -c: line 0: syntax error near unexpected token `}'
O00238: -c: line 0: `cat *-$2.ss | {sed -n 11,1p ; echo $0 $1 $2;} | tr "\n" " "'

What is the problem?

Community
  • 1
  • 1
brucezepplin
  • 9,202
  • 26
  • 76
  • 129
  • 2
    What is in the original array? Perhaps it'd be best to start from there, rather than picking through the pipeline you already have. – Tom Fenech Apr 21 '15 at 08:08
  • 1
    Instead of people trying to bolt something onto the end of your command,if you provide us with the original input and expected output then i think this can all be done in a single command. –  Apr 21 '15 at 08:09
  • thanks both - have added output of `printf '%s\n' "${fa[@]}"` – brucezepplin Apr 21 '15 at 08:12
  • 1
    @brucezepplin we would also need to know what is in all the files in `*-$2.ss` (or an example of what would be in there) –  Apr 21 '15 at 08:13
  • thanks JID - have added output of `cat *-2.ss` – brucezepplin Apr 21 '15 at 08:19
  • Where's the `11 K C` line in the output of `cat`? – Tom Fenech Apr 21 '15 at 08:21
  • @ Tom - each call to cat finds a different file. JID asked for an example - the one I give refers to the first line i.e. `11 D C 0.935 0.036 0.035 O00238 115 03` – brucezepplin Apr 21 '15 at 08:23
  • So it's the 11th line of each of the files, right? – Tom Fenech Apr 21 '15 at 08:23
  • 1
    this could all be done in one simple, clear awk command but I see you've already selected an answer that adds more complexity to your pipeline so if you'd like to see how to do it all at once post a followup question. – Ed Morton Apr 21 '15 at 13:33
  • hi Ed - I'm open to any solution really. It's only that I was already trying to do it in bash only and I can only choose one answer to accept. It may be helpful for others as well to see an awk solution – brucezepplin Apr 21 '15 at 13:44

2 Answers2

2

You could try using something like this:

i=0
for f in *-"$2".ss; do printf '%s %s\n' "$(sed -n '11p' "$f")" "${fa[$((i++))]}"; done

This loops through your files and prints the 11th line alongside a slice from the array fa, whose index i increases by 1 every iteration.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
1

I could not reproduce your setup, but

printf "O00238 115 03\nO00238 126 04" | xargs -n 3 bash -c 'cat test.dat | sed -n 11,1p | tr -d "\n"; echo " $0 $1 $2"'

gives

11 D C   0.935  0.036  0.035 O00238 115 03
11 D C   0.935  0.036  0.035 O00238 126 04

which should work in your case. I just deleted the newline of the sed command.

martin
  • 3,149
  • 1
  • 24
  • 35