awk to reformat output if input file is empty, but not if file has data in it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to reformat output if input file is empty, but not if file has data in it
# 1  
Old 03-20-2017
awk to reformat output if input file is empty, but not if file has data in it

The below awk improved bu @MadeInGermany, works great as long as the input file has data in it in the below format:

input
Code:
chrX	25031028	25031925	chrX:25031028-25031925	ARX	631	18
chrX	25031028	25031925	chrX:25031028-25031925	ARX	632	14
chrX	25031028	25031925	chrX:25031028-25031925	ARX	633	14
chrX	25031028	25031925	chrX:25031028-25031925	ARX	634	13
chrX	25031028	25031925	chrX:25031028-25031925	ARX	635	12

awk
Code:
awk '
# print from stored values
function prt(){
  print p1 ":" (p6start==1 ? p2 : p2+p6start) "-" p2+p6, "\t" p5
}
($4!=p4 || $6!=p6+1) {
# new sequence, print the previous sequence
  if (NR>1) prt()
  p6start=$6  
}
{
# store the values that we need later
  p1=$1
  p2=$2
  p4=$4
  p5=$5
  p6=$6
}
END { prt() }
' input | awk -F"[:-]" ' { print $1 "\t" $2 "\t" $3 "\t" $4}' > out

out
Code:
chrX	25031659	25031663 	ARX

However, when input is an empty file:

out
Code:
	0	0

I am trying to put a condition in the awk that will check for file being empty or 0 bytes and if file is not empty then the output remains the same, but if file is empty then the output is reformatted to the desired output of 4 zeros, tab-delimited. My attempt to do this is in the # check if empty below in bold. Since the process after this awk is expecting 4 tab-delimited fields I need to add this check. Thank you Smilie.

Code:
awk '
  # print from stored values
  function prt(){
    print p1 ":" (p6start==1 ? p2 : p2+p6start) "-" p2+p6, "\t" p5
  }
  ($4!=p4 || $6!=p6+1) {
    # new sequence, print the previous sequence
    if (NR>1) prt()
    p6start=$6  
  }
  {
    # store the values that we need later
    p1=$1
    p2=$2
    p4=$4
    p5=$5
    p6=$6
  }
  END { prt() }
' file | awk -F"[:-]" '
  { print $1 "\t" $2 "\t" $3 "\t" $4}
' > out # make low coverage
# check if empty
if [ -s aFile ]; then  
else  
  output={ print "0     0     0     0" }
fi 

desired out ---- four zeros separated by tabs
Code:
0	0	0     0


Last edited by cmccabe; 03-20-2017 at 03:41 PM.. Reason: fixed format, added details
# 2  
Old 03-20-2017
What do you mean by an empty file...a file with size zero bytes or one that is filled with zeros?
This User Gave Thanks to shamrock For This Post:
# 3  
Old 03-20-2017
Sorry, a file that is zero bytes, I also updated the post... Thank you Smilie.
# 4  
Old 03-20-2017
Hello cmccabe,

It will not work because -s Input_file test condition inside if will check if Input_file is having size more than ZERO. As follows from man test.
Quote:
-s FILE
FILE exists and has a size greater than zero
So if you want to do an action when Input_file is having size 0 then you could change it to as follows.
Code:
if [[ ! -s Input_file ]]
then
       echo -e "0\t0\t0\t0"
else
       echo "Input_file is having size.
fi

Thanks,
R. Singh
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Redirect output to the same input file in awk

Hi, I want to compare a value from test file and redirect the o/p value to the same file input file 250 32000 32 128 Below is my code awk '{ if ($1 < "300") print $1 > /tmp/test}' test want to compare 250 < 300 then print 300 to the same place below is the... (24 Replies)
Discussion started by: stew
24 Replies

2. UNIX for Dummies Questions & Answers

awk - Rename output file, after processing, same as input file

I have one input file ABC.txt and one output DEF.txt. After the ABC is processed and created output, I want to rename ABC.txt to ABC.orig and DEF to ABC.txt. Currently when I am doing this, it does not process the input file as it cannot read and write to the same file. How can I achieve this? ... (12 Replies)
Discussion started by: High-T
12 Replies

3. Shell Programming and Scripting

Using awk to reformat file output

Hi there. I need to reformat a large file. Here is a sample of the file. NETIK0102_UCS_Boot_a,NETIK0102_UCS_Boot_b 5200 2438 70G 5200 2439 70G NETIK0102_UCS_HBA0_a,NETIK0102_UCS_HBA1_b,NETIK0102_UCS_HBA2_a,NETIK0102_UCS_HBA3_b 2673 19D7 55G 2673 19C0 30G 2673 19F5 120G... (5 Replies)
Discussion started by: kieranfoley
5 Replies

4. Shell Programming and Scripting

adding data in input file if 2nd script output SUCCESS

Hi All, how can i edit my original data and add more data if my 2nd script outputs SUCESS? ex. input file: 1.txt nik,is,the 1script.sh if 2ndscript.sh output SUCCESS then i'm going to edit my input file and add data best,pogi.. sample outputdata. nik,is,the,best,pogi 2ndscript.sh... (3 Replies)
Discussion started by: nikki1200
3 Replies

5. Shell Programming and Scripting

Help with reformat input data

Input file: 58227131 50087390 57339526 40578034 65348841 55614853 64363217 44178559 Desired output file: 58227131 50087390 57339526 40578034 65348841 55614853 64363217 44178559 Command that I try: (4 Replies)
Discussion started by: perl_beginner
4 Replies

6. Shell Programming and Scripting

split input data file and put into same output file

Hi All, I have two input file and need to generate a CSV file. The existing report just "GREP" the records with the Header and Tailer records with the count of records. Now i need to split the data into 25 records each in the same CSV file. id_file (Input file ) 227050994 232510151... (4 Replies)
Discussion started by: rasmith
4 Replies

7. Shell Programming and Scripting

How to add data from 2 input files and save it in 1 output file

Hi, i have 2 input files which are file1.txt and file2.txt. I need to extract data from file1.txt and file2.txt and save it in file3.txt like example below:- File1.txt ID scrap1 Name scrap1 start 1 end 10 ID scrap2 Name scrap2 start 11 end ... (4 Replies)
Discussion started by: redse171
4 Replies

8. Shell Programming and Scripting

Reformat the data of a file.

I have a file which have data like A.txt a 1Jan I am in a1. 1Jan I was born. 2Jan I am here. 3Jan I am in a3. b 1Jan I am in b1. c 2Jan I am in c2. d 2Jan I am in d2. 5jan I am in d5. date in the file might be vary evertime. (9 Replies)
Discussion started by: samkhu
9 Replies

9. Shell Programming and Scripting

AWK Script to convert input file(s) to output file

Hi All, I am hoping someone can help me with some scripting I need to complete using AWK. I'm trying to process multiple fixed files to generate one concatenated fixed file in a standard format. The Input file is:- aaaa bbbbb ccccc 1 xxxx aaa bbb aaaa bbbbb ccccc 2 abcd aaa CCC... (9 Replies)
Discussion started by: jason_v_brown
9 Replies

10. Shell Programming and Scripting

Replacing data of output file with input

Hi, I have a ksh which peocess and get me data from 3 days... ie if i process it on jan 28.. it gets data for 25, 26 and 27.... the process run every day and get previous 3 days data...all this data is appened to a file lets call time.out Now time.out cannot have deplicate data so what i want... (10 Replies)
Discussion started by: bhagya2340
10 Replies
Login or Register to Ask a Question