Shell File Operations)

Source: Internet
Author: User
Http://blog.chinaunix.net/u/29289/showart_407859.html
A read operation can assign values to multiple variables at a time, but only one row can be read at a time. For example:

#! /Bin/bash ifs =: Read aa bb cc </etc/passwd echo $ AA $ BB $ CC (note: by default, shell cannot split the strings separated by colons in/etc/passwd. You can set IFS to achieve this. For details, see the bottom of this article) the result is that the first two variables are assigned the correct value, and the last variable is assigned all the values left in the first row of the file. (Because only one row can be read at a time.) In reality, if a file stores such a phone book: passer-by A 13900000000 protagonist a 23320000 mysterious person 12x0x0x0000 narration 85600000 ...... If we want to write a script to query the phone book, we usually write :#! /Bin/bash while read name num do if [$ name = $1] Then ECHO $ num fi done when we use the name of the person we want to query as a parameter to start the script, you can see his number and call him ...... (I will sort out the while and other process control statements later) extract the header-X file in the row. // extract the first X rows of the tail-X file. // extract the X rows of the last x lines of the SED-n xp file. // extract the X rows of the file. Note: here-N is-N, which does not represent anything else. Extract the cut-Fx-dy file in the column. // use y as the separator to extract the awk-fy '{print $ x}' file in column X. // use y as the separator, column x extraction Note: awk can extract multiple columns and use commas to separate them in the command, for example, awk-F: '{print $3, $5} '/etc/passwd will extract the 3rd and 5th columns in the file to sort the sort-ty + Xn file // use y as the separator, use the 1st + x column as the benchmark sorting. Note: If this command is used, other parameters cannot be used in the same statement. If necessary, the command can only be sort once in the pipeline, for example: sort + 4n/etc/fstab | other sort-r parameters: N-to-number sorting D-to-Letter Sorting M-to-month sorting (such as Jan, Feb .... (such as DEC) r reverse $ ifs many times we do not need to specify the delimiter, because $ ifs contains spaces, \ t and line breaks by default. We only need to specify other separators. For example, $ path and/etc/passwd are separated by colons. In this case, we need to specify $ ifs.

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.