awk拆分字符串和比较

时间:2017-03-10 05:09:19

标签: awk

我有一个像AS|REQ|XYZ|value=12这样的字符串,我正在分享:

awk -F\| 'print {$4}' | awk -F"=" '{print $2}'

这给出了值12。

但对于字符串DF|REG|EXP|value=,它会变回空白。

我需要的是好像我的字符串在第四列中遇到value并且是空白的,抛出错误。这可以用awk命令完成吗?

由于

5 个答案:

答案 0 :(得分:2)

您可以更明确地指出抛出错误的含义。如果您希望程序以非零退出代码退出,请使用TOP 1if和值`:

exit

或只是打印错误信息并继续执行:

$ awk 'BEGIN{exit}'
$ echo $?
0
$ awk 'BEGIN{exit 1}'
$ echo $?
1
$ awk -F\| '{split($4,a,"="); if(a[2]=="") exit 1; else print a[2]}' foo
12
$ echo $?
1

上面使用的测试数据:

$ awk -F\| '{split($4,a,"="); print (a[2]==""?"ERROR":a[2])}' foo
12
ERROR

答案 1 :(得分:2)

@JamesBrown对你提出的问题有正确的答案,但是根据您发布的输入,您需要生成所需的所有输出:

0:072> !clrstack
OS Thread Id: 0x31ec (72)
        Child SP               IP Call Site
000000aea766d968 000007f9736a4650 [HelperMethodFrame: 000000aea766d968] 
000000aea766da50 000007f9674e0e5a System.Collections.ArrayList.Add(System.Object)
000000aea766da90 000007f966655292 System.Collections.Specialized.NameObjectCollectionBase.BaseAdd(System.String, System.Object)
000000aea766dae0 000007f9650ac4c9 System.Web.SessionState.SessionStateItemCollection.set_Item(System.String, System.Object)
000000aea766db20 000007f90ed89ce9 UTL.WebStateManager.set_Item(System.String, System.Object)
000000aea766dbf0 000007f90f29370c WebStateManagerHelper.get_OriginalPNR()
000000aea766dc80 000007f90f29242d QueryDynamicLoggingComponent.LogTransaction(System.String, System.String)
000000aea766e110 000007f90f2917e3 WSHelper.Log(System.String, System.String, Boolean, System.String)
000000aea766e160 000007f90f28fd17 WSHelper.GetResponse(System.String, SecurityInfo, System.String, System.String, System.String ByRef, System.String, System.String)
000000aea766e5d0 000007f90f29eae6 WSHelper.SendQuery(System.String, SecurityInfo, System.String)
000000aea766e7f0 000007f90f29e7f8 WSHelper.SendQuery(SecurityInfo, System.String)
000000aea766e840 000007f90f29e4af APIWSPool.SendAndReceiveQueryToString(Agency, System.String, Token, Boolean)
000000aea766e940 000007f90f29e374 APIWSPool.SendAndReceiveQuery(Agency, Token, Boolean)
000000aea766e9b0 000007f90f6168f4 FlightBookingManager.SearchFlightForMPSearchedFlightRecommendations1(Agency, FlightFareDrivenSearchInfo, Boolean)
000000aea766eb80 000007f90f615ec1 ApiFlightBookingProvider.SearchFlightForMPSearchedFlightRecommendations1(Agency, FlightFareDrivenSearchInfo, Boolean)
000000aea766ebe0 000007f90f6158f2 APICOM.Threading.OWCOutboundSearchThread.Work()
000000aea766edb0 000007f9674e2d45 System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
000000aea766ef10 000007f9674e2ab9 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
000000aea766ef40 000007f9674e2a97 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
000000aea766ef90 000007f9674fa161 System.Threading.ThreadHelper.ThreadStart()
000000aea766f2a8 000007f96e0eab53 [GCFrame: 000000aea766f2a8] 
000000aea766f5f8 000007f96e0eab53 [DebuggerU2MCatchHandlerFrame: 000000aea766f5f8] 
000000aea766f788 000007f96e0eab53 [ContextTransitionFrame: 000000aea766f788] 
000000aea766f9a8 000007f96e0eab53 [DebuggerU2MCatchHandlerFrame: 000000aea766f9a8] 

如果这不是您所需要的,那么请编辑您的问题以显示更具真实代表性的样本输入和预期输出。

答案 2 :(得分:0)

或许这样的事情?

awk -F\| '{print $4}' | awk -F"=" '{if ($2 == "") print "ERROR: Empty Value"; else print $2}'

答案 3 :(得分:0)

希望此命令可能适合您。以下命令将按预期运行。如果值字段中有任何值,则只打印该值。否则,如果它是空白,则打印“错误”。该字符串放在test.txt

public class ApiClient {
    private static final int TIME_OUT = 30;
    public static final String BASE_URL = "http://api.openweathermap.org/";
    private static Retrofit retrofit = null;


    public static Retrofit getClient() {
         if (retrofit==null) {
             OkHttpClient.Builder okBuilder = new OkHttpClient.Builder();

             okBuilder.connectTimeout(TIME_OUT, TimeUnit.SECONDS);
             okBuilder.readTimeout(TIME_OUT, TimeUnit.SECONDS);
             okBuilder.writeTimeout(TIME_OUT, TimeUnit.SECONDS);

             Gson gson = new GsonBuilder().create();

             return new Retrofit.Builder()
                 .baseUrl(BASE_URL)                  .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(gson))
                .client(okBuilder.build())
                .build();
    }
    return retrofit;
}

public interface ApiInterface {
     @GET("data/2.5/forecast?id=524901")
     Call<WeatherResponse> getWeatherData(@Query("APPID") String apiKey);
}

答案 4 :(得分:0)

像这样 -

value

搜索4th列中的blank5th列中的<%= form_for (@booking) do |f| %> <%= f.hidden_field :user_id, value: current_user.id %> <%= f.hidden_field :training_id, value: "" %> #pass training id in value <%= f.submit "Confirm book", class: "btn btn-primary" %> <% end %>

相关问题