自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何在Shell腳本中逐行讀取文件

系統(tǒng) Linux
本文介紹了如何使用shell腳本逐行讀取文件內(nèi)容,通過單獨(dú)讀取行,可以幫助搜索文件中的字符串。

 在這里,我們學(xué)習(xí)Shell腳本中的3種方法來(lái)逐行讀取文件。

方法一、使用輸入重定向

逐行讀取文件的最簡(jiǎn)單方法是在while循環(huán)中使用輸入重定向。

為了演示,在此創(chuàng)建一個(gè)名為“ mycontent.txt”的文本文件,文件內(nèi)容在下面: 

  1. [root@localhost ~]# cat mycontent.txt   
  2. This is a sample file  
  3. We are going through contents  
  4. line by line  
  5. to understand 

創(chuàng)建一個(gè)名為“ example1.sh”的腳本,該腳本使用輸入重定向和循環(huán): 

  1. [root@localhost ~]# cat example1.sh   
  2. #!/bin/bash  
  3. while read rows  
  4. do  
  5.   echo "Line contents are : $rows "  
  6. done < mycontent.txt 

運(yùn)行結(jié)果:

如何工作的:

    - 開始while循環(huán),并在變量“rows”中保存每一行的內(nèi)容

    - 使用echo顯示輸出內(nèi)容,$rows變量為文本文件中的每行內(nèi)容

    - 使用echo顯示輸出內(nèi)容,輸出內(nèi)容包括自定義的字符串和變量,$rows變量為文本文件中的每行內(nèi)容

Tips:可以將上面的腳本縮減為一行命令,如下: 

  1. [root@localhost ~]# while read rows; do echo "Line contents are : $rows"; done < mycontent.txt 

方法二、使用cat命令和管道符

第二種方法是使用cat命令和管道符|,然后使用管道符將其輸出作為輸入傳送到while循環(huán)。

創(chuàng)建腳本文件“ example2.sh”,其內(nèi)容為: 

  1. [root@localhost ~]# cat example2.sh   
  2. #!/bin/bash  
  3. cat mycontent.txt | while read rows  
  4. do  
  5.   echo "Line contents are : $rows "  
  6. done 

運(yùn)行結(jié)果:

如何工作的:

    - 使用管道將cat命令的輸出作為輸入發(fā)送到while循環(huán)。

    - |管道符將cat輸出的內(nèi)容保存在"$rows"變量中。

    - 使用echo顯示輸出內(nèi)容,輸出內(nèi)容包括自定義的字符串和變量,$rows變量為文本文件中的每行內(nèi)容

Tips:可以將上面的腳本縮減為一行命令,如下: 

  1. [root@localhost ~]# cat mycontent.txt |while read rows;do echo "Line contents are : $rows";done 

方法三、使用傳入的文件名作為參數(shù)

第三種方法將通過添加$1參數(shù),執(zhí)行腳本時(shí),在腳本后面追加文本文件名稱。

創(chuàng)建一個(gè)名為“ example3.sh”的腳本文件,如下所示: 

  1. [root@localhost ~]# cat example3.sh   
  2. #!/bin/bash  
  3. while read rows  
  4. do  
  5.   echo "Line contents are : $rows " 
  6. done < $1 

運(yùn)行結(jié)果:

如何工作的:

    - 開始while循環(huán),并在變量“rows”中保存每一行的內(nèi)容

    - 使用echo顯示輸出內(nèi)容,$rows變量為文本文件中的每行內(nèi)容

    - 使用輸入重定向<從命令行參數(shù)$1讀取文件內(nèi)容

方法四、使用awk命令

通過使用awk命令,只需要一行命令就可以逐行讀取文件內(nèi)容。

創(chuàng)建一個(gè)名為“ example4.sh”的腳本文件,如下所示: 

  1. [root@localhost ~]# cat example4.sh   
  2. #!/bin/bash  
  3. cat mycontent.txt |awk '{print "Line contents are: "$0}' 

運(yùn)行結(jié)果:

總結(jié)

本文介紹了如何使用shell腳本逐行讀取文件內(nèi)容,通過單獨(dú)讀取行,可以幫助搜索文件中的字符串。 

 

責(zé)任編輯:龐桂玉 來(lái)源: Linux學(xué)習(xí)
相關(guān)推薦

2021-04-21 08:03:34

腳本Shell讀取

2016-12-16 09:23:29

LinuxShell腳本

2016-12-20 09:30:22

shell腳本linux

2023-01-15 17:11:44

Rust

2021-02-15 17:29:46

LinuxShell腳本

2021-08-30 07:50:42

腳本語(yǔ)言命令行

2022-11-03 08:13:52

echo 命令Linux

2022-11-01 15:38:22

LinuxShell

2018-02-01 17:32:30

LinuxUNIXBash Shell

2017-01-18 20:38:36

LinuxShell腳本命令

2022-12-22 20:47:01

腳本循環(huán)結(jié)構(gòu)

2021-03-14 09:28:24

Linux Shell腳本

2022-10-09 10:18:44

LinuxShell腳本

2012-05-08 10:51:10

Linuxshell腳本

2023-04-04 07:52:26

RedisLua腳本

2022-01-14 09:10:56

C++文件Linux

2017-12-18 10:12:48

LinuxShell命令

2021-01-18 17:23:30

代碼調(diào)試VS Code

2023-10-19 14:52:27

2020-11-02 08:23:36

shell腳本Linux
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)