The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Problem with reading file line-by-line, and outputting to a new file Darkness Fish Shell Programming and Scripting 4 07-18-2008 07:54 AM
Need to serach if a new line character exists on the last line in a file sunilbm78 UNIX for Dummies Questions & Answers 10 02-29-2008 02:15 PM
perform some checks on file using perl zedex Shell Programming and Scripting 0 02-22-2008 03:01 PM
Appending the line number and a seperator to each line of a file ? pjcwhite Shell Programming and Scripting 4 03-21-2007 01:29 AM
Remove header(first line) and trailer(last line) in ANY given file madhunk Shell Programming and Scripting 2 03-13-2006 03:36 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-28-2008
muaz muaz is offline
Registered User
  
 

Join Date: May 2008
Posts: 14
to perform checks line by line on a file

Hi,

I have a file abc.txt with data like this
1 /test/
2 /test/file.txt
3 /data/
4 /data/file1.txt
5 /data/file2.txt
I want to take out every path from the file and check if its a directory or file.
I am trying it with cut with something like this but it doesnt work
while read chg_path
do
path=`cut -c5- $chg_path`
if [[ -d $path ]]
then
echo $path "directory"
else
echo $path "file"
fi
done < abc.txt
Please help me. Thanks in advance.
  #2 (permalink)  
Old 08-28-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Assuming the line numbers are not part of the file, you are removing the directory name but leaving a leading slash. This is probably not what you want. (Why would you want to remove the first directory component anyway?) The trivial fix is to use cut -c6- but I suspect you could get a quite different script if you told us why you are doing this and what you are ultimately trying to achieve.
  #3 (permalink)  
Old 08-28-2008
muaz muaz is offline
Registered User
  
 

Join Date: May 2008
Posts: 14
I guess easy way to solve this to find a / character on end of each line. if it is present I would like to treat the path as an input to another command in the check, otherwise ignore.
For example:
1 /test/ ignore
2 /test/file.txt take /test/file.txt to another command
3 /data/ ignore
4 /data/file1.txt take /data/file1.txt to another command
and so on..
Another command is same for all.
Path starts from 5th position in every line.
  #4 (permalink)  
Old 08-28-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
So the problem you are trying to figure out is how to skip the ones which end with a slash?


Code:
cut -c5- abc.txt |
while read path; do
  case $path in */) continue;; esac
  test -d "$path" && echo "$path" directory || echo "$path" not a directory
done

  #5 (permalink)  
Old 08-28-2008
palsevlohit_123 palsevlohit_123 is offline
Registered User
  
 

Join Date: Aug 2008
Location: India-Chennai
Posts: 120
PWD=`pwd`
for VAR in `awk '{printf "%s/%s\n","'$PWD'",$2}' FILENAME`
do
test -d "$VAR" && echo "$VAR" is a DIRECTORY || echo "$VAR" is a FILE
done
  #6 (permalink)  
Old 08-28-2008
dennis.jacob dennis.jacob is offline Forum Advisor  
dj - the student
  
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 611
Try this:


Code:
awk '{ print $2}'  filename | xargs ls -ld | awk ' { if (/^d/) print $NF " is a directory"; else print $NF " is a file"; }'

  #7 (permalink)  
Old 08-28-2008
muaz muaz is offline
Registered User
  
 

Join Date: May 2008
Posts: 14
Thank you! Its solved
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:54 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0