Variable not working in grep from script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable not working in grep from script
# 1  
Old 06-04-2014
Variable not working in grep from script

Hi,

If I hard code a value in the grep it works fine from script, when I use variable it doesn't work. On a seperate note, some lines (during testing) works fine from command line but not from scirpt.

Code:
#!/bin/bash
# Will fetch the (oldest - as ls will sort by name by default)Date in the DONE Filename (eg: DONE_20140530_1609)
# Will check for the same Date in all the files in that folder and move to a differnt folder when it matches the Date
#
a=`ls DONE*  | awk -F'/' '{print $NF}' | head -1|cut -c6-| sed 's/\r$//'`
echo $a
ls | grep "$a" # this line does't work
# After the above line, I want to move the files(result of above line) to different folder

Thanks
Krish
# 2  
Old 06-04-2014
Are the DONE* files in the same directory as your script? As that is where it is looking for them.
# 3  
Old 06-04-2014
Yes same directory.
# 4  
Old 06-04-2014
Few questions :
- Which OS are you working on, as I see you have used \r in sed, We only have \n. Furthermore, you dont need to remove that anyway. unlike perl.

- I dont see the absolute path with your filenames. Why are you splitting the path (the awk) and took the last field?

- Did you get the required value when you do echo $a?
# 5  
Old 06-04-2014
echo $a gives me the date part. I am using that Date to find in all filenames and move those files to different directory.

Just now I realised the script file I am using is dos format. Is it the cause of grep not working by any chance?
# 6  
Old 06-04-2014
Quote:
Originally Posted by krish.m
Just now I realised the script file I am using is dos format. Is it the cause of grep not working by any chance?
Yes. You can use dos2unix filename utility.

Also as I said, you dont need to explicitly remove the \ns from the variables.
# 7  
Old 06-04-2014
Quote:
Originally Posted by krish.m
Hi,

If I hard code a value in the grep it works fine from script, when I use variable it doesn't work. On a seperate note, some lines (during testing) works fine from command line but not from scirpt.

Code:
#!/bin/bash
# Will fetch the (oldest - as ls will sort by name by default)Date in the DONE Filename (eg: DONE_20140530_1609)
# Will check for the same Date in all the files in that folder and move to a differnt folder when it matches the Date
#
a=`ls DONE*  | awk -F'/' '{print $NF}' | head -1|cut -c6-| sed 's/\r$//'`
echo $a
ls | grep "$a" # this line does't work
# After the above line, I want to move the files(result of above line) to different folder

Thanks
Krish
The above script sets a to something like 20140530_1609, (not just the date 20140530). If I understand what you're trying to do, the following should work without needing to invoke ls (twice), awk, head, cut, sed, or grep:
Code:
#!/bin/ksh
IAm=${0##*/}
for i in DATE_*_*
do	date=${i%_*}
	date=${date#*_}
	if [ "$date" = "*" ]
	then	printf "%s: No files found to move.\n" "$IAm" >&2
		exit 1
	fi
	echo "Moving files for date: $date"
	echo mv DATE_${date}_* destination_directory
	exit
done

I usually use the Korn shell, but this script will also work with bash or any other shell that accepts basic POSIX Standard parameter expansion requirements.

You'll have to change destination_directory in the script above to be the pathname of the directory to which you want to move the files. After you have verified that the mv command it generates is what you want; remove the echo so it will actually move the files instead of just showing you the command it would use to move the files.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to grep variable in shell script?

Hi Team, I am trying to grep date in a file but it is not working #!/bin/bash d=`date "+ %Y%m%d %H:%M"` cd /scripts /bin/rm -f test1 cat /var/logs/File.txt.0 |grep $"d" >v.txt instead it is showing the complete file output. kindly suggest how should i grep this variable it works... (7 Replies)
Discussion started by: scriptor
7 Replies

2. Shell Programming and Scripting

Nslookup on a variable inside script not working

I am new at scripting. I have a file that each line is the shortname for a server. ie - Server1 Server2 Server3 I want to read in that file, do a nslookup and print the FQDN to a file. I added an echo statement to make sure my variable was being read and changing. But the nslookup... (5 Replies)
Discussion started by: kellyhkc
5 Replies

3. Shell Programming and Scripting

Grep with variable not working

Hello, I am using below code : export ORAR=orp712z.int.thomsonreuters.com #echo $ORAR; if echo $ORAR|grep -i "_" then ORACLE_SID1= echo $ORAR|cut -f2 -d "_" echo $ORACLE_SID1 ORACLE_SID=fgrep "$ORACLE_SID1" /etc/oratab|cut -f1 -d ":" #echo $ORACLE_SID else ORACLE_SID1= echo $ORAR|cut... (1 Reply)
Discussion started by: admin_db
1 Replies

4. Shell Programming and Scripting

Script not working in cron but working fine manually

Help. My script is working fine when executed manually but the cron seems not to catch up the command when registered. The script is as follow: #!/bin/sh for file in file_1.txt file_2.txt file_3.txt do awk '{ print "0" }' $file > tmp.tmp mv tmp.tmp $file done And the cron... (2 Replies)
Discussion started by: jasperux
2 Replies

5. UNIX for Dummies Questions & Answers

grep'ing a variable - why not working

Hi all, Am writing a ksh script where I am looking for processes that has gone defunct and all of which has the same PPID PID is the variable that I need to match as this is the process ID of the processes that has gone defunct Am just curious how come the following DOES NOT work? ps... (6 Replies)
Discussion started by: newbie_01
6 Replies

6. Shell Programming and Scripting

Issue with passing variable to Grep in a shell script

Hi, I'm trying to check if methods specified in a class have been added to the corrosponding interface. My code below is giving me the following errors: grep: function: No such file or directory grep: import($zipfile): No such file or directory grep: function: No such file or... (1 Reply)
Discussion started by: racshot65
1 Replies

7. UNIX for Dummies Questions & Answers

grep -f not working

Hello, I'm going crazy about this. I'm using grep to filter some values as in pas -ef | grep asterisk. When I use the same with -f somefile something weird happens, if somefile is created with vi it'll work, if somefile is created with vi but values are pasted from an Excell file it will not work.... (2 Replies)
Discussion started by: seveman
2 Replies

8. Shell Programming and Scripting

grep not working ????

Hi, I've prob in doing grep. I want to grep line staring with number 531250 in the 1st column from a file (example in picture attached below) using command grep -w "531250" file my ideal result should be 531250 1 21 42.1 100 1e-05 ... (8 Replies)
Discussion started by: masterpiece
8 Replies

9. Shell Programming and Scripting

Shell script / Grep / Awk to variable and Loop

Hi, I have a text file with data in that I wish to extract, assign to a variable and process through a loop. Kind of the process that I am after: 1: Grep the text file for the values. Currently using: cat /root/test.txt | grep TESTING= | awk -F"=" '{ a = $2 } {print a}' | sort -u ... (0 Replies)
Discussion started by: Spoonless
0 Replies

10. UNIX for Dummies Questions & Answers

grep not working

This condition is not able to grep , can any one tell what's wrong with this part. I am able to see from unix command but not with host script. echo "Checking for Loader Status " >> $REPFILE if test $? = 0 then echo "Successful termination of SQL*Loader "$LOADER1 >>... (5 Replies)
Discussion started by: u263066
5 Replies
Login or Register to Ask a Question