a="123|xyz|datetime|count"
oifs="$IFS" # save default input field delimiter
IFS="|" # change it
val=( $a ) # parse input to the array using $IFS
IFS=$oifs # return $IFS, if need use default ...
echo "${val[0]}"
echo "${val[1]}"
val[0]="some"
val[2]="3th"
res=${val[@]} # all values from array
res=${res// /|} # convert all spaces => | (values can't include spaces in this case)
echo "res:$res"
Not an efficient or direct way to do so, try a loop and the read statement. You can use the special IFS variable to control what it splits fields on. A loop would even be able to handle multiple lines.
while IFS="|" read COL1 COL2 COL3 COL4
do
...
done < inputfile
To update, you could do something like
while IFS="|" read COL1 COL2 COL3 COL4
do
# Alter column 4 on every line
COL4="newvalue"
echo "${COL1}|${COL2}|${COL3}|${COL4}"
done < inputfile > outputfile
# Overwrite input with output
cat outputfile > inputfile
# delete outputfile
rm outputfile
---------- Post updated at 11:56 AM ---------- Previous update was at 11:51 AM ----------
Assuming the file only contains a single line, you can read and update it without the loop and without the temporary file: