1

I recently had a question that was answered but I was writing in shell with cygwin. I found out I need to make this krn Shell compatible for MKS Toolkit.

The sed command does not like this and I am looking for a solution. the file is such that anything ending with -0 needs a date change with user input.

 137611B             0000000130220419    000013012632286-0003-0
 137618C             0000000120220420    000012012635623-0003-0
 141414-001B         0000000600220421    000060012629222-0003-1
 141608A             0000000010220422    000001012634368-0003-1
 146223C             0000000030220420    000003012626555-0003-1
 146327A             0000000020220422    000002012633825-0003-1
 137149D             0000000045220419    000004512632587-0003-0
 137050C             0000000018220419    000001812632410-0003-0
 137147A             0000000045220419    000004512632487-0003-0
 137233B             0000000144220421    000014412630711-0003-1

[[ $user_date = $(date -d "$user_date" +'+%y%m%d') ]]
for i in "$user_date"
do
sed -E '/-0$/{s/ ([^ ]{1,10})[^ ]* / \1'"$user_date"' /2}' ../*.txt > ../neworder.txt
done
Dudi Boy
  • 4,551
  • 1
  • 15
  • 30
fletching
  • 31
  • 4
  • This seems to have very little to do with the korn shell itself. What's the specific problem with the `sed` call? (Please [edit] your question to clarify); thank you! – Jeff Schaller May 25 '22 at 20:15
  • When you say date change with user input, do you mean user manually enters different dates for different lines? Or to first choose a replacement date, then replace all lines ending in `-0` with this date? – dan May 26 '22 at 04:54
  • User will enter the date in a pop up command window. i will post more of the script as it seems needed to get the information. – fletching May 26 '22 at 13:32

1 Answers1

0

suggesting single line awk :

awk '/-0\s*$/{$2=gensub("[0-9]{6}$",d,1,$2)}{printf("%-12s%s %s\n",$1,$2,$3)}' d="$(date '+%y%m%d')" *.txt
Dudi Boy
  • 4,551
  • 1
  • 15
  • 30
  • I am getting an error that says awk: lone 0 (NR=3): variable "gensub" cannot be used as a function. I am on a windows XP machine with older MKS version so it might be an issue. – fletching Jun 03 '22 at 16:02
  • yes, `gensub()` function is in `gawk` a gnu version of `awk`. – Dudi Boy Jun 03 '22 at 17:22
  • suggesting to replace `$2=gensub("[0-9]{6}$",d,$2)` with `gsub("[0-9]{6}$",d,$2)` – Dudi Boy Jun 03 '22 at 17:25
  • I tried that and a couple variations but I am getting wrong number of arguments to function "gsub" – fletching Jun 03 '22 at 18:42
  • Added awk -f to it and it goes through but it is telling me it cannot find the path specified. I will do some more research. – fletching Jun 03 '22 at 20:54