Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a new file from other files names using grep and cut

Status
Not open for further replies.

baggetta

Technical User
Feb 27, 2003
116
US
I want to create a new file name based on reading and grepping for a word in an existing file.

Example script:
#/bin/ksh
grep "*IUNB" file1.xxx.edw > r.out
cut -c 6-10 r.out > r.in

r.out = *IUNBCHERA ZZ FASJRIV Z
Z UNOA22003090911521002 '+:

r.in = CHERA

I would like the new file to be called:
"a-rivCHERA.edw" The extention of the new file needs to be the same as the source file in the inital grep command.
The "a-riv" can be hardcoded somewhere.


 
baggetta,
To create a blank file as described,
[blue]touch a-riv`cat r.in`.edw[/blue]

The a-riv part is hard-coded and the `cat r.in` piece pulls in the contents of r.in, which in this case is CHERA.

If you meant to rename r.in to the new file name, just use the mv command:
[blue]mv r.in a-riv`cat r.in`.edw[/blue]

-Hallux
 
thanks for the tip hallux, but one problem, the data in the new file is 0 bytes because of the touch command. I need to keep the data from the original file (t.edw). I tried to use the cat command after the touch but I'm not getting the results I need. Can you help me out futher?

This is what I have:
#/bin/ksh
grep "*IUNB" t.edw > r.out
cut -c 6-10 r.out > r.in
touch a-riv`cat r.in`.edw
 
Never mind, I see what you meant by using the mv command after it...

#/bin/ksh
grep "*IUNB" t.edw > r.out
cut -c 6-10 r.out > r.in
touch a-riv`cat r.in`.edw
mv t.edw a-riv`cat r.in`.edw

Is there any way of shortening this?
 
baggetta,
Yes it can be improved, shortened. I suggest submitting that question to the UNIX Scripting forum for help from people who write scripts for *NIX all of the time.
-Hallux
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top