File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Linux / UNIX and the fly likes Read file contents in loop using shell script Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Read file contents in loop using shell script" Watch "Read file contents in loop using shell script" New topic

Read file contents in loop using shell script

Rahul Chaudhari

Joined: Jul 30, 2008
Posts: 5
I have one log file which containt output of ls command. I want to read that file content and get the file name and size then I have to check that if that file size is zero then touch that file locally other wise get that file from ftp.

log file contents are(FileName is filelist.txt):

unknown security mechanism
unknown security mechanism
Passive mode off.
-rwxr----- 1 smartscm System 761 Jun 18 2008 APPayment20080618123001.csv
-rwxr----- 1 smartscm System 0 Jun 19 2008 APPayment20080619123000.csv
-rwxr----- 1 smartscm System 0 Jun 25 2008 APPayment20080625123001.csv
-rwxr----- 1 smartscm System 1961 Jun 30 2008 APPayment20080630123001.csv

How i can read the filename and size?
I want to read filename in loop so I can check the size condition and get the file from FTP accd. to condition.

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410

The awk utility is good for this. So are a number of other common system scripting utilities, including raw script programming and perl, but in this case, the awk solution is probably the simplest. You'd just set up a pattern to match only the "ls" output lines and then use awk's builtin field-parsing abilities to extract the values.

An IDE is no substitute for an Intelligent Developer.
Mirko Bonasorte
Ranch Hand

Joined: May 14, 2007
Posts: 244
The script accepts the file name as first argument.
It supposes that:
1. All the files are named *.csv
2. All the fields are splitted with a whitespace

[ July 31, 2008: Message edited by: Mirko Bonasorte ]

SCJP<br />SCWCD 1.4 Upgrade (Remember: me stupid)<br />SCWCD 1.4<br /><a href="" target="_blank" rel="nofollow">SCBCD 5.0</a><br /><a href="" target="_blank" rel="nofollow">SCBCD 5.0 mock exam</a> <br /> <br />SCEA 5 Part1: Preparing...
I agree. Here's the link:
subject: Read file contents in loop using shell script
It's not a secret anymore!