Am 15.02.2023 um 07:25 schrieb Albretch Mueller:
$ _L="Adams, Fred, and Ken Aizawa \"The Bounds of Cognition\""
echo "// __ \$_L: |${_L}|"
_AR=($(echo "${_L}" | awk -F'\"' '{for (i=1; i<=NF; i++) print $i}' )) _AR_L=${#_AR[@]}
echo "// __ \$_AR_L: |${_AR_L}|"
for(( _IX=0; _IX<${_AR_L}; _IX++ )); do
echo "// __ [$_IX/$_AR_L): |${_AR[$_IX]}|"
done
what awk are you using? gnu awk works fine. see:
$ echo "Adams, Fred, and Ken Aizawa \"The Bounds of Cognition\"" | awk
-F'\"' '{for (i=1; i<=NF; i++) print $i;}'
Adams, Fred, and Ken Aizawa
The Bounds of Cognition
$ awk --version
Start reading here:
http://mywiki.wooledge.org/BashFAQ/005
On 2/15/23, DdB <debianlist@potentially-spam.de-bruyn.de> wrote:
$ echo "Adams, Fred, and Ken Aizawa \"The Bounds of Cognition\"" | awk -F'\"' '{for (i=1; i<=NF; i++) print $i;}'
Adams, Fred, and Ken Aizawa
The Bounds of Cognition
yes and this also works:
_L="Adams, Fred, and Ken Aizawa \"The Bounds of Cognition\""
echo "${_L}" | awk -F'\"' '{for (i=1; i<=NF; i++) print $i;}'
Adams, Fred, and Ken Aizawa
The Bounds of Cognition
but I wasn't able to write the output into an array
If you want to read FIELDS of a SINGLE LINE as array elements, use
read -ra:
read -ra myarray <<< "$one_line"
On 2/15/23, Greg Wooledge <greg@wooledge.org> wrote:
If you want to read FIELDS of a SINGLE LINE as array elements, use
read -ra:
read -ra myarray <<< "$one_line"
It didn't work. I tried different options. I am getting: "bash: read:
... : not a valid identifier"
_PTH="83847547|2|dli.ernet.449320/449320-Seduction Of The Innocent_text.pdf"
echo "// __ \$_PTH: \"${_PTH}\""
# read -ra -d "\\|" _PTH_AR <<< "${_PTH}"
# read -ra -d "\|" _PTH_AR <<< "${_PTH}"
# read -ra -d "|" _PTH_AR <<< "${_PTH}"
https://mywiki.wooledge.org/BashPitfalls#pf47
https://mywiki.wooledge.org/BashPitfalls#pf47
what I am trying to do is split a string using as delimiter a pipe
I used to do that with awk,
How do you split a string using as delimiter a pipe these days
without using a bloody hack?
However this would rightly split that line based on the pipe delimiter:
$ echo "${_PTH}" | awk -F '|' '{for (i=1; i<=NF; i++) print $i;}'
83847547
2
dli.ernet.449320/449320-Seduction Of The Innocent_text.pdf
There should be a sane way ;-) to feed those three lines into a bash array.
On 2/15/23, Greg Wooledge <greg@wooledge.org> wrote:
The reason why I use pipes as field delimiter is because it is an
excellent meta character when you are working with filesystems. Pipes
would not accepted for files or directory names for good reasons,
anyway.
On Mon, 20 Feb 2023, Albretch Mueller wrote:
On 2/15/23, Greg Wooledge <greg@wooledge.org> wrote:
The reason why I use pipes as field delimiter is because it is an
excellent meta character when you are working with filesystems. Pipes
would not accepted for files or directory names for good reasons,
anyway.
tim@einstein(7):~ (none)$ touch 'i|use|pipes'
tim@einstein(7):~ (none)$ ls -l i*use*
-rw-rw-r-- 1 tim tim 0 Feb 21 05:14 'i|use|pipes'
tim@einstein(7):~ (none)$ rm i\|use\|pipes
tim@einstein(7):~ (none)$
AFAIR only / and nul are prohibited in file names.
I have a funny feeling Albretch might be using Microsoft file systems
(FAT, NTFS) for a large chunk of his system. Those have a much larger
set of restricted characters.
On 2/21/23, Greg Wooledge <greg@wooledge.org> wrote:
I have a funny feeling Albretch might be using Microsoft file systems
(FAT, NTFS) for a large chunk of his system. Those have a much larger
set of restricted characters.
Certainly not FAT32 and definitely not FAT, but at work (I work as a
Math teacher and most schools use Microsoft) I have had to use WSL and
NTFS. I always thought that FSs used length-defined raster data
structures in order to avoid messing with points and such things.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 297 |
Nodes: | 16 (2 / 14) |
Uptime: | 31:53:21 |
Calls: | 6,669 |
Calls today: | 1 |
Files: | 12,216 |
Messages: | 5,338,219 |