File format check


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers File format check
# 8  
Old 10-21-2008
Thanks again!!!
# 9  
Old 10-21-2008
Or even without double '>':

Code:
awk -F\| '{print>((NF!=nf?"bad":"good")"_file.txt")}' nf="$tmp" infile

# 10  
Old 10-21-2008
Right, >> is append, and > is replace.

The ternary operator is your friend in these simple cases. I started to use the ternary operator but then thought the OP might have more print lines or other operations necessary on either side of the condition, so gave way to the traditional if-then-else.
MrC
# 11  
Old 10-21-2008
Yep,
I only wanted to point out that the AWK redirection is different from the shell redirection. From Effective AWK Programming:

Quote:
When this type of redirection is used, the output file is erased before the first
output is written to it. Subsequent writes to the same output file do not erase
output file, but append to it
. (This is different from how you use redirections in
shell scripts.) If output file does not exist, it is created.
Consider the following:

Code:
$ cat file
1 2 
1 2 3
1 2
1 2 3
1 2
$ awk '{print>((NF!=2?"more":"two")"_columns")}' file
$ head *_col*
==> more_columns <==
1 2 3
1 2 3

==> two_columns <==
1 2 
1 2
1 2


Last edited by radoulov; 10-21-2008 at 03:26 PM..
# 12  
Old 10-21-2008
Understood. The redirection plumbing in awk is setup once, and subsequent > or >> redirections simply continue writing on the already open FD. In shell scripts, the FD is closed at the end of each command.
MrC
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check if file is EBCDIC or ASCII format

So, i have this requirement where i need to check the file format, whether it's EBCDIC or ASCII, and based on format retrieve the information from that file: my file is: file1.txt-->this ebcdic file file2.txt-->ascii file i tried below code: file=file1.txt type="`file $file`" i get... (7 Replies)
Discussion started by: gnnsprapa
7 Replies

2. Shell Programming and Scripting

Telephone Format and Check

How can I script to check telephone number entered by user in a uniz script: print -n "Enter telephone number to check ? " ; read telno How do I script to : 1) Check if entered without dashes or with dashes 2) If without dashes how can I check: if telno is only 10... (2 Replies)
Discussion started by: mrn6430
2 Replies

3. Shell Programming and Scripting

How to check user entered correct file format or not?

Hi Experts, path=/db/files/ format=$1 User can enter any file format.compare the user file format with actual file format existed in the directory /db/files. User enter all characters as "A" apart from date format. example1: user will be entering the file format AAA_AA_YYYYMMDD.AAA Actual... (6 Replies)
Discussion started by: nalu
6 Replies

4. Shell Programming and Scripting

Script to check dos format in a file

Hi All, I am trying to check if the file is in dos format using simple grep command but the problem is lines inside the file with have special characters in between and in some lines end of the line will have the '^M' character. I tried the below command in simple line(without special... (7 Replies)
Discussion started by: Optimus81
7 Replies

5. Shell Programming and Scripting

finding date numeral from file and check the validity of date format

hi there I have file names in different format as below triss_20111117_fxcb.csv triss_fxcb_20111117.csv xpnl_hypo_reu_miplvdone_11172011.csv xpnl_hypo_reu_miplvdone_11-17-2011.csv xpnl_hypo_reu_miplvdone_20111117.csv xpnl_hypo_reu_miplvdone_20111117xfb.csv... (10 Replies)
Discussion started by: manas_ranjan
10 Replies

6. Shell Programming and Scripting

Need to check date format yyyymm

My source file having one date column. The formate of the date column is yyyymm. I need to validate whether all the rows are in same format in the given file. If it is not I have captured that records in a separate file. I am very new to Unix. I don't how to achieve this. Plz help me to achieve... (2 Replies)
Discussion started by: suresh01_apk
2 Replies

7. Shell Programming and Scripting

How to check file name format using shell script?

Hi, I am writting a script, which accepts input file as parameter. Input file name is aa_bb_cc_dd_ee.<ext> I need to check that input file name should be of 5 fileds. Please help me out. :confused: (7 Replies)
Discussion started by: Poonamol
7 Replies

8. Shell Programming and Scripting

How to check for file name of specific format using find?

I have to find the specific formatted file is present in the received list in the directory, for which I have written: file_list=`ls -lrt /tmp/vinay/act/files |grep "$cdate"| awk '{print $9}'` while read fileStr do find $file_list $fileStr > /dev/null status=`echo $?` if ; then ... (3 Replies)
Discussion started by: IND123
3 Replies

9. Shell Programming and Scripting

Check the format of Date

Hi, I have a date field in my input file. I just want to check if its in the format "DD-MM-YYYY". Is there any command which can achieve this? Thanks and Regards, Abhishek (2 Replies)
Discussion started by: AAA
2 Replies

10. Shell Programming and Scripting

Check whether a given file is in ASCII format and data is tab-delimited

Hi All, Please help me out with a script which checks whether a given file say abc.txt is in ASCII format and data is tab-delimited. If the condition doesn't satisfy then it should generate error code "100" for file not in ASCII format and "105" if it is not in tab-delimited format. If the... (9 Replies)
Discussion started by: Mandab
9 Replies
Login or Register to Ask a Question