Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Tuesday, June 7, 2016

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:

Marco  Paolo  Antonio  

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

doing some tasks with $name

I meant that my tasks are expect commands.

Answer by cppcoder for Bash Scripting & Read File line by line


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  

....

Answer by OneWinged for Bash Scripting & Read File line by line


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  

Answer by Grzegorz Wierzowiecki for Bash Scripting & Read File line by line


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"  

Answer by user3546841 for Bash Scripting & Read File line by line


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

if you have set IFS differently you will get odd results.

Answer by Nathan for Bash Scripting & Read File line by line


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

Answer by Varun for Bash Scripting & Read File line by line


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

Answer by Gert for Bash Scripting & Read File line by line


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

Answer by Kolli Yathi for Bash Scripting & Read File line by line


data=cat fileName.txt  

this command returens the content of file or

data=$(

Answer by gluk47 for Bash Scripting & Read File line by line


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.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.