LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   shell script that read each line separatly (http://www.linuxquestions.org/questions/programming-9/shell-script-that-read-each-line-separatly-364259/)

xpucto 09-17-2005 02:20 PM

shell script that read each line separatly
 
Hi,

I would like to write a shell script that reads a text-file, reading out each line separatly in order to use each line as argument.

The Problem is that I do not know how to read each line separatly.
My file has user's names:
bob
john
chris
.....

and I would like to write a script that reads the 1st line and uses bob as argument, then the 2nd line and uses it as argument for the same command, and so on.

I would really appreciate any help.

Christophe

schneidz 09-17-2005 02:43 PM

what have you tried, what are you specifically stuck on...

Code:

post code between code tags

david_ross 09-17-2005 02:45 PM

Try this:
Code:

#!/bin/bash
# Set the field seperator to a newline
IFS="
"
# Loop through the file
for line in `cat file.txt`;do
# Echo the line (echo could be changed to whatever command you want)
 echo $line
done


xpucto 09-17-2005 02:50 PM

the code that I wrote is:
<code>
#!/bin/bash
#file = $1
count= cat $1 | wc -l
myuser=cat $1 | ?????? What should I write here to read each single line separatly and use as argument?

for i in count
do
./another_script mysuser
done
</code>
I don't know how to get just a line at a time (a line beeing a word) to save it into the variable myuser.

xpucto 09-17-2005 03:06 PM

Great! It works fine!
I wrote the following test-script.
Code:

#!/bin/bash
# Set the field seperator to a newline
IFS="
"
# Loop through the file
for line in `cat file.txt`;do
# Echo the line (echo could be changed to whatever command you want)
echo "$line wird gelöscht" >> text2
echo "$line wurde gelöcht"
done


eddiebaby1023 09-18-2005 07:08 AM

If you're using bash (or ksh) the construct
Code:

for line in $(< file.txt);do
is better as the shell doesn't have to run a separate command to get the file contents.

bigearsbilly 09-20-2005 09:06 AM

this doesn't need IFS
and reads the file as needed - doesn't slurp it in as one big line.

Code:


while read line ;do
  echo $line
done < infile



All times are GMT -5. The time now is 05:11 PM.