to perform checks line by line on a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting to perform checks line by line on a file
# 1  
Old 08-28-2008
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  
Old 08-28-2008
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  
Old 08-28-2008
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  
Old 08-28-2008
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  
Old 08-28-2008
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  
Old 08-28-2008
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  
Old 08-28-2008
Thank you! Its solved Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

2. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

3. Shell Programming and Scripting

UNIX shell script - cut specified line and perform loop

Guys, I have a requirement as below. consider,if i use df command, its getting the below output. file system kbytes used avail %used Mounted on /dev/sample/ 45765 40000 5765 50% / /dev/filesys/ 30000 20000 1000 80% /u .... .... Now i wanted to cut the /u... (11 Replies)
Discussion started by: AraR87
11 Replies

4. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

5. Shell Programming and Scripting

Script to perform record format checks

Hi All, I have a requirement to perform the following checks. Input file is a "|" delimited file and looks like this. A|c1|c2|c3|.... B|G1|G2|G3.... C|H1|H2|H3... A|c4|c5|c6|.... B|G4|G5|G6.... C|H4|H5|H6... Now the check is to see if all the "A" records have a corresponding B... (7 Replies)
Discussion started by: gsjdrr
7 Replies

6. Shell Programming and Scripting

How to perform action on newest line in log using tail?

I don't quite know what I'm doing, so this simple script is proving a challenge. Here is some pseudo code that doesn't work yet: if tail -1 "WORKING.txt" >/dev/null | egrep "^NMBR=*" > /dev/null then curl -k 'http://www.myserver.com/log.cgi?input=$?' echo "hi there" fi Purpose:... (3 Replies)
Discussion started by: dihewidd
3 Replies

7. UNIX for Advanced & Expert Users

how do you parse 1 line at a time of file1 ie. line(n) each line into new file

File 1 <html>ta da....unique file name I want to give file=>343...</html> <html>da ta 234 </html> <html>pa da 542 </html> and so on... File 2 343 234 542 and so on, each line in File 1 one also corresponds with each line in File 2 I have tried several grep, sed, while .. read, do,... (4 Replies)
Discussion started by: web_developer
4 Replies

8. Shell Programming and Scripting

perform a check based on number of @ in a log line

Hello, I am intending to perform a check based on number of "@" , present in a line in a log file . The idea is basically to perform a check on cc or bcc sender, based on an email log, which shows all the for email address. Say if the number of @ is more than 30, I will consider it as a mass... (12 Replies)
Discussion started by: fed.linuxgossip
12 Replies

9. Shell Programming and Scripting

perform some checks on file using perl

hi i want check for PVCS header in file if its present then check if its in proper format or not i want to do this is in perl on windows. this is what i am doing : 1 . open file 2 . check for "PVCS information" if found then store the line no to $line var. 3 . check for "sccs" header ... (0 Replies)
Discussion started by: zedex
0 Replies
Login or Register to Ask a Question