Several named sed,awk,sort,uniq,cut for Shell text processing

Source: Internet
Author: User


Text ProcessingCatCommand

1) stitching files: Vertical stitching, not horizontal stitching

[Email protected]:~# cat list List2

Line2

Line3

Line4

Line5

Line5

Line6

Line7

Total 32K

lrwxrwxrwx 1 root root 4 Jan backup-/data/backup

-rw-r--r--1 root root 1 13:18 list

-rw-r--r--1 root root 0 Apr 1 13:29 list2

-rwxr-xr-x 1 root root 1 09:24 test.sh

Drwxr-xr-x 4 root root 4.0K Apr 1 13:16test_shell

Drwxr-xr-x 2 root root 4.0K Mar 09:42 tmp

Drwxr-xr-x 6 root root 4.0K Jul 2016tools

[Email protected]:~# LS-LH | Cat-list #这里的-Marking standard input

Total 36K

lrwxrwxrwx 1 root root 4 Jan backup-/data/backup

-rw-r--r--1 root root 1 13:18 list

-rw-r--r--1 root root 1 13:29 list2

Drwxr-xr-x root root 4.0K Mar root

-R--------1 root root 1.7K Mar 10:06 Solar

Drwxr-xr-x 3 root root 4.0K Dec 1 10:07testgit

-rwxr-xr-x 1 root root 1 09:24 test.sh

Drwxr-xr-x 4 root root 4.0K Apr 1 13:16test_shell

Drwxr-xr-x 2 root root 4.0K Mar 09:42 tmp

Drwxr-xr-x 6 root root 4.0K Jul 2016tools

Line2

Line3

Line4

Line5

Line5

Line6

2) formatted output

Output line number

[Email protected]:~# cat-n List

1 line2

2 Line3

3 line4

4 Line5

5 Line5

6 Line6

Compressed empty cells

[Email protected]:~# cat-s List

Line2

Line3

Line4

Line5

Line5

Line6

Line7

Paste

Merge files by column: Portrait stitching

[Email protected]:~# paste Employe.txt bonus.txt | awk ' {print $1,$2,$3,$5} ' | TR ' [A-z] ' [A-z] ' |s

Ort-s-K2

Ashok Sharma $1,250

Jason Smith $5,000

John Doe

Sanjay Gupta $3,000

[Email protected]:~# cat Employe.txt

Jason Smith

John Doe

Sanjay Gupta

Ashok Sharma

[Email protected]:~# Cat B

CAT:B: No such file or directory

[Email protected]:~# cat Bonus.txt

100 $5,000

200

300 $3,000

400 $1,250

Cut

Processing a line of text in a column format

-D to split columns with what as separators

-F Specifies which columns to display

sorting, de-weight

Sort

-N Sort by number sequence

-R Reverse Sort

-K specifies that a column

-D Sort by dictionary sequence

Tac

Prints the output in reverse order

[Email protected]:~# Cat List

Line2 200

Line3 100

Line4 100

Line5 700

Line5 800

Line6 50

Line7 350

[Email protected]:~# Cat List | TAC-

Line7 350

Line6 50

Line5 800

Line5 700

Line4 100

Line3 100

Line2 200

Uniq

Uniq will remove duplicate rows, but only for inputs that have already been sequenced, or for files that have already been sequenced.

-U displays only those rows that are unique

-C count repeated rows occurrences

-D Find the contents of duplicate rows


TrCommand

TR is a small, beautiful tool for building a single line of commands. TR can only receive standard input as a command parameter. TR can be used to convert, delete, compress files.

Replace

[Email protected]:~# echo "123 abcdefg:aaa:" | TR ":" "<"

123 ABCD efg<aaa<


Delete

[Email protected]:~# echo "123 abcdefg:aaa:" | Tr-d ": '

> ^c

[Email protected]:~# echo "123 abcdefg:aaa:" | Tr-d ":"

123 ABCD Efgaaa


-C defines a collection that no longer deletes all the contents of this collection.

[Email protected]:~# echo "123 abcdefg:aaa:" | Tr-d-C ' A-z \ n '

Abcdefgaaa

[Email protected]:~# echo "123 abcdefg:aaa:" | Tr ' A-Z ' A-Z

123 ABCD EFG:AAA:

special usage, the ability to encrypt characters by the substitution of characters .

Text Matching SearchGrep

[Email protected]:~# grep "Hello" test.sh

123aab456 1232 Hello

ASSBBB hello112332 asss

[Email protected]:~# grep "hello$" test.sh--color=auto

123aab456 1232 Hello

# match multiple match criteria

[Email protected]:~# grep-e "hello$"-E "asss"--color=auto./test.sh

123aab456 1232 Hello

ASSBBB hello112332 asss

Specify the file or path to search

grep "text"./-r–include *. (CPP,C)


Sed

Can replace the found string with the new character we specified

General use : do not modify the original file contents

[Email protected]:~# sed ' s/line2/tttt/g ' list

TTTT 200

Line3 100

Line4 100

Line5 700

Line5 800

Line6 50

Line7 350

[Email protected]:~# Cat List

Line2 200

Line3 100

Line4 100

Line5 700

Line5 800

Line6 50

Line7 350

match with regular expression & use a string that is already histograms as a variable.

here the &, it is marked by the match has been matched to the character A, specifically which characters are matched to, that is, we use parentheses, enclosed parts.

[Email protected]:~# Cat List

Line2 200

Line3 100

Line4 100

Line5 700

Line5 800

Line6 50

Line7 350

[Email protected]:~# sed ' s/\w\+2 \ (\w\+\)/[--&--]/g ' list

[--line2 200--]

Line3 100

Line4 100

Line5 700

Line5 800

Line6 50

Line7 350

If there is more than one word () to match the string then we can use the number designator to indicate exactly which match

[Email protected]:~# sed ' s/\ (\w\+\) 2\ (\w\+\)/[--\1--] [\2]/g ' list

[--line--] [200]

Line3 100

Line4 100

Line5 700

Line5 800

Line6 50

Line7 350

multiple sed combinations,

[Email protected]:~# sed ' s/\ (\w\+\) 2\ (\w\+\)/[--\1--] [\2]/g ' list | sed ' s/line7/tttt/g '

[--line--] [200]

Line3 100

Line4 100

Line5 700

Line5 800

Line6 50

TTTT 350


Awk

Awk is so powerful that I'm not listed here for space reasons, and I can see an article and a comment that I sent earlier.

http://atong.blog.51cto.com/2393905/1343553


There is also the time to look at the Awk programming language book, which can basically jump through the examples listed in the book, more than 200 pages of books, but can be very quick to see the past. I'm sure you can see it in 1 hours.



This article is from the "Start from scratch" blog, be sure to keep this source http://atong.blog.51cto.com/2393905/1913328

Several named sed,awk,sort,uniq,cut for Shell text processing

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.