Tag Info

New answers tagged

1

A separate printf can do the 0 padding with %0Xd when needed. zero_pad(){ # zero_pad <string> <length> [ ${#1} -lt $2 ] && printf "%0$(($2-${#1}))d" '' printf "%s\n" "$1" } . $ zero_pad "" 5 00000 $ zero_pad "over" 5 0over $ zero_pad "under" 4 under $ zero_pad "exact" 5 exact $ zero_pad " space" 7 0 space


2

As @Gnouc has already noted, you're halfway there already: line='some chars' printf '%0'$((20-${#line}))'d%s\n' 0 "$line" ###OUTPUT### 0000000000some chars Of course, then you still wind up with a 0 even if line is longer than 20 chars, and that's only if printf doesn't fail with an error because it doesn't properly handle the -dash in its format string ...


2

Just do the padding manually. I don't think there's a more straightforward solution if you stick to POSIX tools. while [ ${#line} -lt 20 ]; do line=0$line done or n=${#line} while [ $n -lt 20 ]; do printf '0' n=$((n-1)) done printf "$line\n" In zsh, of course, there's syntax for that. Unlike the snippets above, this one truncates the string if ...


4

I assume that the string can contain any character except newlines and null bytes. You can quote the string for use as a sed pattern. The characters $*./[\^ need to be preceded by a backslash. In the replacement text, you need to quote the characters \&/. regexp=$(printf %s "$old" | sed 's:[$*./\[^]:\\&:g') replacement=$(printf %s "$new" | sed ...


0

Yes, you should be able to use the -f option to specify a file containing a [list of] expression[s] -f script-file, --file=script-file add the contents of script-file to the commands to be executed However you will still need to escape any special characters (AFAIK there is no sed equivalent of grep's --fixed-strings) - if perl is available ...


2

If you can use perl: $ perl -e 'print sprintf "%020s\n","shortstring"' 000000000shortstring For more general: $ perl -e 'print sprintf "%020s\n",shift' shortstring 000000000shortstring $ perl -e 'print sprintf "%020s\n", $_ for @ARGV' shortstring qwerty 000000000shortstring 00000000000000qwerty Note Some platform can be formated output string with ...


2

%s is a string formatting specification. Zero padding is only defined to exist for numeric conversions: 0 For d, i, o, u, x, X, a, A, e, E, f, F, g, and G conversion specifiers, leading zeros (following any indication of sign or base) are used to pad to the field width ... For other conversions, the behavior is undefined. If your $line is a number, ...


5

I assume you've inadvertently trimmed the important part of your command lines out here: the URLs in question contain a ? character (or a *). ? and * are special glob matching characters to the shell. ? matches a single character in a filename, and * matches many. When zsh says: zsh: no matches found: http://myvideosite.com?video=123 it's telling you that ...


2

More elegant? No Shorter? Yes :) #!/bin/bash read string if [ ${#string} -ge 5 ]; then echo "error" ; exit else echo "done" fi


2

t=lkj echo ${t:0:${#t}-1} You get a substring from 0 to the string length -1. Note however that this substraction is bash specific, and won't work on other shells. For instance, dash isn't able to parse even echo ${t:0:$(expr ${#t} - 1)} For example, on Ubuntu, /bin/sh is dash


3

With bash, you can do: ${var::-1} Example: $ a=123 $ echo ${a::-1} 12


7

In a POSIX shell, the syntax ${t:-2} means something different - it expands to the value of t if t is set and non null, and otherwise to the value 2. To trim a single character by parameter expansion, the syntax you probably want is ${t%?} Note that in bash, ${t:(-2)} is legal as a substring expansion but is probably not what you want, since it returns the ...


0

It is easy enough to do using regular expression: n=2 echo "lkj" | sed "s/\(.*\).\{$n\}/\1/"


2

for removing the last n characters from a line that makes no use of sed OR awk: > echo lkj | rev | cut -c (n+1)- | rev so for example you can delete the last character one character using this: > echo lkj | rev | cut -c 2- | rev > lk from rev manpage: DESCRIPTION The rev utility copies the specified files to the standard output, ...


11

You need to quote your variables. Without quotes you are writing test -n instead of test -n <expression>. The test command has no idea that you provided a variable that expanded to nothing. In shell syntax, $VARIABLE outside of double quotes is the “split+glob” operator: the value of the variable is split into a list of whitespace-delimited words, and ...


0

With GNU grep, you can use lookbehind: $ grep -P '(?<!0) failed' file 3. verification: 10 passed 1 failed 4. verification: 10 passed 3 failed Or a shorter version of awk: $ awk '$5 > 0' file


1

Assuming the file is called in.txt, then:- awk '$5>0 {print $0; }' in.txt will give you:- 3. verification: 10 passed 1 failed 4. verification: 10 passed 3 failed I've made the assumption the the line numbers (1-5) is part of the file. If it isn't change the $5 to $4. If the line number is part of the file and you want to just print it without the ...


0

Please note that i'm not printint real numbers, but the first number in the line until the dot. With sed: cat inputfile.txt | sed '/ 0 failed$/d ; s/\..*//' With grep and sed: cat inputfile.txt | grep -v ' 0 failed$' | sed 's/\..*//' Output: 3 4



Top 50 recent answers are included