Unable to do grep in a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unable to do grep in a script
# 1  
Old 03-22-2017
Unable to do grep in a script

Hi,

I am trying to grep a filename from a script after taking the file name and other variables as keyboard input .When I run the grep command with the same filename on the prompt, it runs fine, but it is either not giving me the correct output or not running at all from the script using the same grep command.

from the prompt:
Code:
$/bin/grep 20170219_HMS_80Byte_PAYbig3_1_4.prc /backup/omnftppasrv/bw3/201702/bw3-syst_INST19-201702[0-9][0-9].lst

pasting snippet of my script code

Code:
#building regex for date searching
regex=$yr_mnth\[0-9\]\[0-9\]

#building date searching regex list file
xtr_regex=$xtr_flptrn1$inst_nmb-$regex.$list_file

cmptl_extr_list_file=$xfile_pth/$xtr_regex	#storing complete file name with path into a variable

#echo $cmptl_extr_list_file

#grepping the filename from list file
/bin/grep '$flnm_80BYTE' $cmptl_extr_list_file

experts need your advice on this please
# 2  
Old 03-22-2017
This is quite sparse a specification which doesn't necessarily encourage people to offer you help. At first sight, the single quotes prevent the $flnm_80BYTE from being expanded.
# 3  
Old 03-22-2017
Quote:
Originally Posted by RudiC
This is quite sparse a specification which doesn't necessarily encourage people to offer you help. At first sight, the single quotes prevent the $flnm_80BYTE from being expanded.
sorry about that, totally missed mentioning I am on AIX and I had tried it first with double quotes and with no luck, I just tried it with single quotes, but no luck again.

Please let me know any other info required and Ill promptly reply
# 4  
Old 03-22-2017
Code:
#!/bin/ksh
set -x
#building regex for date searching
regex="${yr_mnth}[0-9][0-9]"
# where's $yr_mnt defined?

#building date searching regex list file
xtr_regex="${xtr_flptrn1}${inst_nmb}-${regex}.${list_file}"
#where are xtr_flptrn1  nst_nmb list_file defined?
# placing {} for possible variable names - validate

 cmptl_extr_list_file="${xfile_pth}/${xtr_regex}"  #storing complete file name with path into a variable

#echo $cmptl_extr_list_file

#grepping the filename from list file
/bin/grep '$flnm_80BYTE' $cmptl_extr_list_file
# as noted don't use single quotes - they will hide the variable expansion 
# /bin/grep "${flnm_80BYTE}" ${cmptl_extr_list_file}"

edited code (edits/questions in red) - for possible variable name spec changes - validate the placement of {}'s
Enabled set -x to aid in debugging

Last edited by vgersh99; 03-22-2017 at 11:06 AM..
# 5  
Old 03-22-2017
Hi, I am pasting the complete code meanwhile I am editing the script accoding to vgersh99's advise. Hope, I am able to make some sense. Please let me know if you need any other information

Code:
#!/bin/bash

#static value assignment to a variable

list_file=lst	   #taking a variable into extraction list file

enc_tar_xtnsn=tar.gz.enc	#taking a variable into extraction encrypted file 

xtr_flptrn1=bw3-syst_INST 	#taking a variable into extraction file path

#main_scp=/usr/local/scripts/omniadm/omnftppasrv/extract.sh  #main extraction script

echo "Enter file processing date [YYYYMMDD]"
read file_x_date       

echo "Inst nu:"
read inst_nmb      

#breaking 80byte_flnm into YYYYMM and DD

yr_mnth=`echo "${file_x_date}"|awk '{print substr($0,1,6)}'`

only_dt=`echo "${file_x_date}"|awk '{print substr($0,7,8)}'`

#variable value assigned dynamically based on static value assignemnt above

xtr_flnm_list="${xtr_flptrn1}""${inst_nmb}"-"{$file_x_date}"\."{$list_file}"    #taking a variable into extraction file name

#---echo $xtr_flnm_list

xfile_pth=/backup/omnftppasrv/bw3/"${yr_mnth}"  # storing extraction file path into a variable

#---echo $xfile_pth

enc_tar_filename="${xtr_flptrn1}""${inst_nmb}"-"${file_x_date}"\."${enc_tar_xtnsn}"

#---echo $enc_tar_filename

#cmptl_extr_list_file=$xfile_pth/$xtr_flnm_list	#storing complete file name with path into a variable

#echo $cmptl_extr_list_file

cmptl_extr_enc_file="${xfile_pth}"/"${enc_tar_filename}"

echo $cmptl_extr_enc_file

