Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement text processing with Shell script

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how Shell scripts implement text processing. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

4.1 sort text

4.1.1 sorting of Lin

When no command line option is provided, the entire record is sorted according to the order defined by the current locale.

In traditional C locale, that is, the ASCII order.

4.1.2 sort by field

The-k option is followed by a field number, or a pair of numbers.

Each number can be followed by a period character position, or modifier letter.

If you specify only one field number, the sort key value starts at the beginning of the field

Continue until the end of the record (not the end of the field).

If you are given a pair of field numbers separated by commas, the sort key value will start at the beginning of the first field value

Ends at the end of the second field value. Use a period to indicate the character position.

-k2.4, 5.6 refers to the comparison from the fourth character of the second field to the sixth character of the fifth field.

$sort-t:-K1Power1 / etc/passwd sorted by user name

Bin:x:1:1:bin:/bin:/sbin/nologin

Chico:x:12501:1000:Chico Marx:/home/chico:/bin/bash

Harpo:x:12502:1000:Harpo Marx:/home/harpo:/bin/ksh

...

$sort-t:-k3mai 3nr / etc/passwd introspect UID ranking

Or-k3nrpd3 or-k3pje 3-n-r is fine.

4.1.3 sorting of text blocks

Sometimes you need to sort the data formed by the combination of multiple rows of records. Take the address list as an example:

$cat my-friends

# SORTKEY: Schlo, Hans Jurgen

Hans Jurgen Schlo

Unter den Linden 78

Dmuri 10117 Berlin

Germany

# SORTKEY: Jones, Adrian

...

The trick is to use awk to identify paragraph intervals and temporarily replace branches with an unused character within each address.

The line that sort sees looks like this:

# SORTKEY: Schlo, Hans Jurgen ^ ZHans Jurgen Schlo ^ ZUnter den Linden 78 ^ Z...

Cat my-friends | read address file

Awk-v RS= "'{gsub ("\ n "," ^ Z "); print}'| the translation address is on a single line

Sort-f | sorts address data, ignoring case

Awk-v ORS= "\ n\ n"'{gsub ("^ Z", "\ n"); print}'| restore row structure

Grep-v'# SORTKEY' delete tag line

1. The function gsub () is a global substitution (global substitution), similar to the s/x/y/g structure under sed.

2. Record separator (Record Separator) for input data when RS variable.

Data is usually entered to be separated by line breaks, making each row a single record.

RS= "" is a special use in which records are separated by blank lines.

3. ORS is an output record separator.

Note:'{action}'is the operation for each field, while RS,ORS is the setting for the record.

4.1.5 Stability of sort

Sort is unstable.

4.2 remove duplicates

Sort-u is an elimination operation based on a matching key value, not a matching record.

There are three good options for uniq:

-c add the number of times the row is repeated before each output line.

-d is used to display only duplicate rows.

-u displays only rows that are not duplicated.

4.3 reformat paragraphs

Fwt-w 30

4.4 calculate the number of lines, words and characters

The default output of wc is an one-line report, including the number of lines, words, and bytes.

Available options:-c (number of bytes),-l (number of lines),-w (number of words).

The code is as follows:

$echo Testing one two three | wc-c

1 4 22

$wc / etc/passwd / etc/group

4.6 extract the first or last lines

Displays the first n records of each in the file list:

Head-n [file (s)]

Head-n [file (s)]

Awk 'FNR

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report