如何找到&用ruby中的字符串替换模式?

时间:2016-04-19 06:26:22

标签: ruby regex shell

我的档案是:     

    [root@test etc]# cat nrpe.cfg
command[check_users]=/usr/local/nagios/libexec/check_users -w 30 -c 35 command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20 command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda1 command[check_hda]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sdb command[check_procs]=/usr/local/nagios/libexec/check_procs -w 200 -c 250
我想在ruby中用 / dev / xvda1 替换 / dev / sdb

2 个答案:

答案 0 :(得分:0)

您可以使用gsubsub,具体取决于某一行中的出现次数:

public class connect4 extends AsyncTask<String, Void, String> {
// public static final String product_id = "product_id";
public static final String RECEIPT_INDEX = "RECEIPT_INDEX";
View view;
Activity activity;
ProductAdapter1 Adapter;
public List<ContactObject> receipt = new ArrayList<>();


Bitmap bitmap;

public connect4(Activity activity, View v, ProductAdapter1 A) {
    this.activity = activity;
    view = v;
    Adapter = A;
}

String convertStreamToString(InputStream is) {
    try {
        return new java.util.Scanner(is).useDelimiter("\\A").next();
    } catch (java.util.NoSuchElementException e) {
        return "";
    }
}
protected String doInBackground(String... arg0) {
    String ipAddress = "http://10.207.140.22/apexStore2/";
    try {
        URL url = new URL(ipAddress +"receipt.php");
        String urlParameters =
                URLEncoder.encode("user_id", "UTF-8") + "=" + 
  URLEncoder.encode(arg0[0], "UTF-8") + "&" +
                        URLEncoder.encode("shipping_name", "UTF-8") + "=" + 
  URLEncoder.encode("???", "UTF-8") + "&" +
                        URLEncoder.encode("shipping_address", "UTF-8") + "="
 + URLEncoder.encode("???", "UTF-8")+ "&" +
                        URLEncoder.encode("shipping_contact", "UTF-8") + "=" 
 + URLEncoder.encode("???", "UTF-8")+ "&" +
                        URLEncoder.encode("shipping_email", "UTF-8") + "=" + 
 URLEncoder.encode("???", "UTF-8")+ "&" +
                        URLEncoder.encode("order_date", "UTF-8") + "=" + 
 URLEncoder.encode("???", "UTF-8")+ "&" +
                        URLEncoder.encode("trx_id", "UTF-8") + "=" + 
 URLEncoder.encode("???", "UTF-8")+ "&" +
                        URLEncoder.encode("tracking_num", "UTF-8") + "=" +
 URLEncoder.encode("???", "UTF-8")+ "&" +
                        URLEncoder.encode("quantity", "UTF-8") + "=" + 
 URLEncoder.encode("???", "UTF-8")+ "&" +
                        URLEncoder.encode("product_id", "UTF-8") + "=" + 
  URLEncoder.encode("???", "UTF-8")+ "&" +
                        URLEncoder.encode("product_title", "UTF-8") + "=" + 
 URLEncoder.encode("???", "UTF-8")+ "&" +
                        URLEncoder.encode("product_price", "UTF-8") + "=" + 
  URLEncoder.encode("???", "UTF-8")+ "&" +
                        URLEncoder.encode("product_img1", "UTF-8") + "=" + 
  URLEncoder.encode("???", "UTF-8");

        HttpURLConnection connection = (HttpURLConnection) 
   url.openConnection();

        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");

        connection.setRequestProperty("Content-Length", "" +
                Integer.toString(urlParameters.getBytes().length));
        connection.setRequestProperty("Content-Language", "en-US");

        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        //Send request
        DataOutputStream wr = new DataOutputStream (
                connection.getOutputStream ());
        wr.writeBytes (urlParameters);
        wr.flush ();
        wr.close ();

        //Get Response
        InputStream is = connection.getInputStream();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        StringBuffer response = new StringBuffer();
        while((line = rd.readLine()) != null) {
            response.append(line);
            response.append('\r');
        }
        rd.close();
        //System.out.println(response.toString());

        JSONObject mainObject = new JSONObject(response.toString());
        JSONArray uniObject = mainObject.getJSONArray("result");
        for(int i = 0; i < uniObject.length(); i++) {
            ContactObject co = new ContactObject();
            JSONObject rowObject = uniObject.getJSONObject(i);
            //EventObject co = new EventObject();
            //co.user_id = rowObject.getString("user_id");
            co.name = rowObject.getString("shipping_name");
            co.address = rowObject.getString("shipping_address");
            co.contact = rowObject.getString("shipping_contact");
            co.email = rowObject.getString("shipping_email");
            co.orderdate = rowObject.getString("order_date");
            co.transaction_id = rowObject.getString("trx_id");
            co.tracking_id = rowObject.getString("tracking_num");
            co.quantity = rowObject.getString("quantity");
            co.product_id = rowObject.getString("product_id");
            co.product_title = rowObject.getString("product_title");
            co.product_price = rowObject.getString("product_price");
            co.img1 = ipAddress +"img/products/" + 
rowObject.getString("product_img1");
            //mContentItems.add(co);
            System.out.println("hi" +co.transaction_id);
            receipt.add(new ContactObject(co.name, co.address, co.contact, 
co.email, co.orderdate, co.transaction_id, co.tracking_id, co.quantity, 
co.product_id, co.product_title, co.product_price, co.img1));
            //         userLogin(co.product_id);
        }

        //To further break down JSON
        //JSONObject oneObject = mainObject.getJSONObject("1");
        //String id = oneObject.getJSONObject("id");
        try{

        }
        finally{
            connection.disconnect();
        }
    } catch (Exception e){
        System.out.println(e.toString());
    }
    return "";
}

protected void onPreExecute(){

}

@Override
protected void onPostExecute(String result){
    Adapter.receipt = receipt;
    Adapter.notifyDataSetChanged();
}



private class LoadImage extends AsyncTask<String, String, Bitmap> {
    ImageView img;
    public LoadImage(ImageView img){
        this.img = img;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }
    protected Bitmap doInBackground(String... args) {
        try {
            bitmap = BitmapFactory.decodeStream((InputStream) new 
URL(args[0]).getContent());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    protected void onPostExecute(Bitmap image) {

        if(image != null){
            img.setImageBitmap(image);
        }else{
        }
    }
}
}

由于它在文件中,您可以迭代为:

<?php
 include ('classes/functions.php');

