Read a file line by line assigning the value to a variable

后端 未结 10 976
说谎
说谎 2020-11-21 07:37

I have the following .txt file:

Marco
Paolo
Antonio

I want to read it line-by-line, and for each

相关标签:
10条回答
  • 2020-11-21 08:30

    The following reads a file passed as an argument line by line:

    while IFS= read -r line; do
        echo "Text read from file: $line"
    done < my_filename.txt
    

    This is the standard form for reading lines from a file in a loop. Explanation:

    • IFS= (or IFS='') prevents leading/trailing whitespace from being trimmed.
    • -r prevents backslash escapes from being interpreted.

    Or you can put it in a bash file helper script, example contents:

    #!/bin/bash
    while IFS= read -r line; do
        echo "Text read from file: $line"
    done < "$1"
    

    If the above is saved to a script with filename readfile, it can be run as follows:

    chmod +x readfile
    ./readfile filename.txt
    

    If the file isn’t a standard POSIX text file (= not terminated by a newline character), the loop can be modified to handle trailing partial lines:

    while IFS= read -r line || [[ -n "$line" ]]; do
        echo "Text read from file: $line"
    done < "$1"
    

    Here, || [[ -n $line ]] prevents the last line from being ignored if it doesn't end with a \n (since read returns a non-zero exit code when it encounters EOF).

    If the commands inside the loop also read from standard input, the file descriptor used by read can be chanced to something else (avoid the standard file descriptors), e.g.:

    while IFS= read -r -u3 line; do
        echo "Text read from file: $line"
    done 3< "$1"
    

    (Non-Bash shells might not know read -u3; use read <&3 instead.)

    0 讨论(0)
  • 2020-11-21 08:30

    Many people have posted a solution that's over-optimized. I don't think it is incorrect, but I humbly think that a less optimized solution will be desirable to permit everyone to easily understand how is this working. Here is my proposal:

    #!/bin/bash
    #
    # This program reads lines from a file.
    #
    
    end_of_file=0
    while [[ $end_of_file == 0 ]]; do
      read -r line
      # the last exit status is the 
      # flag of the end of file
      end_of_file=$?
      echo $line
    done < "$1"
    
    0 讨论(0)
  • 2020-11-21 08:34

    Using the following Bash template should allow you to read one value at a time from a file and process it.

    while read name; do
        # Do what you want to $name
    done < filename
    
    0 讨论(0)
  • 2020-11-21 08:37

    Use IFS (internal field separator) tool in bash, defines the character using to separate lines into tokens, by default includes <tab> /<space> /<newLine>

    step 1: Load the file data and insert into list:

    # declaring array list and index iterator
    declare -a array=()
    i=0
    
    # reading file in row mode, insert each line into array
    while IFS= read -r line; do
        array[i]=$line
        let "i++"
        # reading from file path
    done < "<yourFullFilePath>"
    

    step 2: now iterate and print the output:

    for line in "${array[@]}"
      do
        echo "$line"
      done
    

    echo specific index in array: Accessing to a variable in array:

    echo "${array[0]}"
    
    0 讨论(0)
提交回复
热议问题