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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

grep from file and rename 1

Status
Not open for further replies.

learningawk

Technical User
Oct 15, 2002
36
US
Could anyone give me a hint on how to use AWK to do the following?

I have a series of files that I want to grep out comment cards that begin with # and then write the output file name with a concatenated extension.

Example:
grep -v "#" input_file_name.dat > input_file_name_converted.dat

Is there also a way to do this in one script to do all the files in a directory that have a similar file_name pattern?

Such as convert all the files that have "input_file" or similar string located in the file name?

Thanks
 
Try this. It assumes only one period in the file names.

awk -f la.awk file1.dat file2.dat

# ------ la.awk ------
{
if (FNR==1) {
if (NR > 1) close(fn)
split (FILENAME,a,".")
fn = a[1] "_converted." a[2]
}
if ($0 !~ /^#/) print > fn
}
CaKiwi
 
CaKiwi,

Thanks very much. It works perfectly.
This is a great help.
I can adapt this to other uses also.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top