xargs

In Bash, how can I parse multiple newline delimited JSON objects from a log file?

懵懂的女人 提交于 2019-12-04 04:53:38
问题 I am parsing through a log file and get result lines (using grep) like the following: 2017-01-26 17:19:40 +0000 docker: {"source":"stdout","log":"I, [2017-01-26T17:19:40.703988 #24] INFO -- : {\"tags\":\"structured_log\",\"payload\":{\"results\":[{\"baserate\":\"-1\"}]},\"commit_stamp\":1485451180,\"resource\":\"google_price_result_metric\",\"object_id\":\"20170126171940700\"}","container_id":"6ecbf7f64e4c9557e9dd1efbc6666a3c6c53f9cd5c18414ed5633cad8c302e","container_name":"/test-container

Alternatives to xargs -l

本小妞迷上赌 提交于 2019-12-04 03:45:51
I want to rename a bunch of dirs from DIR to DIR.OLD. Ideally I would use the following: find . -maxdepth 1 -type d -name \"*.y\" -mtime +`expr 2 \* 365` -print0 | xargs -0 -r -I file mv file file.old But the machine I want to execute this on has BusyBox installed and the BusyBox xargs doesn't support the "-I" option. What are some common alternative methods for collecting an array of files and then executing on them in a shell script? praetorian droid You can use -exec and {} features of the find command so you don't need any pipes at all: find -maxdepth 1 -type d -name "*.y" -mtime +`expr 2

When should xargs be preferred over while-read loops?

孤街浪徒 提交于 2019-12-03 14:52:57
问题 xargs is widely used in shell scripting; it is usually easy to recast these uses in bash using while read -r; do ... done or while read -ar; do ... done loops. When should xargs be preferred, and when should while-read loops be preferred? 回答1: The thing with while loops is that they tend to process one item at a time, often when it's unnecessary. This is where xargs has an advantage - it can batch up the arguments to allow one command to process lots of items. For example, a while loop: pax>

Ignore empty results for xargs in Mac OS X

不想你离开。 提交于 2019-12-03 11:51:37
The code for my website uses this piece of code for automatic deployment on the server (Ubuntu). cmd = 'cd ' + checkout_dir + ' && ' + svn_command + " st | awk '{print $2}' | grep -v ^deploy | tac | xargs -r" + svn_command + " revert -R && " + svn_command + ' up -r ' + options.revision What this command does is it cd into checkout directory, runs svn status , and then extracts the filename ( $2 ), removes the deploy directory and all its files from the list (I don't want to revert it). If there are no argument it does not run the svn revert command, else it does. Unfortunately the xargs -r

How to sleep for 1 second between each xargs command?

。_饼干妹妹 提交于 2019-12-03 11:15:09
问题 For example, if I execute ps aux | awk '{print $1}' | xargs -I {} echo {} I want to let the shell sleep for 1 second between each echo . How can I change my shell command? 回答1: You can use following syntax: ps aux | awk '{print $1}' | xargs -I % sh -c '{ echo %; sleep 1; }' Be careful with spaces and semicolons though. After every command between brackets, semicolon is required (even after last one). 回答2: Replace echo by some shell script named sleepecho containing #!/bin/sh sleep 1 echo $*

how to output file names surrounded with quotes in SINGLE line?

天大地大妈咪最大 提交于 2019-12-03 10:45:27
问题 I would like to output the list of items in a folder in the folowing way: "filename1" "filename2" "file name with spaces" "foldername" "folder name with spaces" In other words, item names must be in a single line, surrounded with quotes (single or double) and divided by spaces. I know that find . | xargs echo prints output in a single line, but I do not know how to add quotes around each item name. This code is part of a bsh script. The solution can therefore be a set of commands and use

Command to remove all npm modules globally?

匿名 (未验证) 提交于 2019-12-03 09:02:45
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Is there a command to remove all global npm modules? If not, what do you suggest? 回答1: The following command removes all global npm modules. Note: this does not work on Windows. For a working Windows version, see Ollie Bennett's Answer . npm ls -gp --depth=0 | awk -F/ '/node_modules/ && !/\/npm$/ {print $NF}' | xargs npm -g rm Here is how it works: npm ls -gp --depth=0 lists all global top level modules (see the cli documentation for ls ) awk -F/ '/node_modules/ && !/\/npm$/ {print $NF}' prints all modules that are not actually npm itself

Understanding the UNIX command xargs

心不动则不痛 提交于 2019-12-03 04:48:10
问题 I'm pretty much confused on this. Need some clarifications. Example 1 : pgrep string | xargs ps Example 2 : find . | xargs grep whatever From Example 1, I gather it's this way: Search for a "string" which is part of name of running process and return the process-ids of all the matches to 'xargs ps' -> which just appends ps to the matches (which are process-ids themselves) to get the same output as : ps <processid> Can someone explain what xargs really does in this case? From Example 2, I

Shell Scripting: Using xargs to execute parallel instances of a shell function

落爺英雄遲暮 提交于 2019-12-03 03:17:53
I'm trying to use xargs in a shell script to run parallel instances of a function I've defined in the same script. The function times the fetching of a page, and so it's important that the pages are actually fetched concurrently in parallel processes, and not in background processes (if my understanding of this is wrong and there's negligible difference between the two, just let me know). The function is: function time_a_url () { oneurltime=$($time_command -p wget -p $1 -O /dev/null 2>&1 1>/dev/null | grep real | cut -d" " -f2) echo "Fetching $1 took $oneurltime seconds." } How does one do

Ignore empty results for xargs in Mac OS X

匿名 (未验证) 提交于 2019-12-03 02:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: The code for my website uses this piece of code for automatic deployment on the server (Ubuntu). cmd = 'cd ' + checkout_dir + ' && ' + svn_command + " st | awk '{print $2}' | grep -v ^deploy | tac | xargs -r" + svn_command + " revert -R && " + svn_command + ' up -r ' + options.revision What this command does is it cd into checkout directory, runs svn status , and then extracts the filename ( $2 ), removes the deploy directory and all its files from the list (I don't want to revert it). If there are no argument it does not run the svn revert