Substitute File name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Substitute File name
# 8  
Old 03-13-2007
Quote:
Originally Posted by Deal_NoDeal
When you use:
zcat $var2* | grep "^000$var1" >> my_output

It is actually translated to something like this:

zcat 01* | grep ....

So, there is the issue.

If you know that your $var2 contains the exact file name, then remove the "*". Use

zcat $var2 | grep "^000$var1" >> my_output

You can also use the file extension to be safe i.e "zcat $var2.zip".

Or if $var2 doesn't contain the exact file name then try using double quotes around $var*. Like

zcat "$var2*" | grep "^000$var1" >> my_output

Hope this helps.


Dear friend
Already tried all these. But all in vain. Folder contains the files starting with 01.xxxx, 02.xxxxxfg etc some I have to use *. But all attempts fails. Any other Way to do.
# 9  
Old 03-13-2007
Quote:
Originally Posted by ranj@chn
First simplify your code removing unnecessary commands -
Code:
while read var1 var2 
do
zcat ${var2}* | grep "^000$var1" >> my_output
done <myfile

Check if it works. Didnt have time to simulate it. Smilie. Make sure you are in the same directory as the zip files. Otherwise, your 'myfile' should contain the path or you should hardcode the path.

Thanks for trying,
But it does not worked and gave the same error. I have already checked the file names and path etc. No mistake in it. But why it is not replacing zcat $var2* with zcat 01* is not understood. Some other suggestions pl.
Thanks
# 10  
Old 03-13-2007
what shell are you using. I am using bash and this syntax works
Code:
# var=01
# zcat ${var}*

zcat: 01test.gz: not in gzip format

As you can see, it is able to detect files starting with 01. Also, zcat will complain if your extension is not .gz ..
# 11  
Old 03-13-2007
Quote:
Originally Posted by ghostdog74
what shell are you using. I am using bash and this syntax works
Code:
# var=01
# zcat ${var}*

zcat: 01test.gz: not in gzip format

As you can see, it is able to detect files starting with 01. Also, zcat will complain if your extension is not .gz ..

dear friend
is is still not working
Unix Gurus please help me for this script.....
# 12  
Old 03-13-2007
show your code, error outputs and whatever info that's will help us to help you.
# 13  
Old 03-13-2007
File location

I simulated your error and the only time this error occurs is when there are no files of type .Z in the directory where I am searching for the *.Z files. The script that I ran was a part of your code.
Code:
/home/nayakr/ranj/unix_forum> cat chkfile.sh
while read var1 var2
do
zcat tmp/$var2* | grep 'junk' >>myoutfile
done <myfile

There was 'tmp' directory with no *.Z files.
Code:
/home/nayakr/ranj/unix_forum/tmp> ls -l
total 0
/home/nayakr/ranj/unix_forum/tmp>

The content of myfile was
Code:
/home/nayakr/ranj/unix_forum> cat myfile
pattern testfile

/home/nayakr/ranj/unix_forum>

