Tag Info

New answers tagged

2

The problem with your code is the tr command and your (wrong) assumptions about item[0] and item[1]. There are of course various ways to handle that; here is one way (staying close to your approach): while IFS=, read left right do output_names+=( "${left// }" ) output_plots+=( "${right// }" ) done <list (I've only depicted the loop changes, ...


0

This returns a string with spaces: item=$(echo $item | tr "," "\n") change it like this to have an array: item=($(echo $item | tr "," "\n")) output: ** output_names: blue red orange baseball university school desk monitor earphone ** output_plots: none none plot none none none plot none none There may be better ways to achieve your task tough


1

A neat way to do this is to use a bash array to split up a string on spaces. You can declare an array simply by using brackets: var="129 148 181" vars=( $var ) echo "First word of var: '${vars[0]}'" echo "Second word of var: '${vars[1]}'" echo "Third word of var: '${vars[2]}'" echo "Number of words in var: '${#vars[@]}'"


2

Assuming that backslashes themselves are also escaped in your strings (as \x5c, presumably), which udev seems to do, you should use Bash's printf builtin: printf -v translated '"%b"' "$ID_FS_LABEL_ENC" If we try that on your example string: $ ID_FS_LABEL_ENC='New\x20Folder' $ printf -v translated '"%b"' "$ID_FS_LABEL_ENC" $ echo "Translated to: ...


0

In addition to jasonwryan's suggestion, you can use cut: echo $var | cut -d' ' -f1 The above cuts the echo output with a space delimiter (-d ' ') and outputs the first field (-f1)


1

Try this: string=$'New\x20Folder' or string="New\x20Folder" string="$(echo -e "$string")"


2

Bash only, by using a variable: # code: shopt -s extglob A="$PWD//"; echo "Original: $A"; echo "Result: ${A//+(\/)//}" # output: Original: /home/myhome/Projects/Bob/build// Result: /home/myhome/Projects/Bob/build/ // = search and replace, keep searching and replacing +(/) = match one or more of "/" // = replace with, "/" For a better visual ...


4

You mean: pdflatex "$1" && evince "${1%.tex}.pdf"


0

what about pdflatex "$1" && evince "${1%.tex}.pdf" where ${x%suffixe} will delete suffixe from end of ${x}


1

If you add set -vx under #!/bin/bash and then run your script you will see: [[ active port: <analog-output-speaker> == active port: <analog-output-speaker> ]] And you can see the strings are not the same because first string has some spaces. for removing whole string's spaces you can use: trim -d '[[:space:]] for removing leading ...


0

Non-printables troubling you. Try this: $ hash=" active port: <analog-output-speaker>%%%%%" $ echo $hash active port: <analog-output-speaker>%%%%% $ echo "<$hash>" < active port: <analog-output-speaker>%%%%%> $ if [[ "$hash" =~ "active port" ]]; then echo matched; else echo no match; fi matched $ if [[ "$hash" =~ "non ...


3

Always quote variables you say echo $hash but that strips leading and trailing whitespace and condenses all internal spaces. it does not show the content of the variable $hash say instead echo "<$hash>" and you will see the leading whitespace. It looks like pulse audio is using a tab before the word active. So put a tab at the start of the value in ...


13

I don't know if your version of sed will be binary-clean or if will choke on what it thinks are really long lines in its input, but barring those issues, editing the string in-place should work. To see whether it does, compare the old and new versions with cmp -l. It should tell you whether or not the only three differences between the two files are those 3 ...


0

To compare each line to previous starting from second: awk ' NR==1{ split($0,U,"") next} { s=split($0,A,"") f=1 if(length(U)>s) s=length(U) for(i=1;i<=s;i++) if(A[i]==U[i]&&f!=0) printf("%s",A[i]) else { f=0 ...


0

If the two files are guaranteed to be differing one possibility is f1=FILE0000010985.LOG f2=FILE0000010999.LOG for ((l=0; l<${#f1}; l++)) do [[ ${f1:0:l} != "${f2:0:l}" ]] && break done printf "%s\n" "${f1:0:l-1}" (If files may be equal an additional test has to be added.)


1

Pure bash solution. Notice that the output for 1 and 2 is FILE00000109, not FILE0000010. #!/bin/bash arr=(FILE0000010985.LOG FILE0000010999.LOG FILE0000011000.LOG ) for (( i=0; i<${#arr[@]}; ++i )) ; do for (( j=i + 1; j<${#arr[@]}; ++j )) ; do x=${arr[i]} y=${arr[j]} p=0 while [[ ${x:0:p} == ${y:0:p} ...



Top 50 recent answers are included