How to get unique string from variable in bash -
hello ask how can unique string variable.
while read -r line route=$(echo $line | awk -f'[:]' '{print $2}') #get path log file if [ "`dirname "$route"`" == "`xrealpath "$pwd"`" ]; #compare path log file $pwd name=$(echo $line | awk -f'[:]' '{print $1}') #take name 1st column in log file fi if ! [ "$name" == "$help_name" ]; echo $name help_name=$name pom=$pom:$name fi done < $wedi_rc
sample logfile:
proj.sh:/users/tom/documents/proj.sh:2015-03-21:1 proj1.sh:/users/tom/documents/proj.sh:2015-03-21:1 proj.sh:/users/tom/documents/proj.sh:2015-03-21:2 proj1.sh:/users/tom/documents/proj.sh:2015-03-21:2 proj.sh:/users/tom/documents/proj.sh:2015-03-21:3 proj1.sh:/users/tom/documents/proj.sh:2015-03-21:3
how can echo each unique 1 time?
my output looks this:
proj.sh proj1.sh proj.sh proj1.sh proj.sh :proj.sh:proj1.sh:proj.sh:proj1.sh:proj.
expecting output:
proj.sh proj1.sh
i don't know how files can readed in while cycle. cannot use temporary files thank you
answer original version of question
this uses associative array seen
keep track of names have been seen:
declare -a seen while read -r line ... blabla ... if [ -z "${seen[$name]}" ]; echo $name seen["$name"]=1 pom=$pom:$name fi done < "$wedi_rc"
working example (no blabla
)
let start file:
$ cat file proj.sh proj1.sh proj.sh proj1.sh proj.sh
we run code (note ...blabla...
has been removed , loop reads in name
directly):
$ cat script.sh declare -a seen while read -r name if [ -z "${seen[$name]}" ]; echo $name seen["$name"]=1 pom=$pom:$name fi done < file declare -p pom
this result:
$ bash script.sh proj.sh proj1.sh declare -- pom=":proj.sh:proj1.sh"
answer revised question
in revised question, following code appears:
route=$(echo $line | awk -f'[:]' '{print $2}') #get path log file if [ "`dirname "$route"`" == "`xrealpath "$pwd"`" ]; #compare path log file $pwd name=$(echo $line | awk -f'[:]' '{print $1}') #take name 1st column in log file
this means that, code runs, name
may never set depending on current directory when script run. explain error messages reported in comments.
Comments
Post a Comment