10
zgrep -i XXX XXX | grep -o "RID=[0-9|A-Z]*" |
   uniq | cut -d "=" -f2 |
   xargs -0 -I string echo "RequestID="string

My output is

RequestID=121212112
8127127128
8129129812

But my requirement is to have the request ID prefixed before all the output. Any help is appreciated

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
User
  • 401
  • 2
  • 8
  • 15
  • Since I am new to this ,, I am not aware of the formalities .. Thank you for telling this and I will surely accept the answer – User Jun 21 '12 at 00:39
  • I was googling about how to concatenate with xargs, and the answer is: use `-I`. `man xargs`: *`-I replace-str`* *Replace occurrences of replace-str in the initial-arguments with names read from standard input. Implies -x and -L 1.* Please note that `-L 1` should suffice, so `-n 1` is useless. The `-0` is the problem here. – Mathieu CAROFF Jan 03 '19 at 20:16

2 Answers2

18

I had a similar task and this worked for me. It might be what you are looking for:

zgrep -i XXX XXX | grep -o "RID=[0-9|A-Z]*" | uniq | cut -d "=" -f2 | xargs -I {} echo "RequestID="{}

UNagaswamy
  • 2,068
  • 3
  • 30
  • 35
  • 1
    yes! **best answer**, `cat myEpochDates | xargs -I {} date -d @{}` generates ISO dates. Example `date -d @1234567890` – Peter Krauss Nov 17 '21 at 17:54
15

Try -n option of xargs.

-n max-args

Use at most max-args arguments per command line. Fewer than max-args arguments will be used if the size (see the -s option) is exceeded, unless the -x option is given, in which case xargs will exit.

Example:

$ echo -e '1\n2' | xargs echo 'str ='
str = 1 2

$ echo -e '1\n2' | xargs -n 1 echo 'str ='
str = 1
str = 2
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
  • Thank you for your comments ... Now my problem is this xargs -L 1 -I string echo "RequestId="string|xargs -L 1 -I string zcat XXXX.gz|agrep -dEOE string ... Now the xargs string has to pipe through the next one which is agrep would take .. But I am not getting the desired output.. Any ideas about this .. Thank you ... – User Jun 21 '12 at 00:41
  • @User I'm not sure I understand. You need to show the example input and desired output the same way you did in this question. Maybe start a new question? – Lev Levitsky Jun 21 '12 at 07:16
  • **bad answer**, non-complete. See about `xargs -n 1 date -d @`? not work. for example to concatenate as `date -d @1234567890` – Peter Krauss Nov 17 '21 at 17:51