A shell program that replaces a section of a line near a keyword in a text file: __ Development

Source: Internet
Author: User
Tags key string

A shell program that replaces a section of a line near a keyword in a text file:


#!/bin/bash file=$1 findpattern=$2 linegap=$3 replacepattern=$4 substitue=$5 Help () {echo Help:echo \$1:file\ ' s full Path \ (include filename\) echo \$2:the key string to is find echo \$3:the line count between the line where key Strin
G in and the line to is modified echo \$4:the string to be replace echo \$5:the string would take place the key string Echoinput () {echo \$1= $file echo \$2= $findpattern echo \$3= $linegap echo \$4= $replacepattern echo \$5= $substi
  Tue} If ["$" = ""];then help;
exit-1;

Fi echoinput;
if [!-F ${file}];then echo $file is not exist.
Fi #echo "awk '/${findpattern}/{print NR} ' ${file}";
n= ' awk '/' ${findpattern} ' '/{print NR} ' ${file} ';
  If ["$n" = = ""];then Echo can not find ${findpattern} in ${file};
exit-1;
fi echo Find key string $n;
  if [!-n $n];then Echo find too much lines.
exit-1;
Fi n2= ' expr $n + ${linegap} ';
Echo would modify line $n 2,line contnet is:;
awk ' nr== ' ${n2} ' {print $} ' ${file}; #echo "sed ${n2} ' s/${replacepattern}/${substitue}/g ' ${file} > tmp${file};" sed ${n2} ' s/' ${replacepattern} ' '/' ${
Substitue} "'/g ' ${file} > Tmp${file}; Diff ${file} Tmp${file}

There are also bugs: You can only run in the current directory of the text file. Not modified.
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.