If variable contains a pathname then ACTION item


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers If variable contains a pathname then ACTION item
# 1  
Old 05-18-2012
If variable contains a pathname then ACTION item

I have an input file that contains a name of a file followed by the pathname like so

Code:
input1
/here/is/the/path/to/input1
input2
/here/is/the/path/to/input2
input3
/here/is/the/path/to/input3

I'm trying to write a bash script where if a path name is present, grep for a certain key phrase. I tried the following, but to no avail, it does not work.

Code:
#!/bin/sh
while read line
do
if [["$line" == /]] ; then
	grep "SEARCH PHRASE" $line
fi
done < infile > ???

As an added twist, I want to have the searched grep results replace the pathname ($line if it is a path). I know, it's pretty weird...
# 2  
Old 05-18-2012
Code:
while read line
do
  [ -f "$line" ] && grep "SEARCH PHASE" "$line" || echo "It is not an file"
done < infile

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 05-18-2012
Quote:
Originally Posted by tester213
As an added twist, I want to have the searched grep results replace the pathname ($line if it is a path). I know, it's pretty weird...
Replace it with what?
# 4  
Old 05-18-2012
Quote:
Originally Posted by itkamaraj
Code:
while read line
do
  [ -f "$line" ] && grep "SEARCH PHASE" "$line" || echo "It is not an file"
done < infile

very cool!

---------- Post updated at 10:28 AM ---------- Previous update was at 10:13 AM ----------

itakamaraj, can you briefly explain your code? Not sure where the -f switch is being used. I'm asking because for my grep statement, I'm using an OR like so
Code:
grep -E "PHRASE1 | PHRASE2" "$line"

but this no longer works when merged with your code.
# 5  
Old 05-18-2012
The -f checks if the file actually exists before running grep.

You have extra spaces in there which grep will attempt to match literally.

Code:
#wrong
grep -E "PHRASE1 | PHRASE2" "$line"
# right
grep -E "PHRASE1|PHRASE2" "$line"

Otherwise, I don't see anything wrong with it.

Code:
while read line
do
  [ -f "$line" ] && grep -E "a|b|c|d" "$line" || echo "It is not an file"
done < infile

# 6  
Old 05-18-2012
Quote:
Originally Posted by Corona688
The -f checks if the file actually exists before running grep.

You have extra spaces in there which grep will attempt to match literally.

Code:
#wrong
grep -E "PHRASE1 | PHRASE2" "$line"
# right
grep -E "PHRASE1|PHRASE2" "$line"

Otherwise, I don't see anything wrong with it.

Code:
while read line
do
  [ -f "$line" ] && grep -E "a|b|c|d" "$line" || echo "It is not an file"
done < infile

you're right. i had extra spaces. thanks!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Question on storing a variable from a rename action.

I have a simple if statement I am working with to validate if a file exist and rename it using the following method: if ;then mv .htaccess .htaccess.bak_$(date +%F-%T);fi;The output is just as it should be and I need to have that saved as a variable so that I can implement a revert changes... (3 Replies)
Discussion started by: moderhater
3 Replies

2. Shell Programming and Scripting

Command to get the pathname in variable

Hi, I want to saperate file path and file name into two variables. for ex:- lets say file is /home/prasoon/File.txt i want FILE_PATH=/home/prasoon and FILENAME=File.txt i need your help. Cheers Prasoon (2 Replies)
Discussion started by: prasson_ibm
2 Replies

3. UNIX for Dummies Questions & Answers

Why Do You Need the Explicit Pathname to Execute?

Hi! If your working directory contains a file you want to work on, or give as an argument, you don't have to give the explicit pathname, just the filename, like so: $ vi while_loop.ksh But if you want execute an executable file, you must supply the explicate pathname, like so: ./while_loop.ksh... (20 Replies)
Discussion started by: sudon't
20 Replies

4. Shell Programming and Scripting

SED - Pathname problem

Hello. After looking through the forum I am trying without success to remove lines from a file each time that ligne contains a pattern. As this is running in script, I am using some variables: The command : sed '/pattern/d' source_file > corrected_source_file In the script sed ... (2 Replies)
Discussion started by: jcdole
2 Replies

5. UNIX for Dummies Questions & Answers

finding pathname for directory

Hi Could someone help me? I'm not sure how to find the full pathname of a directory. I just want to be able to specify a directory. e.g directory1/directory2/directory3/directory4/directory5 I want to be able to put in "directory5" and then i want a return of the full address. ... (3 Replies)
Discussion started by: shomila_a
3 Replies

6. Shell Programming and Scripting

eliminate pathname from the file name in the o/p

Hi, Im finding some files form a specific path and den writing those files to another file as: find /SYS/admin/data/xml -name '*.xml' -type f ! -newer file1 -print >>out.xml and when im doing cat out.xml im getting like dis: ... (2 Replies)
Discussion started by: ss_ss
2 Replies

7. Shell Programming and Scripting

How to parse a pathname in csh?

I am wondering if there's an easy way to extract individual directories from a pathname in a csh script. For example, if I have a shell variable such as set datadirlist = /space/quimby/1 can I get 'space', 'quimby', and '1' and assign it individually to another variable? Any suggestions... (2 Replies)
Discussion started by: same1290
2 Replies

8. Shell Programming and Scripting

Getting pathname variables with ksh

With C Shell you can get the root, head, tail and extension of a pathname by using pathname variable modifiers. Example Script: #! /bin/csh set pathvar=/home/WSJ091305.txt echo $pathvar:r echo $pathvar:h echo $pathvar:t echo $pathvar:e The result of executing this script is: ... (7 Replies)
Discussion started by: BCarlson
7 Replies

9. UNIX for Dummies Questions & Answers

how to get the last dir from a pathname using IFS

I have a question, and don't know my way around with it. :( If I have a pathname say. i have the following pathname: /users/classA/tests how can I get only 'tests' out of that path. I know we have to use IFS=/ ... but next, i am clueless :confused: I also know that I can use... (2 Replies)
Discussion started by: sam2004
2 Replies

10. UNIX for Dummies Questions & Answers

find without pathname

How can I get the results of a find back without the pathname for example if i do find ../../ -name \*.sql i dont want to see directory/directory/filename.sql I only want to see filename.sql (3 Replies)
Discussion started by: MBGPS
3 Replies
Login or Register to Ask a Question