0

I'm getting 2 arguments inside a script and I want to replace part of the string in arg1 based of the format of arg2, where DATEFORMAT is the reserved word for the placeholder. For example:

]$ ./a.sh nir_20210401-test (1).txt nir_DATEFORMAT-file.txt
nir_20200101-test (1).txt

a.sh looks like:

DATE="20200101"
NAME="${1}"
FORMAT="${2}"
#getting the delimiter before the DATEFORMAT
SRC_FIRST_DELIMITER="${FORMAT%%DATEFORMAT*}" 
SRC_FIRST_DELIMITER="${SRC_FIRST_DELIMITER: -1}"
#getting the delimiter after the DATEFORMAT
SRC_SECOND_DELIMITER="${FORMAT##*DATEFORMAT}" 
SRC_SECOND_DELIMITER="${SRC_SECOND_DELIMITER:0:1}"
#the index/place of DATEFORMAT based of the delimiter
SRC_FORMAT_DELIMITER_IDX=$(<<<"$FORMAT" awk -v RS="$SRC_FIRST_DELIMITER" -v word="DATEFORMAT" '$0 ~ word{print NR-1}')

echo "$NAME"   <--- I want here to use SRC_FIRST_DELIMITER, SRC_SECOND_DELIMITER, and  SRC_FORMAT_DELIMITER_IDX to replace 20210401 with 20200101

EDIT: This one is a good start, but of course this is only if the first and second delimiter are the same which is not what I want:

 echo "${NAME}" | awk -F${SRC_FIRST_DELIMITER} -v OFS=${SRC_FIRST_DELIMITER} '{$'$SRC_FORMAT_DELIMITER_IDX'="20200101"; print }'
Nir
  • 2,497
  • 9
  • 42
  • 71
  • side-note: this is surely possible with the shell, however, if this is a real-worl application I recommend to use a different programming language, like Python – hek2mgl Sep 12 '21 at 11:03
  • this is only small part of bigger bash script – Nir Sep 12 '21 at 12:10
  • Please read [correct-bash-and-shell-script-variable-capitalization](https://stackoverflow.com/questions/673055/correct-bash-and-shell-script-variable-capitalization) and then fix your script. – Ed Morton Sep 12 '21 at 18:32
  • Your shell script is manipulating text. A shell is a tool to create/destroy files and processes and sequence calls to other tools. The guys who invented shell also invented awk for shell to call to manipulate text. Everything in your shell script is inappropriate for shell and should instead be a single awk script. – Ed Morton Sep 12 '21 at 18:37
  • If you [edit] your question to provide a [mcve] with concise, testable sample input and expected output then we can help you do whatever it is you're trying to do. You talk about and use arg 1 and arg 2 but call your script with 3 args so it's hard to tell what is going on with it. – Ed Morton Sep 12 '21 at 18:38

1 Answers1

0

Not pretty but works:

DATE="20200101"
NAME="${1}"
FORMAT="${2}"
#getting the delimiter before the DATEFORMAT
SRC_FIRST_DELIMITER="${FORMAT%%DATEFORMAT*}" 
SRC_FIRST_DELIMITER="${SRC_FIRST_DELIMITER: -1}"
#getting the delimiter after the DATEFORMAT
SRC_SECOND_DELIMITER="${FORMAT##*DATEFORMAT}" 
SRC_SECOND_DELIMITER="${SRC_SECOND_DELIMITER:0:1}"
#the index/place of DATEFORMAT based of the delimiter
SRC_FORMAT_DELIMITER_IDX=$(<<<"$FORMAT" awk -v RS="$SRC_FIRST_DELIMITER" -v word="DATEFORMAT" '$0 ~ word{print NR-1}')

FILE_1ST_PART=$(echo "${NAME}" | cut -d ''$SRC_FIRST_DELIMITER'' -f1-$((SRC_FORMAT_DELIMITER_IDX)))
FILE_2ND_PART=$(echo "${NAME}" | awk -F$SRC_FIRST_DELIMITER -v OFS=$SRC_FIRST_DELIMITER '{ print substr($0, index($0,$'$((SRC_FORMAT_DELIMITER_IDX+1))')) }')
FILE_3ND_PART=$(echo "${FILE_2ND_PART}" | awk -F$SRC_SECOND_DELIMITER -v OFS=$SRC_SECOND_DELIMITER '{ print substr($0, index($0,$2)) }')

echo "${FILE_1ST_PART}${SRC_FIRST_DELIMITER}${DATE}${SRC_SECOND_DELIMITER}${FILE_3ND_PART}"
Nir
  • 2,497
  • 9
  • 42
  • 71
  • You should copy/paste that into http://shellcheck.net and fix the issues it tells you about. See also [how-do-i-use-shell-variables-in-an-awk-script](https://stackoverflow.com/questions/19075671/how-do-i-use-shell-variables-in-an-awk-script) and fix how you're doing that too. – Ed Morton Sep 12 '21 at 18:35