The awk function split (S,A,SEP) splits a string s into a awk array a using the delimiter Sep.
Set time = 12:34:56
Set hr = ' echo $time | awk ' {split ($0,a, ":"); Print a[1]} ' # = 12
Set sec = ' echo $time | awk ' {split ($0,a, ":"); Print A[3]} ' # = 56
# = 12 34 56
Set HMS = ' Echo $time | awk ' {split ($0,a, ":"); Print a[1], a[2], a[3]} '
——————————————————————————————————————————
Q:
Name= "76868&5676&435&43526&334&12312312&12321"
awk ' BEGIN {print split (' $name ', Filearray, ' & ')} '
Why is it 1?
and
awk ' BEGIN {print split (' 76868&5676&435&43526&334&12312312&12321 ', Filearray, ' & ')} '
Then return the correct result, should be 7, is there anyone to answer?
A:
Variable reference error, try this.
awk ' BEGIN {print split (' \ ' $name \ ' ', Filearray, ' & ')} '
awk must use single quotes and double quotes to reference system variables. The ' $sysvar ' format, but the split function also needs double quotes to set the bounds, but this double quote does not allow SH to explain, but should be left to awk to explain, so use the \ "and \" the double quotes
Use of the Split function
He awk function split (S,A,SEP) splits a string s into a awk array a using the delimiter Sep.
Set time = 12:34:56
Set hr = ' echo $time | awk ' {split ($0,a, ":"); Print a[1]} ' # = 12
Set sec = ' echo $time | awk ' {split ($0,a, ":"); Print A[3]} ' # = 56
# = 12 34 56
Set HMS = ' Echo $time | awk ' {split ($0,a, ":"); Print a[1], a[2], a[3]} '
Set HMS = ' Echo $time | awk ' {split ($0,a, ":"); For (I=1 i<=3; i++) print A[i]} '
Example one:
Cat A
A:b:c:d:e:f:g:h:i
Use awk to output the segment on both sides of the string colon
Cat a |awk-f ': ' {split ($0,arr, ': ')}end{for (i=1;i<=nf;i++) printf ("%s\n", Arr[i])} '
The output is as follows
A
C
D
E
F
G
H
I