 if(isset($_POST['user_id'])){
$user_id = $_POST['user_id'];
$check_receipt = "select  si.shipping_name,
    si.shipping_address,
    si.shipping_contact,
    si.shipping_email,
    o.order_date,
    o.trx_id,
    o.tracking_num,
    o.quantity,
    o.product_id,
    p.product_title,
    p.product_price,
    p.product_img1
 from shipping_infos si
 inner join orders o
 on si.user_id = o.user_id inner join products p on p.product_id =    
 o.product_id   
 where si.user_id='".$user_id."';";

        $run_receipt_checking = mysqli_query($con, $check_receipt);
        $result = array();
    while($row = mysqli_fetch_array($run_receipt_checking)){
    array_push($result,
    array(
          'shipping_name'=>$row[0],
          'shipping_address'=>$row[1],
          'shipping_contact'=>$row[2],
          'shipping_email'=>$row[3],
          'order_date'=>$row[4],
          'trx_id'=>$row[5],
          'tracking_num'=>$row[6],  
          'quantity'=>$row[7],
          'product_id'=>$row[8],
          'product_title'=>$row[9],           
          'product_price'=>$row[10],
          'product_img1'=>$row[11]            

));
}
echo json_encode(array("result"=>$result));
 }
?>

答案 1 :(得分:0)

首先让我们将文本写入文件:

str =<<BITTER_END
command[check_users]=/usr/local/nagios/libexec/check_users -w 30 -c 35
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda1
command[check_hda]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sdb
command[check_procs]=/usr/local/nagios/libexec/check_procs -w 200 -c 250
BITTER_END

Fname_in  = "in"
Fname_out = "out"

File.write(Fname_in, str)
  #=> 390

现在将该文件读入字符串,修改字符串并将其写入输出文件:

File.write(Fname_out, File.read(Fname_in).gsub("/dev/sdb", "/dev/xvda1"))
  #=> 392

让我们确认它有效:

puts File.read(Fname_out)
command[check_users]=/usr/local/nagios/libexec/check_users -w 30 -c 35
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda1
command[check_hda]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/xvda1
command[check_procs]=/usr/local/nagios/libexec/check_procs -w 200 -c 250