How to create a file using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to create a file using awk
# 1  
Old 04-18-2010
How to create a file using awk

Hi there to the community,
i have a bash script with an awk command inside, the awk program is in a separate file cause it's a bit big.
Inside the awk program i calculate a filename from some data and i want to create a new file with this name into a directory whose path is passed in awk.

Etc. lets say that the awk command in the script looks like this :
Code:
awk -v target=$dir1/$dir2 -f inputfile

what i need to do is create (inside the awk program) a file called 'filename' under the target.
I 've been trying to do this with touch(), i know that system commands do not work in awk so i had to call touch() through system() to get it done.
So i am using
Code:
system ("touch "target/filename)

, but it doesn't work. Not that is generates an error, it simply does nothing.
If i write
Code:
system ("touch "filename)

the file is being created but the thing is that i want to create it in the directory pointed by target.
Any help? Smilie
# 2  
Old 04-18-2010
Quote:
Originally Posted by beatblaster666
...
So i am using
Code:
system ("touch "target/filename)

, but it doesn't work.
...
Maybe something like this ?

Code:
system ("touch "target"/filename")

tyler_durden
# 3  
Old 04-18-2010
Quote:
Originally Posted by durden_tyler
Maybe something like this ?

Code:
system ("touch "target"/filename")

tyler_durden

Hm this one creates a file under target but it's name is filename and not what the string variable filename is equal to. Any idea?
# 4  
Old 04-19-2010
Quote:
Originally Posted by beatblaster666
Hm this one creates a file under target but it's name is filename and not what the string variable filename is equal to. Any idea?
Seems like I misread your first post:

Quote:
...what i need to do is create (inside the awk program) a file called 'filename' under the target.
...
Anyway, try this -

Code:
system ("touch "target"/"filename)

which should create a file with the name that equals the value of the variable "filename", in the directory with the name that equals the value of the variable "target".

tyler_durden
# 5  
Old 04-19-2010
Quote:
Originally Posted by durden_tyler
Seems like I misread your first post:



Anyway, try this -

Code:
system ("touch "target"/"filename)

which should create a file with the name that equals the value of the variable "filename", in the directory with the name that equals the value of the variable "target".

tyler_durden
Found another way by using :
Code:
path=sprintf("%s%s%s",target,"/",filename);
system("touch "path"");

A bit dummy but really made an effort!Smilie
Yours works like a charm also, thanks a lot!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk and sed script to create one output CSV file

Hi All , I would require your help to generate one output file after post processing of one CSV file as stated below This file is just a small cut from a big file . Big file is having 20000 lines PATTERN,pat0,pat1,pat2,pat3,pat4,pat5,pat6,pat7,pat8,pat9... (2 Replies)
Discussion started by: kshitij
2 Replies

2. Shell Programming and Scripting

Splitting a text file into smaller files with awk, how to create a different name for each new file

Hello, I have some large text files that look like, putrescine Mrv1583 01041713302D 6 5 0 0 0 0 999 V2000 2.0928 -0.2063 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0 5.6650 0.2063 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0 3.5217 ... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

3. Shell Programming and Scripting

awk to create variables to pass into a bash loop to create a download link

I have created one file that contains all the necessary info in it to create a download link. In each of the lines /results/analysis/output/Home/Auto_user_S5-00580-6-Medexome_67_032/plugin_out/FileExporter_out.67... (8 Replies)
Discussion started by: cmccabe
8 Replies

4. Shell Programming and Scripting

awk parsing file to create a database

Hi Guys, I have a list a hotels stored in many different text files. This list is kept in the following format: 20/03 Hotel: The Bear Hotel Honey Street Woodstock UK Tel:+44-xxxxxx Rate: 100 21/03 Hotel: The Bush Hotel Nice Street Farnham (4 Replies)
Discussion started by: freddie50
4 Replies

5. Shell Programming and Scripting

Awk: from two file create a mail with attached error

Hi. My name is Mirko I have two file. File 1: mio@mio.it tst@test.com bye@bye.fr File 2: error 08 ffff mio@mio.it test error 05 ffff bye@bye.fr test error 11 ffff tst@test.com test error 65 ffff mio@mio.it test error 55 ffff bye@bye.fr test Examples I would like to send a mail... (2 Replies)
Discussion started by: Gionfalco01
2 Replies

6. Shell Programming and Scripting

Using awk to create a summary of a structured file

I am trying to use awk to create a summary of a structured file. Here is what it looks like: (random text) H1 H2 H3 H4 44 78 99 30 31 -- 32 21 12 33 55 21 I'd like to be able to specify a column, say H2, and then have information about that column printed. ... (4 Replies)
Discussion started by: afulldevnull
4 Replies

7. Shell Programming and Scripting

Create ranges on a file with awk

Hello all, Let's say we have this file : $ cat inputfile 2 3 4 7 8 11 15 16 17 I'm trying to make a script with awk which gives as output the following : (4 Replies)
Discussion started by: rany1
4 Replies

8. Shell Programming and Scripting

generating a create ddl from a csv file using awk

Hello, I would greatly appreciate some help on the this I have comma delimited file as follows: csv file -------- TEST1,fld1,VARCHAR2,3,,, TEST1,fld2,DATE,,,, TEST1,fld2,VARCHAR2,51,,, TEST1,fld4,VARCHAR2,2,,, TEST1,fld5,NUMBER,4,0,, TEST1,fld6,VARCHAR2,1,,,... (5 Replies)
Discussion started by: jville
5 Replies

9. Shell Programming and Scripting

Select some lines from a txt file and create a new file with awk

Hi there, I have a text file with several colums separated by "|;#" I need to search the file extracting all columns starting with the value of "1" or "2" saving in a separate file just the first 7 columns of each row maching the criteria, with replacement of the saparators in the nearly created... (4 Replies)
Discussion started by: capnino
4 Replies

10. Shell Programming and Scripting

Read a file and search a value in another file create third file using AWK

Hi, I have two files with the format shown below. I need to read first field(value before comma) from file 1 and search for a record in file 2 that has the same value in the field "KEY=" and write the complete record of file 2 with corresponding field 2 of the first file in to result file. ... (11 Replies)
Discussion started by: King Kalyan
11 Replies
Login or Register to Ask a Question