BASH:如何从文件中获取X和Y行(1-线性)

时间:2019-02-01 21:55:19

标签: bash

我有一些输出,我想得到第1行和第7行。作为输出流,我们可以用2个不同的模数流式传输它。但是,我离题了。

我可以轻松地通过for循环来做到这一点,但我想知道是否有更实用的1行方法:

以下是我正在使用的数据(我想要URL和内容类型):

--2019-02-01 01:02:19--  https://artifactory/artifactory/BIF-Releases/com/foo/bif/eventlog/maven-metadata.xml.md5
Reusing existing connection to :443.
HTTP request sent, awaiting response... 
  HTTP/1.1 200 OK
  Server: nginx/1.12.2
  Date: Fri, 01 Feb 2019 09:02:33 GMT
  Content-Type: application/x-checksum
  Content-Length: 32
  Connection: keep-alive
  X-Artifactory-Id: d111c347124a8603:2a97a6e1:1681a62df25:-8000
  Last-Modified: Fri, 01 Feb 2019 09:02:33 GMT
--
--2019-02-01 01:02:19--  https://artifactory/artifactory/BIF-Releases/com/foo/bif/eventlog/maven-metadata.xml.sha1
Reusing existing connection to artifactory:443.
HTTP request sent, awaiting response... 
  HTTP/1.1 200 OK
  Server: nginx/1.12.2
  Date: Fri, 01 Feb 2019 09:02:33 GMT
  Content-Type: application/x-checksum
  Content-Length: 40
  Connection: keep-alive
  X-Artifactory-Id: d111c347124a8603:2a97a6e1:1681a62df25:-8000
  Last-Modified: Fri, 01 Feb 2019 09:02:33 GMT

我想要的输出就是:

--2019-02-01 01:02:19--  https://artifactory/artifactory/BIF-Releases/com/foo/bif/eventlog/maven-metadata.xml.md5
  Content-Type: application/x-checksum
--2019-02-01 01:02:19--  https://artifactory/artifactory/BIF-Releases/com/foo/bif/eventlog/maven-metadata.xml.sha1
Content-Type: application/x-checksum

3 个答案:

答案 0 :(得分:1)

要获取第1行和第7行:

sed -n -e 1p -e 7p

您可能还想提前终止:

sed -n -e 1p -e '7{p; q;}' 

sed -n -e 1p -e 7p -e 7q

答案 1 :(得分:1)

第1行和第7行:

<html>
<body>
    <h1>Hello</h1>
    <form method="POST" action="{{ route('date_process') }}">
        <input type="text"/>
        <input type="submit" value="Enviar">
    </form>
    <script src="{{asset('js/jquery-3.3.1.min.js')}}"></script>
    <script src="{{asset('js/script.js')}}"></script>
</body>
@csrf

</html>

Route::get('/', 'DateFormController@show');
Route::post('/date_process', 'DateFormController@process')->name('date_process');

or
Route::get('/', 'DateFormController@show');
Route::post('/date_process', ['uses'=>'DateFormController@process','as'=>'date_process']);

答案 2 :(得分:1)

您也可以尝试选择一些内容:

grep -E "https://|Content-Type:"
# Or when you want to remove the date
grep -Eo "(https://|Content-Type:).*"