echo "filename to extract"
read flnm_80byte    

	if [[ "$flnm_80byte" =~ .*\.prc ]]
	then
		echo "correct file name"
	elif [[ "$flnm_80byte" =~ .*\.inc ]]
	  then
		  strppd_flname=`echo $flnm_80byte|awk '{print substr($0, 1, length($0) - 4)}'`
		  flnm_80BYTE=$strppd_flname\.prc
		  echo $flnm_80BYTE
	  else
		  flnm_80BYTE=$flnm_80byte\.prc
		  echo $flnm_80BYTE
	  
	fi

#building regex for date searching
regex="{$yr_mnth}"\[0-9\]\[0-9\]

#building date searching regex list file
xtr_regex="${xtr_flptrn1}${inst_nmb}-${regex}\.${list_file}"

cmptl_extr_list_file="${xfile_pth}"/"${xtr_regex}"	#storing complete file name with path into a variable

echo $cmptl_extr_list_file

#grepping the filename from list file
/bin/grep "${flnm_80BYTE}" "${cmptl_extr_list_file}"

# 6  
Old 03-22-2017
add set -x at the beginning of the script and see the output of the execution - follow the script's variable expansion and the execution path.
# 7  
Old 03-22-2017
Hi, I have updated the script to ksh and added the set -x. Thanks a lot for that. However still facing errrors, as expected Smilie. I have pasted the output also

your advice please

Code:
#!/bin/ksh

set -x

#static value assignment to a variable

list_file=lst	   #taking a variable into extraction list file

enc_tar_xtnsn=tar.gz.enc	#taking a variable into extraction encrypted file 

xtr_flptrn1=bw3-syst_INST 	#taking a variable into extraction file path

#main_scp=/usr/local/scripts/omniadm/omnftppasrv/extract.sh  #main extraction script

echo "Enter file processing date [YYYYMMDD]"
read file_x_date       

echo "Inst nu:"
read inst_nmb      

#breaking 80byte_flnm into YYYYMM and DD

yr_mnth=`echo "${file_x_date}"|awk '{print substr($0,1,6)}'`

only_dt=`echo "${file_x_date}"|awk '{print substr($0,7,8)}'`

#variable value assigned dynamically based on static value assignemnt above

xtr_flnm_list="${xtr_flptrn1}""${inst_nmb}"-"{$file_x_date}"."{$list_file}"    #taking a variable into extraction file name

#---echo $xtr_flnm_list

xfile_pth=/backup/omnftppasrv/bw3/"${yr_mnth}"  # storing extraction file path into a variable

#---echo $xfile_pth

enc_tar_filename="${xtr_flptrn1}""${inst_nmb}"-"${file_x_date}"."${enc_tar_xtnsn}"

#---echo $enc_tar_filename

#cmptl_extr_list_file=$xfile_pth/$xtr_flnm_list	#storing complete file name with path into a variable

#echo $cmptl_extr_list_file

cmptl_extr_enc_file="${xfile_pth}"/"${enc_tar_filename}"

echo $cmptl_extr_enc_file

echo "filename to extract"
read flnm_80byte    

	if [[ "$flnm_80byte" =~ .*\.prc ]]
	then
		echo "correct file name"
	elif [[ "$flnm_80byte" =~ .*\.inc ]]
	  then
		  strppd_flname=`echo $flnm_80byte|awk '{print substr($0, 1, length($0) - 4)}'`
		  flnm_80BYTE=$strppd_flname\.prc
		  echo $flnm_80BYTE
	  else
		  flnm_80BYTE=$flnm_80byte\.prc
		  echo $flnm_80BYTE
	  
	fi

#building regex for date searching
regex="{$yr_mnth}"\[0-9\]\[0-9\]

#building date searching regex list file
xtr_regex="${xtr_flptrn1}${inst_nmb}-${regex}\.${list_file}"

cmptl_extr_list_file="${xfile_pth}"/"${xtr_regex}"	#storing complete file name with path into a variable

echo $cmptl_extr_list_file

#grepping the filename from list file
/bin/grep "${flnm_80BYTE}" "${cmptl_extr_list_file}"

