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 12-01-2002, 04:24 PM   #1
pk21
Member
 
Registered: Jun 2002
Location: Netherlands - Amsterdam
Distribution: RedHat 9
Posts: 549

Rep: Reputation: 30
Print line for line


how can i open a file and print line for line to the screen in bash???
I was thinking of:

#!/bin/bash
for line in /home/test/list
do
echo $line
sleep 2
done

But this prints each word to the screen in stead of each line in the file.
 
Old 12-01-2002, 07:12 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
... and it should, cuz that's what you asked Bash to do...

#!/bin/bash
cat /home/test/list | while read line; do echo $line; sleep 2; done

If you want to know what went wrong with your script try running it as "bash -x <scriptname>" (and add "2> <scriptname>.log" if you want it logged as well).
 
Old 12-02-2002, 04:05 AM   #3
pk21
Member
 
Registered: Jun 2002
Location: Netherlands - Amsterdam
Distribution: RedHat 9
Posts: 549

Original Poster
Rep: Reputation: 30
Thanks!
 
Old 12-02-2002, 09:47 AM   #4
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
The IFS by default is set to any whitespace, "newline, tab, space" if you set the field seperator to be newline only, the script works.

#!/bin/bash

IFS='
'

for line in `cat web.xml`; do
echo $line
sleep 1
done
 
Old 12-02-2002, 01:59 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
"for line in `cat web.xml`; do"

will cause the script to exit with an error if web.xml is big.
unSpawn's solution doesn't have this problem:

"cat web.xml | while read line ; do"
 
Old 12-02-2002, 02:48 PM   #6
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
True, I was just explaining why his script was displaying words and not lines.
 
Old 03-16-2004, 07:25 AM   #7
pablomme
LQ Newbie
 
Registered: Mar 2004
Location: Spain / UK
Distribution: Mandrake 10.0
Posts: 3

Rep: Reputation: 0
Quote:
Originally posted by crabboy
The IFS by default is set to any whitespace, "newline, tab, space" if you set the field seperator to be newline only, the script works.

#!/bin/bash

IFS='
'

for line in `cat web.xml`; do
echo $line
sleep 1
done
This is useful anyway, I think, because I found out that piping the cat output to 'while read line do {' causes the following code to be executed in a sub-shell, so that any variable that you set within do { } done is automatically forgotten after that. So I've got some questions:
1) How to make the 'cat $file | while read line do {' stuff work and not execute the code in a subshell?
2) Or otherwise, how can I make changes done in a subshell persist after exit? For example, if I change dir and want to stay there after the script, or if I set a variable and want to keep it.
3) Apart from all of this, how can I get a specific line (whose number is known) from a file?
Thanks!
 
Old 03-16-2004, 10:12 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Don't forget to SAVE $IFS before you change it.

ifs=$IFS

blah blah

IFS=$ifs


Otherwise someone who calls your script may get some
unwelcome surprises!

You can't persist variables upwards to the calling script, sorry!
You can 'dot' a file like you do your ~/.bashrc which runs inline.






regards, billy
 
Old 03-16-2004, 10:14 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Getting a specific line?

There's a million ways,
how about....

the 50th line?

Code:
      sed -n '50p' file


billy
 
Old 03-16-2004, 12:01 PM   #10
pablomme
LQ Newbie
 
Registered: Mar 2004
Location: Spain / UK
Distribution: Mandrake 10.0
Posts: 3

Rep: Reputation: 0
Quote:
Originally posted by bigearsbilly
Getting a specific line?

There's a million ways,
how about....

the 50th line?

Code:
      sed -n '50p' file


billy
Thanx indeed! Didn't figure that out from 'man sed'. Any other of those million ways that you'd like to mention?
 
Old 03-16-2004, 12:09 PM   #11
pablomme
LQ Newbie
 
Registered: Mar 2004
Location: Spain / UK
Distribution: Mandrake 10.0
Posts: 3

Rep: Reputation: 0
> You can't persist variables upwards to the calling script, sorry!
Shame!
> You can 'dot' a file like you do your ~/.bashrc which runs inline.
Don't get that... I've tried to write a script ".env" that sets $ENV to "Hello", but doesn't exist afterwards.. I think I did't understand what you meant.
Thanx!
 
Old 03-17-2004, 04:04 AM   #12
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
No no, pablo
when i mean'dot' a file i mean like:
call it with a .

literally, type 'dot' space filename:

Code:
.  file
 
Old 03-17-2004, 04:12 AM   #13
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
you can create functions which you can
use for setting environment or changing directory:

e.g
create a file called say, funcs with the following function:

Code:
billym.devserver>cat funcs   # here is a file i created
mycd()
{
    cd $1
}
billym.devserver>pwd
/tmp
billym.devserver>. funcs    # this puts the function into the current env.
billym.devserver>mycd $HOME  # so this now works
billym.devserver>pwd
/export/home/billym
 
  


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
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
print the line of following line no suchi_s Programming 6 09-08-2004 08:22 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
Print via OO Writer from command line? MikHud Linux - Software 2 12-13-2002 09:55 AM
Print from command line cli_man Linux - Newbie 4 06-16-2002 09:57 PM

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

All times are GMT -5. The time now is 06:55 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