Can I create 3 files in one command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can I create 3 files in one command
# 1  
Old 07-07-2015
Can I create 3 files in one command

Hi Gurus,

I need to separate a file to 3 (based on column 1 value). I have code like below. it works.
Code:
awk -F"|" '$1=="H" {print $0}' ${filename} > ${hea_name}
awk -F"|" '$1=="D" {print $0}' ${filename} > ${det_name}
awk -F"|" '$1=="T" {print $0}' ${filename} > ${tra_name}

is it possible to run awk once then create 3 files?

Code:
 
awk -F"|" '{if($1=="H") var="Hea"; else if ($1=="D") var="Det"; else var="Tra"; print $0, var}' file

I can get the value, but I don't know how to put the value in the output file name.
# 2  
Old 07-07-2015
Code:
awk -F"|" -v head_name="$head_name" -v det_name="$det_name" -v tra_name="$tra_name"  '
    $1=="H" {print > head_name}
    $1=="D" {print > det_name}
    $1=="T" {print > tra_name}
' ${filename}

I would placed a next after every successful match, so it doesn't have to evaluate the $1 three times when it is already done with that line. Like:
Code:
$1=="H" {print > head_name; next}


Last edited by Aia; 07-07-2015 at 03:13 PM..
This User Gave Thanks to Aia For This Post:
# 3  
Old 07-07-2015
Or pretty similar to your attempt
Code:
awk -F"|" '{if($1=="H") var="Hea"; else if ($1=="D") var="Det"; else var="Tra"; print $0 > var}' file

This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 07-07-2015
Assuming that the first field will always be exactly one of those three characters, you could also try something like:
Code:
awk -F'|' '{print > ($1=="D"?d:$1=="H"?h:t)}' d="$det_name" h="$hea_name" t="$tra_name" file


Last edited by Don Cragun; 07-09-2015 at 04:37 AM.. Reason: Add missing single and double quotes
These 2 Users Gave Thanks to Don Cragun For This Post:
# 5  
Old 07-08-2015
Thanks for all of you give me very good suggestion as always.
# 6  
Old 07-09-2015
Somemore way
Code:
awk -F"|" -v head_name="$head_name" -v det_name="$det_name" -v tra_name="$tra_name" '
BEGIN{
	A["h"]=head_name
  	A["d"]=det_name
	A["T"]=tra_name
} 
$1 in A{
	print >A[$1]
	next
}' ${filename}

OR make a file which contains char to be matched and corresponding filename, something like this

Code:
[akshay@localhost tmp]$ cat search_list
h head_name
d det_name
T tra_name
a someother_name1
b someother_name2
c someother_name3

Code:
awk '
FNR==NR{
	A[$1]=$2
	next
}
$1 in A{
	print >A[$1]
	next
}' search_list FS="|" ${filename}

# 7  
Old 07-09-2015
Quote:
Originally Posted by Don Cragun
Assuming that the first field will always be exactly one of those three characters, you could also try something like:
Code:
awk -F'|' {print > ($1==D?d:$1==H?h:t)}' d="$det_name" h="$hea_name" t="$tra_name" file


As much as I appreciate this smart an compact solution, it has some minor drawbacks:
- the leading single quote is missing for the script part
- the "D" and "H" need double quote to become string constands else they're (empty) variables
- ALL input lines not beginning with "D" or "H" will be stuffed into the tra_name file, which is not what was requested in post#1 (acknowledged that Don Cragun assumed only D, H, T)

Try this small adaption of Don Cragun's snippet:
Code:
awk -F'|' '{print > ($1=="D"?d:$1=="H"?h:$1=="T"?t:"/dev/null")}' d="det_name" h="hea_name" t="tra_name" file

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to create or convert to pdf files from csv files using shell script?

Hi, Can anyone help me how to convert a .csv file to a .pdf file using shell script Thanks (2 Replies)
Discussion started by: ssk250
2 Replies

2. Shell Programming and Scripting

Short command to create two files >{respo,nd}.php (with "tee" command?)

08:29 < xsi> >{respo,nd}.php bash: {respo,nd}.php: ambiguous redirect 08:31 < geirha> xsi: maybe you want tee So I was advised to do so. And I can't create two OR MORE files at once with {a,b,c,d,e,f}.php (which I quickly now need to create and to learn to create in the future to quickly... (2 Replies)
Discussion started by: Xcislav
2 Replies

3. Shell Programming and Scripting

How to create zip/gz/tar files for if the files are older than particular days in UNIX or Linux?

I need a script file for backup (zip or tar or gz) of old log files in our unix server (causing the space problem). Could you please help me to create the zip or gz files for each log files in current directory and sub-directories also? I found one command which is to create gz file for the... (4 Replies)
Discussion started by: Mallikgm
4 Replies

4. Shell Programming and Scripting

Single command to create multiple empty files(no trailing lines as well).

Hi, i need a single command to create multiple empty files(no trailing lines as well) and empty the files if already existing. please let me know or if this has been ansered, if some ocan share the link please, thanks > newfile.txt or :> newfile.txt do not work (4 Replies)
Discussion started by: Onkar Banerjee
4 Replies

5. Solaris

Command to list all header files used to create an executable

All, I would like to know if there is a command which will list all the header files which were used while creating the executable. (1 Reply)
Discussion started by: helper
1 Replies

6. UNIX for Dummies Questions & Answers

how to create a command that would print files only from 1 folder

Hi there how to create a command in csh that would print files only from 1 folder but as an argument takes home directory for e.g. in my home diecrtory I have 3 folders unix, windows,mac and alot of files. and I running the program as ./op ~ this should return me files from unix folder without... (1 Reply)
Discussion started by: FUTURE_EINSTEIN
1 Replies

7. UNIX for Dummies Questions & Answers

Create individual tgz files from a set of files

Hello I have a ton of files in a directory of the format app.log.2008-04-04 I'd like to run a command that would archive each of these files as app.log.2008-04-04.tgz I tried a few combinations of find with xargs etc but no luck. Thanks Amit (4 Replies)
Discussion started by: amitg
4 Replies

8. Shell Programming and Scripting

Create files with one command

I whant a create some files in the certain range. For example, begin 8811 and finishing 8878 I know one good decision touch 88{11,12,13,14,15,16,...,...,78} I try touch 88, but i don't have results :( Bash create file with name 88 :) But it very long command :( I wanna create it easy ... (6 Replies)
Discussion started by: jess_t03
6 Replies

9. UNIX for Dummies Questions & Answers

Creating create mirrored disk files systems using mkfs command

What is the correct mkfs syntax to create mirrored disk files systems? I need to make the file system 20gb. For example: machine# mkfs -F ufs /dev/md/dsk/d40 size not specified ufs usage: mkfs special size(sectors) \ -m : dump fs cmd line used to make this partition -V : print this... (4 Replies)
Discussion started by: GLJ@USC
4 Replies
Login or Register to Ask a Question