Awk usage Summary (2) Multiple delimiters are specified when split () is used, and multiple delimiters are specified when-F is used.

Source: Internet
Author: User

First, you can set multiple separators for split.

Example 1.

Output the following text and extract the two numbers at the end of each line. For example, extract from the first line and 8 from the second line.

ERROR20121211 11:35:00[AppWorker] __LSE vec_txt_size:185 vec_pic_size:5 ERROR20121211 11:35:00[AppWorker] __LSE vec_txt_size:8 vec_pic_size:1ERROR20121211 11:35:00[AppWorker] __LSE vec_txt_size:46 vec_pic_size:20 ERROR20121211 11:35:00[AppWorker] __LSE vec_txt_size:0 vec_pic_size:0

The delimiter can be set to a colon or space.

awk -F"\t" '{split($0,array,/[: ]/);for(i=1;i<=length(array);i++)print array[i]}'   $INPUTFILE 

Example 2.

Arglst = "5p12p89"

Delimiter set to P or P

split( ArgLst, Arr, /[Pp]/)

After execution: arr [1] = 5, arr [2] = 12, arr [3] = 89

Second,-F can also set multiple delimiters. In general, awk-F' \ T' is often used to represent the delimiters, such
awk -F '\t' '{print $1}' file1.txt

The processing program file1.txt is separated by a tab (\ t), and the first column is printed.

Now let's assume that you want to process a piece of text. Each line has multiple separators. For example, we need to extract the number 244 after the PIC in the following line of text and the number 246 After the TXT,
20130304 16:50:00 [Normal predict] word: mobile phone pic: 244 TXT: 246
If you can specify a separator that can be either a space or a colon, the processing will become simple. You can use a regular expression to specify multiple separators in the format of-F' [space:] + 'as follows
awk -F'[ :]+' '{print $NF"\t"$(NF-2)}'  file1.txt

The output result is

244 246

Example 3: Similarly, if you want to specify (and, as the separator, you can write

awk -F '[(,]' '{print $2"\t"$3}' 

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.