awk not behaving as expected


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk not behaving as expected
# 1  
Old 10-13-2013
Question awk not behaving as expected

Hi,

Immediate help on below will be appreciated.

I have to read a file (max of 10MB) which will have no new line characters, i.e. data in single line. and have to inster '\n' at every 100 characters. and if record starts with 'BUCA' then need to pick value of length 10 at position 71 and write it to a file otherwise drop the record.

I tried below, working fine up to inserting '\n' after every 100 characters but sending all the records into target file.

Code:
awk 'BEGIN { len = 100 } length <=100  { print; next }; { print substr($0,1,len); $0 = substr( $0, len+1); while ( length >= 100 ) {; printf "%s\n", substr($0,0,len); $0 = substr( $0, len+1) }; if ( length ) {; if( substr($0, 1, 4) ~ /"BUCA"/ ) {; print substr($0, 71, 10) } } }' "src_file.txt" > tgt_file.txt

(No new lines in the above script, only single line)
Also taking too much time for processing 10MB file (more than 5mins).

Don't know where I did mistake, Please correct me.

Thanks In Advance.

Smilie

Last edited by Scott; 10-13-2013 at 03:42 PM.. Reason: Added code tags
# 2  
Old 10-13-2013
Please provide sample input (consisting of a few hundred characters) and desired output.
# 3  
Old 10-13-2013
IO for above ove

IN file:
Code:
BUCH20131013                                                          4748420001                    BUCA20131013                                                          4748520001                    BUCH20131013                                                          4748420002                    BUCA20131013                                                          4748520002                    BUCH20131013                                                          4748420001                    BUCA20131013                                                          4748520003                    BUCH20131013                                                          4748420001                    BUCA20131013                                                          4748520004                    BUCH20131013                                                          4748420001                    BUCA20131013                                                          4748520005

(no new lines in the above data)
Out file:
Code:
4748520001
4748520002
4748520003
4748520004
4748520005

(output with new line after every 100 characters and pick 10 characters starting at position 71 from every record where record should start with "BUCA")

Last edited by Scrutinizer; 10-13-2013 at 07:31 PM.. Reason: code tags
# 4  
Old 10-13-2013
Code:
fold -w100 file
BUCH20131013 4748420001 BUCA20131013 4748520001 BUCH20131013 4748420002 BUCA20131013 4748520002 BUCH
20131013 4748420001 BUCA20131013 4748520003 BUCH20131013 4748420001 BUCA20131013 4748520004 BUCH2013
1013 4748420001 BUCA20131013 4748520005

I'm sure I don't understand how you want to produce your desired output.

Edit: Now that code tags have been applied to your input data, it's a different story - pipe the fold result through a grep or awk cmd to filter out the BUCA lines and get your (obviously) second field value.

Last edited by RudiC; 10-14-2013 at 02:12 PM..
# 5  
Old 10-13-2013
Please use code tags for input as well as output data.
# 6  
Old 10-13-2013
Try:
Code:
gawk 'NR>1{print $2}' RS=BUCH file

# 7  
Old 10-14-2013
try this awk ( stored in a file)

Code:
{rec=$0;endthis=1;this=1;len=length(rec)/2}
END { while (endthis<len+1)
printf("%s\n",substr(rec,this,100));
this+=100;endithis++;}}


Last edited by Scrutinizer; 10-14-2013 at 03:41 AM.. Reason: changed icode tags to regular code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk not giving the output expected

Hello, I am practising awk and decided to compare two columns and print the result of the comparison as third column i/p data c1,c2,c3 1,a,b 1,b,b i am trying to compare the last two columns and if they match I am trying to print match else mismatch(Ideally i want that as a last column... (5 Replies)
Discussion started by: mkathi
5 Replies

2. Shell Programming and Scripting

awk matching script not working as expected

This is my ubuntu version: $ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 16.04.4 LTS Release: 16.04 Codename: xenial $ /bin/awk -V | head -n1 bash: /bin/awk: No such file or directory I have gotten a script that helps me to parse,... (14 Replies)
Discussion started by: delbroooks
14 Replies

3. Shell Programming and Scripting

awk gsub not working as expected

Hi Experts, Need your kind help with gsub awk. Below is my pattern:"exec=1_host_cnt=100_dup=4_NameTag=targetSrv_500.csv","'20171122112948"," 100"," 1"," 1"," 4","400","","", " aac sample exec ""hostname=XXXXX commandline='timeout 10 openssl speed -multi 2 ; exit 0'"" ","-1","-1","1","... (6 Replies)
Discussion started by: pradyumnajpn10
6 Replies

4. Shell Programming and Scripting

awk command not working as expected

Following one line of awk code removes first 3 characters from each line but when I run the same code on another linux platform it doesn't work and only prints blank lines for each record. Can anyone please explain why this doesn't work? (31 Replies)
Discussion started by: later_troy
31 Replies

5. Shell Programming and Scripting

awk output not what was expected

Good Moring, I am currently reading about awk in a manual and following the examples using the oratab file. My system is SOLARIS 10 I think I am getting strange behavior judging by what the book says to do and what I am getting with my little program. Here is my program: grep -v oratab |... (4 Replies)
Discussion started by: bdby
4 Replies

6. Shell Programming and Scripting

awk not working as expected in script

Dear all, I had script which used to work, but recently it is not working as expected. I have command line in my shell script to choose the following format from the output_elog and perform some task afterwards on As you see, I want all numbers in foramt following RED mark except for... (12 Replies)
Discussion started by: emily
12 Replies

7. Shell Programming and Scripting

AWK Looping. How can I get expected result?

Can Anyone help with looping... awk 'FNR==1{i++} {for(k=1; k<=NF; k++) A=$k} # 3 Dimension Array END{ for(i=1;i<=217;i++) # For loop 2nd File 1st and 2nd column x=0;y=0 ... (18 Replies)
Discussion started by: Akshay Hegde
18 Replies

8. Shell Programming and Scripting

awk not generating the expected output

Hi, I am presently stuck in a csv file. INPUT CSV baseball,NULL,8798765,Most played baseball,NULL,8928192,Most played baseball,NULL,5678945,Most played cricket,NOTNULL,125782,Usually played cricket,NOTNULL,678921,Usually played EXPECTED OUTPUT CSV ... (7 Replies)
Discussion started by: scripter12
7 Replies

9. UNIX for Dummies Questions & Answers

awk command behaving differntly on 2 servers--urgent

Hi I am using awk command for string replacement. I have 2 servers. The command runs perfectly well on 1st server On the second server when i run the command on the same datset The command gets stuck while processing a large piece of record.. Does it have anything to with setting on the 2... (1 Reply)
Discussion started by: aixjadoo
1 Replies

10. Shell Programming and Scripting

awk not working as expected with BIG files ...

I am facing some strange problem. I know, there is only one record in a file 'test.txt' which starts with 'X' I ensure that with following command, awk /^X/ test.txt | wc -l This gives me output = '1'. Now I take out this record out of the file, as follows : awk /^X/ test.txt >... (1 Reply)
Discussion started by: videsh77
1 Replies
Login or Register to Ask a Question