![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Code:
cat awktest 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|defuser|usernamedesc2 CON|Controllers Department|ghiuser|usernamedesc3 CON|Controllers Department|jkluser|usernamedesc4 |
|
#3
|
|||
|
|||
|
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
|
|||
|
|||
|
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:
Code:
NR==1{ ff=$1;sf=$2;tf=$3; }
if ( flag == 1 ) flag=0; ff=$1;sf=$2;tf=$3; |
|
#5
|
|||
|
|||
|
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
|
|||
|
|||
|
Thanks a lot!!
with warm regards Apoorva Kumar |
|
#7
|
|||
|
|||
|
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
|
|||
| Google The UNIX and Linux Forums |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|