LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-17-2005, 01:20 PM   #1
xpucto
Member
 
Registered: Sep 2005
Location: Vienna, Austria
Distribution: Mint 13
Posts: 524

Rep: Reputation: 31
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
 
Old 09-17-2005, 01:43 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
what have you tried, what are you specifically stuck on...

Code:
post code between code tags
 
Old 09-17-2005, 01:45 PM   #3
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
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
 
Old 09-17-2005, 01:50 PM   #4
xpucto
Member
 
Registered: Sep 2005
Location: Vienna, Austria
Distribution: Mint 13
Posts: 524

Original Poster
Rep: Reputation: 31
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.
 
Old 09-17-2005, 02:06 PM   #5
xpucto
Member
 
Registered: Sep 2005
Location: Vienna, Austria
Distribution: Mint 13
Posts: 524

Original Poster
Rep: Reputation: 31
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

Last edited by xpucto; 09-17-2005 at 02:09 PM.
 
Old 09-18-2005, 06:08 AM   #6
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
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.
 
Old 09-20-2005, 08:06 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM
[Shell] Read a File from script yussef Programming 4 08-19-2008 04:26 AM
How to execute a command line in a shell script orgazmo Programming 5 05-09-2005 09:16 AM
Shell Script to read 500files from the command line saravanan1979 Programming 1 09-22-2004 09:44 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration