The usage of the split function in awk in the Linux _linux shell

Source: Internet
Author: User
Tags set time

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.