Keywords in the shell intercept file

Source: Internet
Author: User
Tags first string truncated

1.Linux Shell intercepts the first 8 bits of a character variable


There are several ways to implement this:

    1. Expr substr "$a" 1 8
    2. echo $a |awk ' {print substr (, 1,8)} '
    3. Echo $a |cut-c1-8
    4. echo $
    5. Expr $a: ' \ (. \ \).
    6. echo $a |dd Bs=1 count=8 2>/dev/null

2. Intercept by the specified string


(1) The first method:

A string after the last string is truncated from left to right
${varible##*string}
A string after the first string is truncated from left to right
${varible#*string}
A string after the last string is intercepted from right to left
${varible%%string*}
A string after the first string is intercepted from right to left
${varible%string*}
"*" is just a wildcard can not

Take a look at the following example:

$ myvar=foodforthought.jpg$ echo ${myvar##*fo}rthought.jpg$ Echo ${myvar#*fo}odforthought.jpg


(2) The second method:

${VARIBLE:N1:N2}: Intercepts the variable varible the N2 character starting from N1, forming a substring. You can select specific substrings by using a different form of variable extension, depending on the specific character offset and length. Try entering the following line in bash:

$ exclaim=cowabunga$ echo ${exclaim:0:3}cow$ Echo ${exclaim:3:7}abunga


This form of string truncation is simple, with only a colon separated to specify the starting character and substring length.

3. Split according to the specified requirements:

3.1 For example, get a list of suffix ls

CD $FROM _dir

For i in ' ls E2c_all_*|awk ' {print '} '
Do

Echo $i
S_filename= "
echo "define param: $s _filename"

3.2 Getting row data
Cat $i | While Read line
Do
Va_line=${line}
headfix= ' expr substr ' $va _line "1 1 '

if [$headfix = "#"];then

3.3 Intercept the keyword, the field before the number, two times the field after the # number is intercepted
S_filename= ' echo $va _line|awk-f ', ' {print $} ' |awk-f ' # ' {print $} '
Content= ' echo $va _line|awk-f ', ' {print $} '
echo "FileName: $s _filename"
Touch $TO _dir$s_filename
echo $content >> $TO _dir$s_filename
Else
echo $va _line >> $TO _dir$s_filename
Fi
Done

3.4 After the append processing is finished, do the backup processing

MV ${i} ${back_dir}${i}

Done

Keywords in the shell intercept file

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.