Splitting script with getops bash script -
i'm trying split code using options getops, i'd 1 script run if 1 option set, , script run if option set, see code:
#!/bin/bash # posix variable optind=1 # reset in case getopts has been used in shell. # initialize our own variables: 1="" 2="" verbose=0 while getopts "h?v:f:m:l:" opt; case "$opt" in h|\?) exit 0 ;; v) verbose=1 ;; f) 1=$optarg ;; m) 2=$optarg ;; esac done file in $1; 1="$(sed '1d' $1 | awk -f \" '{print $2}')" printf "$1\n" done <"$1" file in $2; 2="$(awk '{print $1}' $2 | sort | uniq)" printf "$2\n" done <"$2" so in code here, if option -f set file, i'd loop starting "for file in $1" run. , if -m set file, i'd loop starting "for file in $2" run.
i'd make -m or -f can run, i.e. not @ same time.
the command-line usage is: ./script.sh -f file.txt or ./script.sh -m file.txt
how can set this? (accepting other options other getops too)
example file -f:
<blank line> "b2f5ff47436671b6e533d8dc3614845d","54fd1711209fb1c0781092374132c66e79e2241b","path/path","output" "8fa14cdd754f91cc6554c9e71929cce7","4a0a19218e082a343a1b17e5333409af9d98f0f5","path/path2","output2" "2510c39011c5be704182423e3a695e91","27d5482eebd075de44389774fce28c69f45c8a75","path/path3","output3" ... example file -m:
b2f5ff47436671b6e533d8dc3614845d /paths/path 2510c39011c5be704182423e3a695e91 /paths/path2 i'm using awk pull out first field -f , -m file. sed command removes first line () of file before extracts first field out.
expected output:
b2f5ff47436671b6e533d8dc3614845d 8fa14cdd754f91cc6554c9e71929cce7 2510c39011c5be704182423e3a695e91 b2f5ff47436671b6e533d8dc3614845d 2510c39011c5be704182423e3a695e91 which can use match against hashlist file.
assuming contents of 2 files below,
cat file1 "b2f5ff47436671b6e533d8dc3614845d","54fd1711209fb1c0781092374132c66e79e2241b","path/path","output" "8fa14cdd754f91cc6554c9e71929cce7","4a0a19218e082a343a1b17e5333409af9d98f0f5","path/path2","output2" "2510c39011c5be704182423e3a695e91","27d5482eebd075de44389774fce28c69f45c8a75","path/path3","output3" and other file being,
cat file2 b2f5ff47436671b6e533d8dc3614845d /paths/path 2510c39011c5be704182423e3a695e91 /paths/path2 you can use single awk as
awk 'fnr==nr && nf{gsub(/"/,"",$1); uniquestring[nr]=$1; next}{uniquestring[nr]=$1; next}end{for (i in uniquestring) print uniquestring[i]}' fs="," file1 fs=" " file2 which gives
b2f5ff47436671b6e533d8dc3614845d 8fa14cdd754f91cc6554c9e71929cce7 2510c39011c5be704182423e3a695e91 b2f5ff47436671b6e533d8dc3614845d 2510c39011c5be704182423e3a695e91 the core logic of awk creating array uniquestring index nr; special variable in awk keeps track of line numbers each lines processed in file
the fnr==nr parses first file storing entries in $1 hash-map. since values containing double-quotes, removed using gsub() , field split fs done field ,.
once lines parsed in file1, following {..} part executed on second file file2 again storing entries of $1 in array, field separator set white-space.
after lines processed, end clause used print lines parsed.
Comments
Post a Comment