2.10 Temporary file naming and random numbers

Source: Internet
Author: User

When you write a shell script, you often need to store temporary data that is best suited for storing temporary data in/tmp (the contents of that directory are emptied after the system restarts).

1) Create temporary files

$ Filename= ' mktemp '

$ echo $filename

/tmp/tmp.8xvhkjf5fh

The above code creates a temporary file and prints out the name of the file stored in the $filename.

2) Create temp directory

$ Dirname= ' mktemp-d '

$ echo $dirname

Tmp. Ni8xzw7vrx

The above code creates a temporary directory and prints out the directory names stored in the $dirname

3) If you want to create a file name only, and do not want the actual files or directories,

$ Tmpfile= ' mktemp-u '

$ echo $tmpfile

/tmp/tmp. rsgmilrpct

File name is stored in $tmpfile, but the corresponding file is not created

4) Create a temporary file name based on the template

$ mktemp test.xxx

Text.2c

How it works: mktemp it generates a temporary file and returns its file name (if a directory is created, returns a directory name), and if a custom template is provided, x is replaced by random characters (numbers or letters), noting that the mktemp normally works by ensuring that the template has at least 3 x.

2.11 Splitting files and data

1) Working principle

Suppose there is a file data.file, its size is 100KB, it is divided into several files of 10KB size, as follows

$ split-b 10k Data.file

$ ls

Data.file xaa xab xac xad xae xaf xag xah Xai Xaj

These files are named in the form Xab,xac,xad, which indicates that they all have a letter suffix that can be used with the-d parameter to specify the suffix length as a numeric suffix, and-a, such as

$ split-b 10k data.file-d-a 4

In addition to the K (KB) suffix, there are M (MB), G (GB), C (Byte), W (word).

2) Supplemental Content

A. Specify the filename prefix for the split file

The previously separated files have a filename prefix x, which uses their own custom prefixes, in the following format

$ split [Command_args] PREFIX

Such as

$ split-b 10k data.file-d-a 4 split_file

If you do not like the size of the block, but you need to split the file according to the number of rows, you can use-l no_of_lines, as

$ split-l Ten Data.file

#分割多个文件, each file contains 10 rows

B, Csplit, which splits the log file according to the specified conditions and string matching options. It is a variant of the split tool, split can only split files based on data size or number of rows, and csplit can be segmented based on the character of the text itself-whether there is a word or text content that can be used as a condition for splitting text.

For example, splitting a log containing multiple servers into multiple log files

$ csplit server.log/server/-n 2-s {*}-F Server-b "%02d.log"; RM Server00.log

$ ls

Server01.log Server02.log Server03.log Server.log

/server/: Used to match a row, the split process starts here

/[regex]/: Represents a text style, including matching rows that contain "SERVER" from the current line (the first row) until (but not included).

{*}: Indicates that the split is repeated according to the match, until the end of the file, and the number of split executions can be specified in the form {integer}.

-S mission order into silent mode, no additional information printed

-n Specifies the number of digits after the split filename suffix, such as 01,02,03

-f Specifies the filename prefix after splitting

-b Specifies the suffix format. For example,%02d.log, a parameter format similar to printf in C.

Because the first file after the split is nothing (the matching word is in the first line of the file), we removed the server00.log.

2.10 Temporary file naming and random numbers

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.