茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1664574 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
PIMG_00194.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

小企鵝開談 : [轉貼]Bash Scripting & Read File line by line

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15766
[轉貼]Bash Scripting & Read File line by line
Bash Scripting & Read File line by line

I'm new to bash scripting. I've the following txt file:

I want to read it line by line, and for each line I want to assign txt line value to a variable: I explain better (suppose my variable is $name), flow is:
Read First line from file
 assign $name = "Marco"
...
 doing some tasks with $name
...
Read Second line from file
assign $name = "Paolo"

Thank you all, and if I want read a file using expect how should I do? I want do that because when I wrote

I meant that my tasks are expect commands.

The following (save as rr.sh) reads a file passed as an argument line by line:
#!/bin/bash
while IFS='' read -r line || [[ -n "$line" ]]; do
    echo "Text read from file: $line"
done < "$1"

Explanation:

IFS='' (or IFS=) prevents leading/trailing whitespace from being trimmed.
-r prevents backslash escapes from being interpreted.
|| [[ -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).

Run the script as follows:
chmod +x rr.sh
./rr.sh filename.txt


I encourage you to use -r flag for read which stands for:
   -r     Do not treat a backslash character in any special way. Consider each
          backslash to be part of the input line.

cite from man 1 read.

Another thing is to take filename as an argument.

Here is updated code:
#!/usr/bin/bash
filename="$1"
while read -r line
do
    name="$line"
    echo "Name read from file - $name"
done < "$filename"


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


filename=$1
IFS=$'\n'
for next in `cat $filename`
do
    echo "$next read from $filename"
done
exit 0


#! /bin/bash
cat filename | while read LINE
do
echo $LINE
done


I read the question: " if I want read a file using expect how should I do? I want do that because when I wrote 'doing some tasks with $name', I meant that my tasks are expect commands."

Why not read the file from within expect itself?

yourExpectScript:
#!/usr/bin/expect
# pass in filename from command line

set filename [ lindex $argv 0 ]

# assumption: file in same directory

set inFile [ open $filename r ]

while { ! [ eof $inFile ] } {

    set line [ gets $inFile ]

    # you could set name directly.

    set name $line

    # do other expect stuff with $name ...

    puts " Name: $name"

}

close $inFile

Then call it like:
yourExpectScript file_with_names.txt


One way reading line by line is this also
//read.sh
    #!/bin/bash

    while read variable
    do
       echo $variable
    done

where read.sh is script file and input.txt is data file
On shell give this command
$./read.sh < input.txt

data=cat fileName.txt this command returens the content of file or data=$(
shareimprove this answer

If you'd rather stick with bash without expect, then use the following solution:
#!/bin/bash
exec 3<"$1"
while IFS='' read -r -u 3 line || [[ -n "$line" ]]; do
    read -p "> $line (Press Enter to continue)"
done

Based on the accepted answer and on the bash-hackers redirection tutorial.

Here, we open the file decriptor 3 for the file passed as the script argument and tell read to use this descriptor as input (-u 3). Thus, we leave the default input descriptor (0) attached to a terminal or another input source, able to read user input.

原文出處:Bash Scripting & Read File line by line - Stack Overflow
前一個主題 | 下一個主題 | 頁首 | | |



Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|