Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Systems > Linux / BSD / OS X
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 08-15-2005, 01:44 PM   #1 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
Iterating through a file

I want to iterate through the lines in a text file (they aren't single-word lines). What's the best way to do that in a shell script?
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-15-2005, 03:12 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
do you have a php interpretor?
PHP Code:
#! /usr/bin/php4
<?php
// myscript.php

$array file('/home/me/myfile.txt');

if(
is_array($array)){

  foreach(
$array as $line){
    
// do something with line
  
}
}
?>
Code:
$ chmod u+x myscript.php $ ./myscript.php
i like using php as a server-side scripting language. =)
__________________
testing 1 2 3
sde is offline   Reply With Quote
Old 08-15-2005, 03:36 PM   #3 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
I thought about it, but I'd need to install PHP. I was hoping there was a way to do it with regular Unix tools, but messing around with wc, head and tail just is too ugly. . .
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-15-2005, 05:17 PM   #4 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
Here's a snippet from a script I use to remove users from AIX in bulk.. There's probably a better way, but I had to scrap something together without much fuss..

Code:
for i in `cat $1|grep -v -E '^#'|cut -d"|" -f2` do if [ $2 = "-k" ]; then echo "Saving lsuser info - '$i'" lsuser -f $i |tee -a $1.lsuser echo "--------------------------" >> $1.lsuser echo "Removing user - '$i'" rmuser -p $i else echo "Skipping user - '$i'" fi done
So each line in the loop is stored in $i .. Not sure if you need to split the line or get certain pieces, but that may help..

(worst case use perl.. it'll probably be installed)

-r
__________________
idx is offline   Reply With Quote
Old 08-15-2005, 05:18 PM   #5 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
Oh yeah, my input file was pipe delimited, so the cut -d"|" -f2 is just grabbing the username.. Perhaps adjust the entire line to `cat $1` for no filtering.

-r
__________________
idx is offline   Reply With Quote
Old 08-15-2005, 10:00 PM   #6 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 595
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
I think if you ned to do arbitrary transforms to lines, sed is the best utility. If you only need some lines grep is the ticket.
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 08-16-2005, 01:47 AM   #7 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
I'm transforming some HTML output I grab with CURL into a csv. I'm 99% of the way there (with a number of sed calls), but the problem is I can't simply walk through line-by-line to attach the <td> values together. I guess I'll play around with sed some more, and transform </td> into a comma and try to remove the CR/LF, eliminating the need to walk through.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-16-2005, 02:05 AM   #8 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 595
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
if sed runs out of steam, the next step would be perl with the HTML::Treebuilder module.
Alternatively 'perl -pe 's/regex/subst/g' is a great sed substitute. 'perl -pe 's/\n//mg' will turn a file with unix line endings into one really long line for example.
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 08-16-2005, 06:08 AM   #9 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
I can't figure out how to insert a newline in sed. \n only gives me an "n". *sigh* - that was the last hurdle. I think I'm going to switch over to PHP because I really don't want to learn Perl just for this.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-16-2005, 08:07 AM   #10 (permalink)
fp_unit
mike
 
Join Date: Jan 2005
Location: Ottawa, ON
Posts: 79
fp_unit is on a distinguished road
Talking

Code:
#!/usr/bin/perl open(FILE, "> yourfile.txt"); while(line = <FILE>) { # do something with line }
__________________
fp_unit is offline   Reply With Quote
Old 08-16-2005, 11:32 AM   #11 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 595
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
The reason you can't do that with sed, is because sed only sees one line at a time. But like I said, perl can be a good sed substitute, if invoked as perl -pe 'your old sed directives' it behaves exactly like sed. The addition of m to a search makes it a multiline search, which sed can't usually do. Also if your sed scripts are huge, you can uses s2p to have it automatically converted to a perl script (though it'll be even less legible that hand written perl).
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 08-16-2005, 03:14 PM   #12 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
Actually, I got it working using "tr" to strip out new lines, and turning the whole thing into a single line. From there I turned </tr> into a newline (had to install GNU sed to get it to work - the sed that was on Solaris9 really disliked my attempts to insert a newline), and </td><td> into a comma. Seems to have worked, I'll test it out tomorrow and see if the program that reads the CSV likes it.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-20-2005, 02:50 PM   #13 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
Quote:
Originally Posted by fp_unit
Code:
#!/usr/bin/perl open(FILE, "> yourfile.txt"); while(line = <FILE>) { # do something with line }
? No, that would write to the file.

-r
__________________
idx is offline   Reply With Quote
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
'old-skool' server code enhancement zyrxfz Standard C, C++ 11 06-14-2005 05:17 AM
Output problem with file manipulation (newbie) crazyant Java 4 03-11-2005 01:03 PM
.htaccess -- Image Hotlinking Prevention DavH27 HTML / CSS 0 08-27-2004 03:43 AM
.htaccess -- Custom Error Pages v1.1 DavH27 HTML / CSS 0 08-26-2004 04:40 PM
Dynamic File Includes Sarlok ASP Classic 0 02-24-2003 12:49 PM


All times are GMT -8. The time now is 09:30 PM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle