awk script queries


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users awk script queries
# 1  
Old 07-31-2011
awk script queries

Hi,

First query:
I am trying to execute the below command to pull all the record whose length is not of the expected. But this is not giving the expected results.

$2 is the record length passed in the script as second parameter.$filename is the filename on which the awk is executed.It is returning all the records irrespective of criteria. Please advise.

awk -v xx=$2 'length!=$xx' $filename

Second query:
In an flat file, some of the records are not of the fixed length.This is due to presence of null character and junk characters.If record length is expected to be 100..due to presence of these characters it is varying to be 102-105. when executing the awk command with length function to pull the records whose length isnt matching...for those records which have null character..the record displayed on standard output is only till null character.The subsequent fields after null character is not returned by awk.Please explain why this would happen and what is the solution to retain entire record.

Also suggest a command to identify the null characters and replace by a space or any solution to fix these junk or null characters..so that we can have a complete file whose length isnt deviating from expected length..Thanks!
# 2  
Old 07-31-2011
Please give examples of input and output.
# 3  
Old 07-31-2011
Code:
awk -v len=2 '{ if(NF!=len) print $0 }' awk_test

# 4  
Old 07-31-2011
Examples

Samples are entirely one line..as this text window doesnt accomodate many characters it shown as two lines.

First query:
By script pass : $2 = 144 ,filename=cusemp.txt

awk -v xx=144 'length!=xx' cusemp.txt >> out.txt

cat out.txt
E00200406250000000000 0001273411 0000000200406250000000000 000127341174208547600200406250000000000 0001273411INS

This is of 144 characters length.

Second query:

Due to the presence of junk characters..record size is different..this should also be of 144 characters in input..but it is not.

Input:
9LB200406250000000000 0001273409 0000000200406250000000000 0001273409^A1¤÷166200406250000000000 0001273409INS

Output:
9LB200406250000000000 0001273409 0000000200406250000000000 0001273409


---------- Post updated at 03:41 AM ---------- Previous update was at 03:39 AM ----------

This command isnt working.I tried now..it returns records which have correct length as well..please advise.
# 5  
Old 07-31-2011
Code:
awk -v mylen=144 '{if(length($0)!=mylen)print $0}' out.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Automation script for Oracle queries for two different Databases

Hi Team, I am Oracle Databse developer. I am currently working on two databases. DB1 and DB2. in DB1 I have a Select query which will return 100 records. In Db2 I have a Select query which also return 100 records. In these two tables ( in different Schemas) we have a common column. ... (2 Replies)
Discussion started by: vasuvv
2 Replies

2. Shell Programming and Scripting

How to run multiple Queries in a ksh Script?

How to run multiple Queries in a ksh Script I have a KSH script that has one SQL Query and generates and emails output of the query in HTML format. I want to change the script so that it has three SQL queries and the last query generates and emails the HTML output page of just that query. So far... (5 Replies)
Discussion started by: JolietJake
5 Replies

3. Shell Programming and Scripting

Script (with sql queries) not working using cron

Hi all, I have script, which performing sql queries and put output into file. When I run this script manually, its working fine, but when I want to schedule it with cron I am getting errors... I defined LD_LYBRARY_PATH and ,but no result. After I defined it, I am getting error: # more... (4 Replies)
Discussion started by: nypreH
4 Replies

4. Shell Programming and Scripting

Few queries regarding awk...

One of the command output is as below. -rw-r--r--+ 1 root root 75G Nov 21 16:43 /var/ovs/mount/86BXXX/running_pool/Machine1/System-sda.img -rw-r--r--+ 1 root root 75G Nov 21 16:36 /var/ovs/mount/86BXXX/running_pool/Machine2/System.img -rw-r--r--+ 1 root root 150G Sep 23 19:13... (2 Replies)
Discussion started by: pinga123
2 Replies

5. UNIX for Dummies Questions & Answers

Help with BASH/AWK queries ....

Hi Everyone, I have an input file in the following format: score.file1.txt contig00045 length=566 numreads=19 1047 0.0 contig00055 length=524 numreads=7 793 0.0 contig00052 length=535 numreads=10 607 e-176 contig00072 length=472 numreads=46 571 e-165... (8 Replies)
Discussion started by: Fahmida
8 Replies

6. Shell Programming and Scripting

Nested SQL queries within Shell script

Hi, Would someone know if I can fire nested sql queries in a shell script? Basically what I am trying to do is as follows: my_sql=$(sqlplus -s /nolog<<EOF|sed -e "s/Connected. *//g" connect... (2 Replies)
Discussion started by: shrutihardas
2 Replies

7. Shell Programming and Scripting

How to extract queries using UNIX shell script?

Hi, I have an input file which have many lines,from which i need to extract only the complete sql statements and write this alone to an output file. please help us in this. Regards Meva (7 Replies)
Discussion started by: meva
7 Replies

8. Shell Programming and Scripting

Multiple MySql queries in shell script?

Hi guys, i know how to run a single query using mysql embedded in a shell script as follows: `mysql -umyuser -pmypass --host myhost database<<SQL ${query}; quit SQL` However, how would i be able to run several queries within the same connection? The reason for this is i am creating... (3 Replies)
Discussion started by: muay_tb
3 Replies

9. UNIX for Dummies Questions & Answers

shell script for sql queries

Hi All, I have written 4 sql queries . Now I want to write one SHELL SCRIPTING program for all these queries... i.e 1.select * from head; 2. select * from detail; 3. delete from head; 4. delete from detail; Please let me know how to write a shell script... Thank you (1 Reply)
Discussion started by: user71408
1 Replies

10. Shell Programming and Scripting

Extracting select queries from script

Hi, I have a script in which a lot of sql queries are embedded ie,"Select .....;".My purpose is to document all the dml statements in the script along with the line number. I am thinking of writing a perlscript or by using awk/sed scripts for extracting all the 'select' statements/queries... (3 Replies)
Discussion started by: DILEEP410
3 Replies
Login or Register to Ask a Question