Linux - 检查文件是否有多行

时间:2016-01-25 08:36:29

标签: linux bash

是否有命令或bash函数来检查文件是否有多行? 假设我有这两个文件:

foo.txt的

Hello World

跳回到bar.txt

Hello
World

3 个答案:

答案 0 :(得分:0)

您可以使用此脚本:

名:test.sh

#!/bin/bash

back=`wc -l $1`
nl=${back:0:1}

if [ $nl = "1" ];then
    echo "single line"
else
    echo "multi lines"
fi

使用此命令更改文件mod:

chmod a+x test.sh

然后执行脚本,paramater是您要测试的文件:

./test.sh hello

答案 1 :(得分:0)

我用这个命令解决了它:

<link rel="stylesheet" type="text/css" href="{{ absolute_url(asset('bootstrap/css/bootstrap.css')) }}">

输出:1

package com.so34985191;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;

/**
 * Solution for SO question 34985191
 */
public class GsonTest {
  public static void main(String args[]) {
    Converter converter = new Converter();

    List<Integer> sampleList1 = Arrays.asList(new Integer[] {1, 2, 3});
    List<Integer> sampleList2 = Arrays.asList(new Integer[] {5, 6, 7});

    @SuppressWarnings({"rawtypes"})
    MyClass[] test = {
        new MyClass<List<Integer>, Integer>(sampleList1),
        new MyClass<List<Integer>, Integer>(sampleList2)};

    Gson gs = new Gson();
    String jsonEncodedString = gs.toJson(test);

    // Example where the type is provided to the getModelList method
    Type listType = new TypeToken<MyClass<List<Integer>, Integer>[]>() {}.getType();
    System.out.println(converter.getModelList(jsonEncodedString, listType));

    // Trying with another example.
    Type nameType = new TypeToken<Test[]>() {}.getType();
    System.out.println(converter.getModelList(
        gs.toJson(new Test[] {new Test("Java"), new Test("Scala")}), nameType));
  }

  /**
   *
   * Utility class to convert a json encoded strings to respective types.
   */
  public static class Converter {
    /**
     * Converts the encoded json string to respective type depeding on the provided type parameter
     * @param jsonStr the string to be decoded.
     * @param type the type used for conversion
     * @return the decoded list
     */
    public <T> List<T> getModelList(String jsonStr, Type type) {
      T[] yourClassList = new Gson().fromJson(jsonStr, type);
      return Arrays.asList(yourClassList);
    }
  }

  /**
   * Container for a list of ids.
   * @param <E>
   */
  public static class MyClass<T extends List<E>, E> {
    private final T ids;

    public MyClass(T ids) {
      this.ids = ids;
    }

    public T getId() {
      return this.ids;
    }

    @Override
    public String toString() {
      return this.ids.toString();
    }
  }

  /**
   * A sample wrapper over string.
   */
  public static class Test {
    private final String name;

    public Test(String name) {
      this.name = name;
    }

    public String getName() {
      return this.name;
    }

    @Override
    public String toString() {
      return this.name;
    }
  }
}

输出:2

这显示了行数,在脚本中我可以检查这个数字是否大于1,我的问题就解决了。

答案 2 :(得分:-1)

你可以这样做:

cat foo.txt | wc -l

这会读取文件并返回文件中的行数。

cat - 阅读文件 管道传输到wc(字数)-l(行数)