Code:
./extract80byte.sh
+ list_file=lst
+ enc_tar_xtnsn=tar.gz.enc
+ xtr_flptrn1=bw3-syst_INST
+ echo Enter file processing date [YYYYMMDD]
Enter file processing date [YYYYMMDD]
+ read file_x_date
20170219
+ echo Inst nu:
Inst nu:
+ read inst_nmb
19
+ + awk {print substr($0,1,6)}
+ echo 20170219
yr_mnth=201702
+ + echo 20170219
+ awk {print substr($0,7,8)}
only_dt=19
+ xtr_flnm_list=bw3-syst_INST19-{20170219}.{lst}
+ xfile_pth=/backup/omnftppasrv/bw3/201702
+ enc_tar_filename=bw3-syst_INST19-20170219.tar.gz.enc
+ cmptl_extr_enc_file=/backup/omnftppasrv/bw3/201702/bw3-syst_INST19-20170219.tar.gz.enc
+ echo /backup/omnftppasrv/bw3/201702/bw3-syst_INST19-20170219.tar.gz.enc
/backup/omnftppasrv/bw3/201702/bw3-syst_INST19-20170219.tar.gz.enc
+ echo filename to extract
filename to extract
+ read flnm_80byte
20170216_HMS_80Byte_PAYbig3_1_6.inc
./extract80byte.sh[52]: syntax error at line 52 : `=~' unexpected

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unable to grep using wildcard in a file.

I wish to check if my file has a line that does not start with '#' and has 1. Listen and 2. 443 echo "Listen 443" > test.out grep 'Listen *443' test.out | grep -v '#' Listen 443 The above worked fine but when the entry changes to the below the grep fails... (2 Replies)
Discussion started by: mohtashims
2 Replies

2. Shell Programming and Scripting

Unable to grep the process

I have user1 run a script called logginexpert.sh while has this line of code sleep 888I then login to another putty session with another user2 and try to grep for the logginexpert.sh process using ps -ef | grep exSunOS mymac 5.11 11.2 sun4u sparc SUNW,SPARC-Enterprise But, i dont get any... (20 Replies)
Discussion started by: mohtashims
20 Replies

3. UNIX for Dummies Questions & Answers

Unable to grep

I have a file with 2 lines of code Rome is in Romeo Romeo is in Rome How do I grep, so that only last line would be the outcome. sample output Romeo is in Rome I have tried with all possible greps but its resulting in both the lines in output. Please help. (6 Replies)
Discussion started by: gotamp
6 Replies

4. UNIX for Dummies Questions & Answers

unable to grep the reqd field

hi all, i have a data sm thg like this 28504 0 abc 148782859 42 101M nhmmmm ilopo abc 2345432 i want to get only the field which is just aftr abc i,e., 148782859, 2345432 i have used grep /abc\t/ filename to get that but its not working can any 1 help me out (5 Replies)
Discussion started by: anurupa777
5 Replies

5. Shell Programming and Scripting

unable to match grep pattern

Hi All, I am trying to select all files in a directory which are not with .gz extension . for which I am using below script , but its rejecting both .gz and .z extension files,as in each letter is considered separately. PFB ls -lrt | awk '{print $9}'| egrep "^IRAMS.*$" please suggest... (1 Reply)
Discussion started by: Jcpratap
1 Replies

6. Shell Programming and Scripting

unable to grep the running processes.

Hi All, We have a shell script(ODS_Load)which loads the data from perticular flat file to oracle table invoking sqlplus based on the parameter. When we execute the script(ODS_Load) independently, script is working fine and able to load the tables successfully. We invoke(ODS_Load) with... (1 Reply)
Discussion started by: Nagaraja Akkiva
1 Replies

7. Shell Programming and Scripting

Unable to grep control/non printable characters

Unable to grep: Able to grep: (11 Replies)
Discussion started by: proactiveaditya
11 Replies

8. Shell Programming and Scripting

Log file is not getting created & unable to grep error from it

Hi All, Below is my code,what I am trying to do is redirecting output of ftp to a log file & then greping the errors but here I am unable to grep "Permission denied" error only & also the corresponding log file is also not getting created. #!/bin/sh . cfg USER='abc' PASSWD='abc123' ... (4 Replies)
Discussion started by: ss_ss
4 Replies

9. Shell Programming and Scripting

grep or awk problem, unable to extract numbers

Hi, I've trouble getting some numbers from a html-file. The thing is that I have several html-logs that contains lines like this: nerdnerd, how_old_r_u:45782<br>APPLY: <hour_second> Verification succeded This is some of what I've extracted from a html file but all I really want is the number... (7 Replies)
Discussion started by: baghera
7 Replies

10. Shell Programming and Scripting

unable to grep the following pattern

I have the following line in file1 elif ; then now if i try to grep this using following command grep -e "elif ; then" file1 it is showing nothing... how to grep such patterns (2 Replies)
Discussion started by: suri
2 Replies
Login or Register to Ask a Question