awk 2 delimiter nested


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk 2 delimiter nested
# 1  
Old 10-09-2007
awk 2 delimiter nested Problem

Hello All, This work could be very easy for you guys. I would really appreciate help.

input file:
Quote:
aa|bb|cc|dd,ee ff,xx|gg,zz|hh,bv
ii|jj|kk|ll,mm|nn,as|oo,lk
pp|qq|rr|ss,tt uu,yy|vv,xc|ww,mn
output file: (Desired)
Quote:
num=aa value=bb digits=cc name1=dd:link1=ee name2:ff:link2=xx file=gg:link=zz code=hh:link=bv
num=ii value=jj digits=kk name1=ll:link1=mm file=nn:link=as code=oo:link=lk
num=pp value=qq digits=rr name1=ss:link1=tt name2=uu:link2=yy file=vv:link=xc code=ww:link=mn
What I am capable of doing:
Command: cat inputfile | awk -F\| '{print "num="$1" value="$2" digits="$3" name1="$4" file="$5" code="$6}' > outputfile

Result what I am capable of getting:
Quote:
num=aa value=bb digits=cc name1=dd,ee ff,xx file=gg,zz code=hh,bv
num=ii value=jj digits=kk name1=ll,mm file=nn,as code=oo,lk
num=pp value=qq digits=rr name1=ss,tt uu,yy file=vv,xc code=ww,mn
As per my understanding some tricks to that command is needed to involve nested 2 delimiters (i.e. "|" & "," & " "(space) chracter). I dont have any idea how to go about it. Could anyone solve this thing for me or give me some diff logic to acheive this desired output file.

I would really appreciate if some one helps as soon as poss. Thanks a lot people. Smilie

Last edited by onlyroshni; 10-09-2007 at 07:54 PM..
# 2  
Old 10-09-2007
With a modern awk (nawk, gawk, etc), FS (field separator) can be any regular expression.
including a backslash. For example

FS = "[;:\\\\]"

includes:

";" ":" "\"

(The brackets "[" and "]" are part of the regular expression syntax)
# 3  
Old 10-09-2007
I knw that. I myself have used "|" as delimiter. My problem here is to use 2 delimiters in order to get desired output file.

I need outputfile from inputfile by some trick.

Thanks for replying.Smilie
# 4  
Old 10-10-2007
awk

Hi,
Please try this one. On my pc it is ok.

Code:
awk 'BEGIN{FS="|"}
{
printf("num=%s value=%s digits=%s ",$1,$2,$3)
n=split($4,arr," ");
for (i=1;i<=n;i++)
{
	split(arr[i],brr,",")
	printf("name%d=%s:link%d=%s ",i,brr[1],i,brr[2])
}
split($5,crr,",")
printf("file=%s:link=%s ",crr[1],crr[2])
split($6,drr,",")
printf("code=%s:link=%s\n",drr[1],drr[2])
}' a

# 5  
Old 10-10-2007
Genious effort.

thank u v much. It works. I appreciate ur efforts.
# 6  
Old 10-10-2007
This is a lengthy one but easy!!!!!!!!!!!!!!!


awk -F"|" '{print "num="$1" value="$2" digits="$3" name1="$4" file="$5" code="$6""}' bb |awk -F"," '{print ""$1" link1="$2" link2="$3" link3="$4" link4="$5""}' |awk -F" " '{print ""$1" "$2" "$3" "$4" "$5" name2="$6" "$7" "$8" "$9" "$10" "$11""}'\^J| sed '2s/line4=//' |sed '2s/name2=//'

Regards,
aajan
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - multiple and nested if-then-else

Hello. I would like to convert the following piece of code from bash to awk. Here are bash variables in a bash script. CUR_ROW_ID and ROW_ID_TO_SEARCH contains a string which represent a row id. The string contain a valid row id. CUR_ROW_ID sometimes may be null. CUR_VALUE... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

awk nested looping?

I am trying to parse a text file and send its output to another file but I am having trouble conceptualizing how I am supposed to do this in awk. The text file has a organization like so: Name Date Status Location (city, state, zip fields) Where each of these is on a separate line in... (1 Reply)
Discussion started by: kellyanneghj
1 Replies

3. Shell Programming and Scripting

How to add second variable in awk nested if condition?

Hi Gurus, I have a command to assign value based on input value. current condition is "if pattern matches "case", then assign "HOLD" else "SUCC"right now, I need to add one more condition (variable name is VAR). the condition is "if pattern1 matches "case", then assign "HOLD" else if... (2 Replies)
Discussion started by: ken6503
2 Replies

4. Shell Programming and Scripting

Nested awk Statements

Hello again everyone, yes, I'm back again for more help! So I'm attempting to read two separate files and generate some XML code from that. My current code is: BEGIN { print "<?xml version=\"1.0\" encoding=\"utf-8\">" print "<Export>" } { x=1; print "<section name=\"Query" NR "\">"... (5 Replies)
Discussion started by: Parrakarry
5 Replies

5. Shell Programming and Scripting

Help with nested $s and quotations in bash / awk

Folks - newbie bash coder here and I'd like to get your help to make the code below work. As you can see, I was trying to count the total number of lines with the 3rd value >= 15 in a file and wanted to make the threshold "15" configurable, but apparently the $THRESHOLD value was not populated... (3 Replies)
Discussion started by: bashzipper
3 Replies

6. Shell Programming and Scripting

Nested case inside awk

please let me know if the below code could be written efficiently inside single awk case "$INP" in ksh) cat catalog | awk 'BEGIN {FS=",";} { print $2 } END {}' ;; pset) cat catalog | awk 'BEGIN {FS=",";} { print $3 } END {}' ;; dml) cat catalog | awk 'BEGIN {FS=",";} {... (2 Replies)
Discussion started by: cvsanthosh
2 Replies

7. Shell Programming and Scripting

syntax question in regards to nested awk statements

Hello all, I am writing up an input file and I was hoping I could get some guidance as to how to best consolidate these 2 awk statements for 1 while loop. Here's my input file # cat databases.lst #NOTE: These entries are delimited by tabs "\t" #oracleSID name/pass # db11 ... (2 Replies)
Discussion started by: Keepcase
2 Replies

8. Shell Programming and Scripting

Awk Script: Nested search using different patter

Hi Friends. Please have a look at dummy file. I need to extract from this file: 1. Counts of event= 2. the 2nd coulmn is unique call id of this transaction. Based on that, i have to search for txstatus= . Note: Values of event, calltype and txstatus can be anything. I want to print... (1 Reply)
Discussion started by: itsmesanju
1 Replies

9. Shell Programming and Scripting

Awk formatting of a data file - nested for loops?

Hello - is there any way in awk I can do... 4861 x(1) y(1) z(1) 4959 x(1) y(1) z(1) 5007 x(1) y(1) z(1) 4861 x(2) y(2) z(2) 4959 x(2) y(2) z(2) 5007 x(2) y(2) z(2) 4861 x(3) y(3) z(3) 4959 x(3) y(3) z(3) 5007 x(3) y(3) z(3) to become... 4861 x(1) y(1) z(1) 4861 x(2) y(2) z(2)... (3 Replies)
Discussion started by: catwoman
3 Replies

10. UNIX for Advanced & Expert Users

sed in awk ? or nested awk ?

Hey all, Can I put sed command inside the awk action ?? If not then can i do grep in the awk action ?? For ex: awk '$1=="174" { ppid=($2) ; sed -n '/$ppid/p' tempfind.txt ; }' tempfind.txt Assume: 174 is string. Assume: tempfind.txt is used for awk and sed both. tempfind.txt... (11 Replies)
Discussion started by: varungupta
11 Replies
Login or Register to Ask a Question