sed if statement to see if file exists


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed if statement to see if file exists
# 1  
Old 08-21-2013
sed if statement to see if file exists

Is there an easy way of checking for the existence of a file that ends with the extension .order and if it exists do something? if not do nothing
# 2  
Old 08-21-2013
Hello,

Could you please use the following command. This is just an example you can put this code in a script with .ksh extension and run it.

Code:
$ filename="something.order"
$ if [[ -e $filenwme ]]
> then
> echo "R. Singh"
> else
> echo "nothing"
> fi

As above code shows if file exist it will show R. Singh if doesn't then it will show nothing. Just an example for you.


Note: this I have ran as command line not as a script.

Thanks,
R. Singh
# 3  
Old 08-21-2013
hi thanks for the reply,

I need to find all files with the extension .order, so I tried the following:

Code:
#!/bin/bash
filename="*.order"
if [[ -e $filename ]]
then
echo "yes its there"
else
echo "no its not"
fi

it keeps returning its not there when the file is present.

Thanks

Paul
# 4  
Old 08-21-2013
try something like:
Code:
if [ -n "$(ls | grep "[.]order$")" ]
then
   echo found
else
   echo not found
fi

# 5  
Old 08-21-2013
Something similar...
Code:
if [ -n "$(ls *.bin  2>/dev/null)" ];then 
  echo "Okk..."
else
  echo "Oops"
fi

--ahamed
# 6  
Old 08-21-2013
Code:
#!/bin/bash
files=( *.order )

if (( "${#files[@]}" > 0 )); then
  ...
else
  ...
fi

for file in "${files[@]}"; do
  something with $file
done

# 7  
Old 08-21-2013
Code:
exists () {
  [ -e "$1" ]
}

filename=*.order 
if exists $filename; then
  echo yes
fi

--
With the external program ls:
Code:
filename=*.order 
if ls $filename >/dev/null 2>&1; then 
  echo hello
fi


Last edited by Scrutinizer; 08-21-2013 at 04:39 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed within awk statement

input | Jan 8 2018 11:28PM| 24 | 75 | 51 | 1 | 1.600| | Jan 8 2018 12:01PM| 52 | 823 | 21 | 6 | 2.675| desired output Jan-8-2018-11:28PM 24 75 51 1 1.600 Jan-8-2018-12:01PM 52 823 21 6 2.675 Dear friends, I have input file , as shown above and... (10 Replies)
Discussion started by: sagar_1986
10 Replies

2. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

3. Shell Programming and Scripting

sed to add word if pattern exists

Hi, I have a file with data listed below: clm_id double, cnl_id double, cnl_amt double I want output to be clm_id string, cnl_id string, cnl_amt double So replace double by string if the field contains the word _id I tried sed -e 's/_id/& string/' filename The above will not... (2 Replies)
Discussion started by: wahi80
2 Replies

4. Shell Programming and Scripting

If else statement in sed

Hello Guys, I am new here and this is my first post, hope someone can help me I am writing a script that is supposed to go in 9 different directories and edit a given file in each of the directories. I am using sed to edit the file as sed -i 'line# s/#to be changed/#to be replaced with/... (5 Replies)
Discussion started by: Madiouma Ndiaye
5 Replies

5. Shell Programming and Scripting

File exists, but cannot be opened.How to check- whether it could be opened to read when it exists

Hi #Testing for file existence if ; then echo 'SCHOOL data is available for processing' else echo 'SCHOOL DATA IS NOT AVAILABLE FOR PROCESSING' : i wrote a script, where it begins by checking if file exists or not. If it exists, it truncates the database... (2 Replies)
Discussion started by: rxg
2 Replies

6. UNIX for Dummies Questions & Answers

Statement to find if an entry exists in a file

I need to check if an entry input by the user is in a file. If so, I need to run a command, and if it does not exist then it should output entry does not exist. So I have so far... echo "Enter record:" read record //command || //command Can I use an if statement to do this? (3 Replies)
Discussion started by: itech4814
3 Replies

7. Shell Programming and Scripting

IF statement to check file exists

Hi All, If i run below copy command, it works absolutely fine, /opt/csw/bin/scp axetlxyz01:/opt/data/test/QURIES* ./input I want to make the above line better, by adding an IF statement, want to check if there is any file exists with name QURIES*.* then i need to copy that. if ... (7 Replies)
Discussion started by: rkrgarlapati
7 Replies

8. Shell Programming and Scripting

How to check if a file exists using the if statement

Hi, I'm trying to write a bit of code that will check if a file exists and then archives the file Im trying to use the following if statement without success.. if then mv filename archive/filename else echo "no filename exists" fi Should the file name be... (3 Replies)
Discussion started by: Jazmania
3 Replies

9. Shell Programming and Scripting

Variables within a sed statement

I am just wondering if it's possible to refer to variables within a sed statement as follows:- cat $file | sed -e 1's/$oldtext/$newtext/' > $file as when I run the script, the variables are not recognised and nothing happens..?? Thanks (5 Replies)
Discussion started by: sirtrancealot
5 Replies

10. Shell Programming and Scripting

if and sed statement

this is my output for my crawler. /about.html /ads/ /advanced_search?hl=en froogle.google.com/frghp?hl=en&tab=wf&ie=UTF-8 groups.google.com/grphp?hl=en&tab=wg&ie=UTF-8 /imghp?hl=en&tab=wi&ie=UTF-8 /intl/en/options/ /language_tools?hl=en /maphp?hl=en&tab=wl&ie=UTF-8... (3 Replies)
Discussion started by: chris1234
3 Replies
Login or Register to Ask a Question