parsing file through awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting parsing file through awk
# 1  
Old 08-17-2006
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
# 2  
Old 08-18-2006
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
# 3  
Old 08-18-2006
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
# 4  
Old 08-18-2006
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;
# 5  
Old 08-18-2006
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

# 6  
Old 08-18-2006
Thanks a lot!! Smilie you have clearified the things!!

with warm regards
Apoorva Kumar
# 7  
Old 08-18-2006
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing a file name with awk

I have a command to print out the top 5 most cpu intensive commands: ps aux --sort=-%cpu --no-headers I would like to make a printout containing only the parsed file name. So, I want to turn this: $ ps aux --sort=-%cpu --no-headers user 24883 4.3 1.7 2010436 131440 ? Sl ... (2 Replies)
Discussion started by: bedtime
2 Replies

2. Shell Programming and Scripting

awk parsing file

Looking to parse a file to remove the duplicates and get only few fields and uniq rows. input.tx Loc (TC) ID : ssfad_fs Serial : PIC002340098 ... (8 Replies)
Discussion started by: shunya
8 Replies

3. Shell Programming and Scripting

AWK? parsing /etc/passwd file.

Hello guys, please help me to make simple script for parsing passwd file. I have many passwd files from our servers, named server1.pass, server2.pass etc.. so for server in `ls *.pass` i need to print these rows: server1;root:!:0:0::/root:/usr/bin/ksh... (7 Replies)
Discussion started by: rubico
7 Replies

4. Shell Programming and Scripting

Help in file parsing with awk

Hi, I have a data set as shown below: 09e757fd,22727,2012-03-01,text1,text2,to 3fd0cae7,310,2012-03-01,text3,text4,to 3fd0cae7,310,2012-03-01,text3,text5,to 3fd0cae7,311,2012-03-01,text7,text10,cc 3fd0cae7,311,2012-03-01,text7,text11,to 3fd0cae7,312,2012-03-01,text8,text15,to... (3 Replies)
Discussion started by: shekhar2010us
3 Replies

5. Shell Programming and Scripting

Parsing a file with AWK

Input Group: Erecords: Copy: ADC R2: Replication volumes: Replication set: RSet 1 Replication size: 50.00GB SAN Info: 50.00GB DGC VRAID CX4-960 LUN 1040 (1040) 60,06,01,60,32,bb,21,00,84,a0,08,b1,da,ec,df,11... (2 Replies)
Discussion started by: greycells
2 Replies

6. UNIX for Advanced & Expert Users

Parsing through a file with awk/sed

I don't necessary have a problem, as I have a solution. It is just that there may be a better solution. GOAL: Part one: Parse data from a file using the "\" as a delimiter and extracting only the last delimiter. Part two: Parse same file and extract everything but the last delimited item. ... (8 Replies)
Discussion started by: OrangeYaGlad
8 Replies

7. Shell Programming and Scripting

awk/sed for parsing file

Hi All, I have a log file like this E Mon Oct 06 00:17:08 2008 xxx2 cm:10614 fm_pi2_svc_iptv_purchase.c:149 1:pin_deferred_act:10601:11:169:1223245028:16 pi2_op_svc_iptv_purchase error <location=PIN_ERRLOC_FM:5 class=PIN_ERRCLASS_SYSTEM_DETERMINATE:1... (10 Replies)
Discussion started by: subin_bala
10 Replies

8. Shell Programming and Scripting

Parsing a file (sed/awk?)

Hello people, newbie question. I'm trying to parse these type of file 1 "CAR " " C1 " " " 6 0 C1 2 "CAR " " O1A" " " 8 0 O1A 3 "CAR " " O1B" " " 8 -1 O1B 4 "CAR " " C2 " " " 6 0 C2 5 "CAR " " C3 " " " 6 ... (10 Replies)
Discussion started by: aristegui
10 Replies

9. Shell Programming and Scripting

awk and file parsing

Hi, I have a input file like this TH2TH2867Y NOW33332106Yo You Baby TH2TH3867Y NOW33332106No Way Out TH2TH9867Y NOW33332106Can't find it TJ2TJ2872N WOW33332017sure thing alas TJ2TJ3872N WOW33332017the sky rocks TJ2TJ4872N WOW33332017nothing else matters ... (4 Replies)
Discussion started by: devtakh
4 Replies

10. Shell Programming and Scripting

AWK and Magic with file parsing

Hi, I have a input file like this TH2TH2867Y NOW33332106Yo You Baby TH2TH3867Y NOW33332106No Way Out TH2TH9867Y NOW33332106Can't find it TJ2TJ2872N WOW33332017sure thing alas TJ2TJ3872N WOW33332017the sky rocks TJ2TJ4872N WOW33332017nothing else matters TJ2TJ5872N WOW33332017you know... (1 Reply)
Discussion started by: devtakh
1 Replies
Login or Register to Ask a Question