0

This is working, we can see all spaces in path (between "-" and "MOYEN").

[root@xx ~]# echo "${FILE_LIST_PATH[7]}"
files_encryption/keys/files/xx/xx - Enceinte -  MOYEN 1 & 2.DWG     23172

When i pipe awk, additionnal space is removed (between "-" and "MOYEN")

[root@xx ~]# echo "${FILE_LIST_PATH[7]}" | awk '{$NF=""}1'
files_encryption/keys/files/xx/xx - Enceinte - MOYEN 1 & 2.DWG

When i attribute variable, same

[root@xx ~]# FILE_PATH=`echo "${FILE_LIST_PATH[7]}"`
[root@xx ~]# echo $FILE_PATH
files_encryption/keys/files/xx/xx - Enceinte - MOYEN 1 & 2.DWG 23172

I need to avoid processes to delete additionnal spaces. Thanks.

Alex Lum
  • 175
  • 2
  • 12
  • Yes, awk does that. Have you considered using a different tool? – Charles Duffy Mar 22 '23 at 16:39
  • 2
    BTW, `echo $FILE_PATH` deletes spaces as a bash problem; make it `echo "$FILE_PATH"` with the quotes and you fix the bash problem and leave only the awk problem. – Charles Duffy Mar 22 '23 at 16:40
  • 1
    The problem isn't the assignment to `FILE_PATH` (which can be simplified to `FILE_PATH=${FILE_LIST_PATH[7]}`), but the failure to quote the expansion of `$FILE_PATH`. – chepner Mar 22 '23 at 16:41
  • @chepner, the OP _also_ has a problem in that assigning to a field in awk prevents the spaces between fields from being preserved, or am I misremembering that? Because if my memory is at all correct there are two separate problems happening concurrently here. – Charles Duffy Mar 22 '23 at 16:41
  • 1
    I was ignoring `awk` as you had already mentioned that. I don't know the exact cause; I think modifying a field means `awk` has to reconstruct `$0` by joining the individual fields back together, and the original spacing is no longer available. The recomputation of `$0` is explicitly mentioned in the POSIX spec in the case of a *new* field beyond `$NF` being created. I assume it needs to be done for any modified field as well. – chepner Mar 22 '23 at 16:50
  • 1
    Use `sub` like so `awk '{sub(/[ [:digit:]]+$/, "", $0)}1'` if it's always digits after some spaces. – Andre Wildberg Mar 22 '23 at 16:50
  • 1
    Could also use bash-native regular expressions and avoid any need for awk at all. `re='^(.*[^ ]) +[[:digit:]]+ *$'; if [[ $file_path =~ $re ]]; then echo "Name is ${BASH_REMATCH[1]}" >&2; else echo "Unable to match path" >&2; fi`, or such (note lower-case variable names because all-caps ones are used by the shell and other POSIX tools; this means the rest of the script should rename `FILE_PATH` to `file_path`; see https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html) – Charles Duffy Mar 22 '23 at 16:54

0 Answers0