我正在使用' awk'让CPU空闲(vmstat),它在Linux上运行良好。 奇怪的是,下面的命令并没有在AIX上显示任何内容。
package com.googledoc;
import com.google.api.client.auth.oauth2.Credential;
importcom.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.sheets.v4.SheetsScopes;
import com.google.api.services.sheets.v4.model.*;
import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.Sheets.Spreadsheets;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.mortbay.log.Log;
public class SheetsQuickstart {
/** Application name. */
private static final String APPLICATION_NAME =
"Google Sheets API Java Quickstart";
/** Directory to store user credentials for this application. */
private static final java.io.File DATA_STORE_DIR = new java.io.File(
System.getProperty("user.home"), ".credentials/sheets.googleapis.com-java-quickstart.json");
/** Global instance of the {@link FileDataStoreFactory}. */
private static FileDataStoreFactory DATA_STORE_FACTORY;
/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY =
JacksonFactory.getDefaultInstance();
/** Global instance of the HTTP transport. */
private static HttpTransport HTTP_TRANSPORT;
/** Global instance of the scopes required by this quickstart.
*
* If modifying these scopes, delete your previously saved credentials
* at ~/.credentials/sheets.googleapis.com-java-quickstart.json
*/
private static final List<String> SCOPES =
Arrays.asList(SheetsScopes.SPREADSHEETS);
public static ValueRange response;
public static UpdateValuesResponse request;
public static void main (String[] args) throws Exception {
//List<List<Object>> values = SheetsQuickstart.getResponse("BrowserSheet","A1","A").getValues ();
SheetsQuickstart.setValue("BrowserSheet","A1","A");
}
static {
try {
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
} catch (Throwable t) {
t.printStackTrace();
System.exit(1);
}
}
/**
* Creates an authorized Credential object.
* @return an authorized Credential object.
* @throws IOException
*/
public static Credential authorize() throws IOException {
// Load client secrets.
InputStream in =
SheetsQuickstart.class.getResourceAsStream("/resources/client_secret.json");
GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(DATA_STORE_FACTORY)
.setAccessType("offline")
.build();
Credential credential = new AuthorizationCodeInstalledApp(
flow, new LocalServerReceiver()).authorize("user");
System.out.println(
"Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
return credential;
}
/**
* Build and return an authorized Sheets API client service.
* @return an authorized Sheets API client service
* @throws IOException
*/
public static Sheets getSheetsService() throws IOException {
Credential credential = authorize();
return new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
}
public static ValueRange getResponse(String SheetName,String RowStart, String RowEnd) throws IOException{
// Build a new authorized API client service.
Sheets service = getSheetsService();
// Prints the names and majors of students in a sample spreadsheet:
String spreadsheetId = "1234";
String range = SheetName+"!"+RowStart+":"+RowEnd;
response = service.spreadsheets().values()
.get(spreadsheetId, range).execute ();
return response;
}
public static void setValue(String SheetName,String RowStart, String RowEnd) throws IOException{
// Build a new authorized API client service.
Sheets service = getSheetsService();
// Prints the names and majors of students in a sample spreadsheet:
String spreadsheetId = "1234";
String range = RowStart+":"+RowEnd;
List<List<Object>> arrData = getData();
ValueRange oRange = new ValueRange();
oRange.setRange(range); // I NEED THE NUMBER OF THE LAST ROW
oRange.setValues(arrData);
List<ValueRange> oList = new ArrayList<>();
oList.add(oRange);
BatchUpdateValuesRequest oRequest = new BatchUpdateValuesRequest();
oRequest.setValueInputOption("RAW");
oRequest.setData(oList);
BatchUpdateValuesResponse oResp1 = service.spreadsheets().values().batchUpdate(spreadsheetId, oRequest).execute();
// service.spreadsheets().values().update (spreadsheetId, range,) ;
//return request;
}
public static List<List<Object>> getData () {
List<Object> data1 = new ArrayList<Object>();
data1.add ("Ashwin");
List<List<Object>> data = new ArrayList<List<Object>>();
data.add (data1);
return data;
}
}
我可以通过上面的命令看到Linux上AIX的文本结果的正确结果,但我不能在AIX上。
vmstat 1 1 | awk '{for(i=NF;i>0;i--) if($i=="id") {x=i;break} } END{print $x}'
# Vmstat
# AIX
System Configuration: lcpu=8 mem=16384MB
kthr memory page faults cpu
----- ----------- ------------------------ ------------ -----------
r b avm fre re pi po fr sr cy in sy cs us sy id wa
1 1 1566673 633894 0 0 0 0 1 0 895 7958 348 1 1 98 1
# Linux
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 210564 109296 103864 580288 1 1 145 4152 12 8 7 7 86 0 0
你能看到我错过的东西吗?
vmstat 1 1 使用上面的命令,我们可以在重启后获得平均CPU,因此应该考虑使用以下命令。
86 (on Lnux)
Nothing (on AIX)<----- means just blank line.
答案 0 :(得分:1)
我在想第一行可能是查找id
列的问题。怎么样(对于linux):
vmstat 1 1 | awk '
NR == 2 { for(i=NF;i>0;i--) if($i=="id") {x=i;break} }
NR == 3 { print $x }'
对于AIX:
vmstat 1 1 | awk '
NR == 4 { for(i=NF;i>0;i--) if($i=="id") {x=i;break} }
NR == 5 { print $x }'
NR
是行号,所以我假设你输出的那些。
编辑:在家中进行了一些扩展工作之后,我找到了一个awk解决方案,将数据线保存在数组中:
vmstat 1 1 | awk '{ line[NR] = $0 }
END {
split(line[NR-1],params);
for(i in params) if(params[i] == "id") { break; }
split(line[NR],values);
print values[i]
}'
编辑:关于尾部解决方案,您必须相应地使用NR:
vmstat 1 1 | tail -2 | awk '
NR == 1 { for(i=NF;i>0;i--) if($i=="id") {x=i;break} }
NR == 2 { print $x }'