Note that, the file contained a newline at the end. On running the script,
Code:
/home/nayakr/ranj/unix_forum> chkfile.sh
tmp/testfile*.Z: A file or directory in the path name does not exist.
tmp/*.Z: A file or directory in the path name does not exist.

Hope that answers your question.
# 14  
Old 03-13-2007
Quote:
Originally Posted by ghostdog74
show your code, error outputs and whatever info that's will help us to help you.

The code I am trying is :
cat myfile | awk '{print $1, $2}' | while read var1 var2
do
zcat $var2* | grep "^000$var1" >> my_output
done
where myfile contains data like :
1231231 01
1451515 02
I just want to replace the var2 i.e. 01 with the zip files (.Z files) starting with 01.abcdfef
01.hjjjkjhjh and so on and then grep the var1 from these files.
Error I am getting is :
*.Z : No such file or directory.
*.Z : No such file or directory.
*.Z : No such file or directory.
*.Z : No such file or directory.
.
.
.
.
I am not getting how to substitute the file names read as var2 of myfile. Please help...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to substitute a word in multiple file?

Team, I want to change below parameter in all the files in a directory, Check for HOSTNAME=`hostname` Change to HOSTNAME=localhost And I tried below but, its not working ☹ find /tmp -type f -exec sed 's/"HOSTNAME\=\`hostname\`"/"HOSTNAME\=localhost/g'" Help me if I am missing... (6 Replies)
Discussion started by: natraj005
6 Replies

2. UNIX for Beginners Questions & Answers

Substitute particular char in a file

I have a file like this. 1 aaa bcd 1 56 xyz 1 2 ccc rrr 1 25 512 1 1 zaz eee 1 55 511 1 I want to change middle 1's ie after bcd,rrr,eee to 0 where as other 1's should not change. Can you please provide a solution . (5 Replies)
Discussion started by: kshari8888
5 Replies

3. Shell Programming and Scripting

Substitute special Characters into a file

Hi experts :) I need to replace special characters into a file , in the followiing way : " --> "" ' --> '' _--> \_ I tried with the sed command but I'm getting and error ksh: $: not found. ksh: $: not found. sed: Function s/\/\/ cannot be parsed. Any idea ? Thanks , KOLAS... (2 Replies)
Discussion started by: Kolas79
2 Replies

4. UNIX for Dummies Questions & Answers

Help with AWK - Compare a field in a file to lookup file and substitute if only a match

I have the below 2 files: 1) Third field from file1.txt should be compared to the first field of lookup.txt. 2) If match found then third field, file1.txt should be substituted with the second field from lookup.txt. 3)Else just print the line from file1.txt. File1.txt:... (4 Replies)
Discussion started by: venalla_shine
4 Replies

5. Shell Programming and Scripting

Shell script: substitute value in text file

Hi gurus, I need help with shell script. I have various INSERT queries which inserts data into database. I want to insert 3rd column data into newline for one particular table. I have very time long txt file everytime and it have various INSERT/UPDATE queries but i have to done with it only one... (8 Replies)
Discussion started by: mirfan
8 Replies

6. Shell Programming and Scripting

How to substitute in a large gz file?

I have a large .gz file where i wish to replace the occurrence of a particular word with another word.. Can anyone help?I prefer a short script.. Can sed work with gz files? (5 Replies)
Discussion started by: maximus8886
5 Replies

7. Shell Programming and Scripting

Using awk to substitute columns from one file into another

Hi, I am new to this forum and my script skills are very weak. I have this file (file 1) that contains 3 columns (tab delimited): kyle start stop john start stop joe start stop And I want to replace name (column 1) from another file. This other file has two columns, one is the... (4 Replies)
Discussion started by: kylle345
4 Replies

8. UNIX for Dummies Questions & Answers

substitute file name

correct file names are: *_0.txt *_1.txt incorrect file names are: *_12.txt *_0123.txt *_04321.txt all files that are incorrect need to replace the ending with *_1.txt therefore need to create a loop to find the wrong files in a dir ->that dont end in _1.txt or _0.txt and then... (3 Replies)
Discussion started by: sigh2010
3 Replies

9. Shell Programming and Scripting

substitute a line in file

i have an file ,i want to substitute line 003 M 33 22 22 00 WITH NEW 003 M 24 26 28 00 how can i do it (2 Replies)
Discussion started by: RahulJoshi
2 Replies

10. Shell Programming and Scripting

how to substitute more than one word in a text file?

well i have this file here: <XML> <pregate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <system_c>HPREGATE</system_c> <trans_c>HSPG</trans_c> <trans_dt>20060105161333</trans_dt> <user_id_m></user_id_m> <func_c>C</func_c> </pregate> </XML> i want to... (2 Replies)
Discussion started by: forevercalz
2 Replies
Login or Register to Ask a Question