How do I use the split
function to split by "\."?
For example, first consider splitting by :
:
echo "03:26:12" | awk '{split($0,a,":"); print a[3] a[2] a[1]}'
Which produces this output:
122603
But if the incoming string is instead:
echo "03\.26\.12" | awk '{split($0,a,???); print a[3] a[2] a[1]}'
With desired output:
122603
What should the ???
be?
The patsplit() function splits strings into pieces in a manner similar to the way input lines are split into fields using FPAT (see Defining Fields by Content).
In bash, a string can also be divided without using $IFS variable. The 'readarray' command with -d option is used to split the string data. The -d option is applied to define the separator character in the command like $IFS. Moreover, the bash loop is used to print the string in split form.
Here is a simple awk command to split it into 3 columns. In the above awk command, we use -F option to specify input delimiter as comma, -OFS option to specify the output field delimiter, that is the delimiter to be used for output file. We issue a print statement within curly braces {} to print the columns 1, 2, 3.
echo "03\.26\.12" | awk '{split($0,a,"\\\."); print a[3] a[2] a[1]}'
This gives the same output.
You must escape both characters:
echo "03\.26\.12" | awk '{split($0,a,/\\\./); print a[3] a[2] a[1]}'
Result:
122603
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With