LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 04-04-2011, 08:07 AM   #1
jozelo
LQ Newbie
 
Registered: Sep 2007
Posts: 9

Rep: Reputation: 0
Question awk does not print the value of the variable as I expected, please help


Originally Posted by jozelo
I have this simple awk program but it not prints var_cab whe^E or ^S or ^M after he finds the second ^C

/^C/ { var_cab = substr($0,1,28); ent = 0 ; print "estoy en cabecera" var_cab;}
/^E/ { if ( ent ==0 ) var_fech = substr($0,4,8) ; ent ++;
print var_cab var_fech $0; }
/^S/ { print var_cab var_fech $0; }
/^M/ { print var_cab var_fech $0; }
END { print var_cab,ent}


the input file is many lines like this, every new block stats with ^CAB

CABES3000088888880000007EAMB
ENT20090706D060709-888 0028560000000012VALLE CA'ZULIA5501, 5502, 5498 y 5535
SAL201008250000134900000321V1202935-MU

SAL201011170001361000005245DES3008888777
5577
SAL201011240002224200008601V1202935-MU

SAL201011290001605400016054V1202935-MU

MEN20090701010026108100000020
MEN20090801020026096500000040
MEN20090901030026072900000066
MEN20091001040026044800000101
MEN20091101050025935600000135
CABES300030640069001EBSS
ENT20090324CU-L53-240309 0027839900000021ABSA 133225
SAL201006010004794800021040006,6900V1205439-MU

SAL201007010005631000024981006,5100V1205439-MU

SAL201010010000034400000199006,6800V1205439-MU

MEN20100101110024285000000400
MEN20100201120024211600000410
MEN20100301130024182100000417
MEN20100401140024153100000425
CABES300030640069005EBSS
ENT20080823CU-L50-230808 0026850400000030ABSA 129033
MEN20110101300012104000000942
MEN31/02/2011310012070900000951
CABES300030640069006EBSS
ENT20090120CU-L52-200109 0028987000000019AQUASTREM-G 17NR00000000010618
SAL201005010001311400005219006,4300V1205439-MU

SAL201006010000528300002086006,6900V1205439-MU

SAL201007010009972900046519006,5100V1205439-MU

MEN20100101130025965100000407
MEN20100201140025855500000417
MEN20100301150025775800000425
MEN20100401160025703100000434
MEN20100501170024350700000405
MEN20100601180023624400000455
MEN20100701190012744100000515
MEN20100801200012658400000573
MEN20100901210012522800000641
.... many more lines

Last edited by jozelo; 04-04-2011 at 08:31 AM.
 
Old 04-04-2011, 09:30 AM   #2
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
If the data was created in Windows there could be a carriage return character at the end of each line.
The first ^C line has 28 characters so substr($0,1,28) omits the carriage return.
The next ^C lines have only 24 characters so substr($0,1,28) includes the carriage return.

Carriage returns can be removed from the file with GNU sed
sed 's/\r//g'

or with
tr -d '\r'
 
Old 04-04-2011, 10:19 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
For that matter, awk can do it too. Add this line to the script to change dos endings to unix endings.
Code:
{ sub(/\r$/,""); }
Or as another option, try adding this to your script:
Code:
BEGIN{ RS="\n|\r\n"; }
This should allow it to handle both dos- and unix-style line endings, without actually converting them. At least it works for me in testing.

file filename.txt should tell you which kind of endings you have. If it says "with CRLF line terminators", then it's a dos-encoded file.
 
1 members found this post helpful.
Old 04-04-2011, 02:14 PM   #4
jozelo
LQ Newbie
 
Registered: Sep 2007
Posts: 9

Original Poster
Rep: Reputation: 0
Thumbs up Thanks a lot it work perfect with ...

Quote:
Originally Posted by David the H. View Post
For that matter, awk can do it too. Add this line to the script to change dos endings to unix endings.
Code:
{ sub(/\r$/,""); }
Or as another option, try adding this to your script:
Code:
BEGIN{ RS="\n|\r\n"; }
This should allow it to handle both dos- and unix-style line endings, without actually converting them. At least it works for me in testing.

file filename.txt should tell you which kind of endings you have. If it says "with CRLF line terminators", then it's a dos-encoded file.
the BEGIN{ RS="\n|\r\n"; } to rcognize end of record

Last edited by jozelo; 04-04-2011 at 02:15 PM. Reason: the BEGIN{ RS="\n|\r\n"; }
 
Old 04-04-2011, 02:17 PM   #5
jozelo
LQ Newbie
 
Registered: Sep 2007
Posts: 9

Original Poster
Rep: Reputation: 0
Thanks a lot it woked perfectly with the BEGIN{ RS="\n|\r\n"; }
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 03:18 AM
printf \b not working as expected in AWK Rockydell Programming 5 11-13-2009 08:28 AM
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 01:08 AM
get variable out from awk sharathkv25 Programming 1 05-08-2007 07:33 PM
awk on a variable onradius Programming 8 02-22-2006 09:34 AM


All times are GMT -5. The time now is 12:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration