The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help me with parsing this file eamani_sun Shell Programming and Scripting 2 05-16-2008 12:39 PM
awk and file parsing devtakh Shell Programming and Scripting 4 05-06-2008 08:13 AM
Parsing xml file using Sed kapilkinha UNIX for Advanced & Expert Users 3 04-08-2008 06:43 AM
Parsing a csv file chiru_h Shell Programming and Scripting 6 02-12-2008 06:33 AM
File Parsing jsusheel Shell Programming and Scripting 5 09-25-2007 07:25 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 08-17-2006
Registered User
 

Join Date: Oct 2002
Location: singapore
Posts: 6
parsing file through awk

hi,

how can i achieve this in awk

CON Controllers Department
R abcuser usernamedesc1
R defuser usernamedesc2
R ghiuser usernamedesc3
R jkluser usernamedesc4

expected results:

CON|Controllers Department|abcuser|usernamedesc1
CON|Controllers Department|defuser|usernamedesc2
CON|Controllers Department|ghiuser|usernamedesc3
CON|Controllers Department|jkluser|usernamedesc4
Reply With Quote
Forum Sponsor
  #2  
Old 08-17-2006
Registered User
 

Join Date: Jun 2006
Location: Delhi, India
Posts: 88
Code:
cat awktest
CON Controllers Department
R abcuser usernamedesc1
R defuser usernamedesc2
R ghiuser usernamedesc3
R jkluser usernamedesc4

Code:
awk 'BEGIN{OFS="|"}NR==1{string=$1"|"$2" "$3}NR>1{print string,$2,$3}' awktest
CON|Controllers Department|abcuser|usernamedesc1
CON|Controllers Department|defuser|usernamedesc2
CON|Controllers Department|ghiuser|usernamedesc3
CON|Controllers Department|jkluser|usernamedesc4
Reply With Quote
  #3  
Old 08-17-2006
Registered User
 

Join Date: Aug 2006
Posts: 69
Dear vish_indian,

I was also trying a solution, with a different approch. I wrote this script

awk 'BEGIN {flag=1;
> ff=$1;sf=$2;tf=$3;
> }
> if ( flag == 1 )
> flag=0;
> else
> print(ff "|" sf tf "|" $2 "|" $3);
> ' < sample

where sample is the input file. But this script is giving me error...

awk: syntax error near line 4
awk: bailing out near line 4

can you plese guide what is wrong with this??

regards
Apoorva Kumar
Reply With Quote
  #4  
Old 08-18-2006
Registered User
 

Join Date: Jun 2006
Location: Delhi, India
Posts: 88
Hi,

There are couple of things.

You forgot the brackets around if-else block.
awk 'BEGIN {flag=1; ff=$1;sf=$2;tf=$3; } {
if ( flag == 1 )
flag=0;
else
print(ff "|" sf tf "|" $2 "|" $3);
}' filename

If you run this code, output is
Quote:
||abcuser|usernamedesc1
||defuser|usernamedesc2
||ghiuser|usernamedesc3
||jkluser|usernamedesc4
It happens so because code in BEGIN block is executed before the file is read. Thus, $1,$2,$3 are all "blank". Thus, you need to define variables ff,ss,tf in another block and since it has to run only for 1st row, code would be like
Code:
NR==1{ ff=$1;sf=$2;tf=$3; }
or inside your if condition
if ( flag == 1 )
flag=0;
ff=$1;sf=$2;tf=$3;
Reply With Quote
  #5  
Old 08-18-2006
Registered User
 

Join Date: Jun 2006
Location: Delhi, India
Posts: 88
Here's the code using your logic.


Code:
awk 'BEGIN {flag=1;
  }
 {if ( flag == 1 )
 {flag=0;ff=$1;sf=$2;tf=$3;}
 else
print(ff "|" sf,tf "|" $2 "|" $3);}
' awktest
Reply With Quote
  #6  
Old 08-18-2006
Registered User
 

Join Date: Aug 2006
Posts: 69
Thanks a lot!! you have clearified the things!!

with warm regards
Apoorva Kumar
Reply With Quote
  #7  
Old 08-18-2006
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
here is something more generic ..

Code:
awk 'BEGIN{val=""} { 
if(NR==1) { 
for( x=1; x<=NF; x++) { 
if(x == 1 ) {
val=val$x"|" 
}
else {
val=val" "$x
}
} 
val=val"|"
} 
else { 
final="" 
for(x=2;x<=NF;x++) { 
final=final$x"|" 
} 
print val""final
} 
}' data
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:14 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0