while read读取文本内容

读取文件给 while 循环
方式一:

exec <FILE
while read line
do
    cmd
done 

方式二:

cat FILE_PATH |while read line
do
    cmd
done 

方式三:

while read line
do
    cmd
done <FILE

举例:

ip.txt内容如下:

10.1.1.11 root 123
10.1.1.22 root 111
10.1.1.33 root 123456
10.1.1.44 root 54321

写法1:

cat ip.txt | while read ip user pass
do
    echo "$ip--$user--$pass"
done

写法2:

while read ip user pass
do
    echo "$ip--$user--$pass"
done < ip.txt

使用IFS读文件

说明:默认情况下IFS是空格,如果需要使用其它的需要重新赋值

IFS=:

例如:

# cat test
chen:222:gogo
jie:333:hehe
# cat test.sh
#!/bin/bash
IFS=:
cat test | while read a1 a2 a3
do
    echo "$a1--$a2--$a3"
done

原文地址:https://www.cnblogs.com/fcing/p/9375117.html

时间: 2024-08-26 17:49:58

while read读取文本内容的相关文章

面试题目java读取文本内容方式

面试题目java读取文本内容方式二种方式 第一种通过FileInputStream()方式读取 FileInputStream fis = new FileInputStream("a.txt"); //创建流对象 byte[] arr = new byte[4]; int len; while((len = fis.read(arr)) != -1) { System.out.print(new String(arr,0,len)); } fis.close(); 第二种通过:Fil

Shell 读取文本内容

在Linux中有很多方法逐行读取一个文件的方法,其中最常用的就是下面的脚本里的方法,而且是效率最高,使用最多的方法.为了给大家一个直观的感受,我们将通过生成一个大的文件的方式来检验各种方法的执行效率. 方法1:while循环中执行效率最高,最常用的方法.  function while_read_LINE_bottm(){     While read LINE     dod         echo $LINE     done  < $FILENAME }           注释:我习惯

关于逐行逐行读取文本内容并写入数组

有关逐行写入的在这里 下面是关于逐行读取 fn maxfilelog finpath = ( fin = openfile finpath seek fin #eof maxlen=filepos fin seek fin 0 res = readChars fin maxlen errorAtEOF:false filterString res "\n" ) -- 将文件内容逐行添加到数组 之前想用来记录和读取配置文件,后来发现了更简单的... getINISetting <f

Java中使用字节流类读取文本内容

package cn.jbit.inputstream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Test { /** * @param args */ public static void main(Stri

ajax读取文本内容(此处的txt文件和html文件处于同级目录)

<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <style> </style> <title>read file</title> </head> <script src="jquery-1.11.2.js"></script> <script> $(functi

Python将文本内容读取分词并绘制词云图

功能:Python将文本内容读取分词并绘制词云图 import matplotlib import matplotlib.pyplot as plt #数据可视化 import jieba #词语切割 import wordcloud #分词 from wordcloud import WordCloud,ImageColorGenerator,STOPWORDS #词云,颜色生成器,停止 import numpy as np #科学计算 from PIL import Image #处理图片

Android Studio 如何获取 text文本内容

1.找到目录的main先建立assets格式的文件夹 2.再把需要读取的txt 文件放入到该文件夹下(名字随意),这里取 list.txt. 文件内容 格式如下 3.读取文本内容 工具代码 /** * 返回学生名单 以String 数组形式 * * @return */ public String[] initAssets() { try { //获取输入流 InputStream inputStream = getAssets().open("list.txt"); //这里的名字是

获取Asset下文本内容和读取图片

1 import android.content.Context; 2 import android.content.res.AssetManager; 3 import android.graphics.Bitmap; 4 import android.graphics.BitmapFactory; 5 6 import java.io.BufferedReader; 7 import java.io.ByteArrayOutputStream; 8 import java.io.IOExce

C++ 读取txt文本内容,并将结果保存到新文本

循序渐进学习读文件 1 // readFile.cpp : 定义控制台应用程序的入口点. 2 3 4 #include "stdafx.h" 5 #include <iostream> 6 #include <fstream> 7 #include <string> 8 using namespace std; 9 10 //引申:文件拷贝 11 void fileCopy(string file1,string file2){ 12 ifstrea