1

How can I check the length of a piped input? I'm trying:

echo "A" | xargs sh -c 'echo ${#$1}'

Also:

echo "A" | xargs sh -c 'echo ${#\"$1\"}'

Based on the answers here, for a non-pipe example.

3
  • 1
    Just use wc ? echo "A" | wc -c
    – steve
    Jul 7, 2017 at 13:13
  • @steve thanks that looks like what I want. I'm just wondering why echo "A" | wc -c returns 2? I'm expecting 1. Jul 7, 2017 at 13:14
  • 1
    Newline. Try echo -n A or printf A
    – steve
    Jul 7, 2017 at 13:17

2 Answers 2

3

As @steve wrote, just use wc -c to count the number of bytes. See man wc for other options.

echo appends a newline to the end of output, which is why you'll get one more character than you might expect when counting. Use printf to control output more strictly.

3
  • Thanks, this is almost what I need but I also need to use the value. See anything wrong with the following? echo "A" | xargs sh -c 'wc -c \"$1\"' Jul 7, 2017 at 13:18
  • It seems stupid but I'm trying to run a command on the value only if length is less than 2. Jul 7, 2017 at 13:19
  • I will open as a new question. I think my approach was off on the comment above. Jul 7, 2017 at 13:22
1

Let us say what you want is to run the command if an argument is less than 2 long:

seq 100 | parallel echo '{= length($_) >= 2 and skip =}'

It will also run the jobs in parallel - one on each core.

NB: GNU Parallel is not the parallel in moreutils.

GNU Parallel is a general parallelizer and makes is easy to run jobs in parallel on the same machine or on multiple machines you have ssh access to.

If you have 32 different jobs you want to run on 4 CPUs, a straight forward way to parallelize is to run 8 jobs on each CPU:

Simple scheduling

GNU Parallel instead spawns a new process when one finishes - keeping the CPUs active and thus saving time:

GNU Parallel scheduling

Installation

For security reasons you should install GNU Parallel with your package manager, but if GNU Parallel is not packaged for your distribution, you can do a personal installation, which does not require root access. It can be done in 10 seconds by doing this:

(wget -O - pi.dk/3 || curl pi.dk/3/ || fetch -o - http://pi.dk/3) | bash

For other installation options see http://git.savannah.gnu.org/cgit/parallel.git/tree/README

Learn more

See more examples: http://www.gnu.org/software/parallel/man.html

Watch the intro videos: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

Walk through the tutorial: http://www.gnu.org/software/parallel/parallel_tutorial.html

Sign up for the email list to get support: https://lists.gnu.org/mailman/listinfo/parallel

4
  • Doesn't work for me, on ubuntu had to install moreutils because I don't have parallel, but after install the command returns nothing. Jul 8, 2017 at 1:15
  • I installed parallel and it runs but it prints numbers 1-100. Does it only print if less than 2 long on your PC? Jul 8, 2017 at 1:27
  • You need version >=20161122.
    – Ole Tange
    Jul 8, 2017 at 1:33
  • This will work with 20150822 or later: seq 100 | parallel-20150822 echo '{= length($_) >= 2 and $job->skip() =}'
    – Ole Tange
    Jul 8, 2017 at 1:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .