Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to use values with sed in shell scripting?

i am trying te write a shell script in alphametic ,

i have 5 parameters like this

$alphametic 5790813 BEAR RARE ERE RHYME

to get

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ABEHMRY -> 5790813

i tried this :

  #!/bin/bash
    echo "$2 $3 $4 $5" | sed  's/ //g ' | sed 's/./&\n/g' | sort -n |  sed '/^$/d' | uniq -i > testing
    paste -sd ''  testing  > testing2 
    sed "s|^\(.*\)$|\1 -> ${1}|" testing2 

but i get error (with the last command sed), i dont know where is the problem .

>Solution :

#!/bin/sh

val="$1"

shift

printf '%s' "$@" | awk -v FS='' -v OFS='\n' '{$1=$1}1' | sort -n | uniq | tr -d '\n'

printf " -> %s\n" "$val"
  • shift removes $1 from the arguments. $@ will then contain all the arguments but the first one, and the old $2 will then be $1, $3 will be $2, etc…
  • printf '%s' "$@" concatenates all the arguments into a single string
  • awk -v FS='' -v OFS='\n' '{$1=$1}1' outputs each character vertically
  • tr -d '\n' removes all \n characters
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading