Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xargs: String concatenation

Tags:

grep

xargs

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

like image 320
User Avatar asked Jun 20 '12 21:06

User


People also ask

How do I use xargs grep?

Combine xargs with grepUse xargs with the grep command to search for a string in the list of files provided by the find command. The example above searched for all the files with the . txt extension and piped them to xargs , which then executed the grep command on them.

What is xargs option?

xargs (short for "extended arguments") is a command on Unix and most Unix-like operating systems used to build and execute commands from standard input. It converts input from standard input into arguments to a command.

What is xargs in shell script?

The xargs command is used in a UNIX shell to convert input from standard input into arguments to a command. In other words, through the use of xargs the output of a command is used as the input of another command.

What is the default command used by xargs?

xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is echo) one or more times with any initial-arguments followed by items read from standard input.


2 Answers

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="{}

like image 178
UNagaswamy Avatar answered Nov 06 '22 21:11

UNagaswamy


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
like image 15
Lev Levitsky Avatar answered Nov 06 '22 21:11

Lev Levitsky