Hi all,
Hope all the expert can help me in this situation.
Let say I have one file with multiple record like below:
[10001]
NAME=FRAGMENT
LANGUAGE=1
DIALECT=0
GENDER=NONE
FILE=TEST1
DIRECTORY=D:/DETAILS/1/0/test1.txt
END
[10002]
NAME=FRAGMENT
LANGUAGE=1
DIALECT=0
GENDER=NONE
FILE=TEST2
DIRECTORY=
END
[10003]
NAME=FRAGMENT
LANGUAGE=1
DIALECT=0
GENDER=NONE
FILE=TEST3
DIRECTORY=
END
For each record, I need to generate the path for DIRECTORY where,
D:/DETAILS/ is fixed
1 is taken from LANGUAGE=1
0 is taken from DIALECT=0
test1 is taken from FILE=TEST1
test1 must be SMALL LETTER
Since in the file there are huge amount of records,so I think may be can use script to generate the path for each record. But getting stuck for the script now. Please advise.
Thank you so much.
Best Regards,
Shirley
Show what script have you prepared so far.
Try:
awk ' /DIRECTORY/ {print $1"=D:/DETAILS/"a["LANGUAGE"]"/"a["DIALECT"]"/"tolower(a["FILE"]);next}a[$1]=$2{}1' FS='=' sourceFile
Hi Klashxx,
I get the below error when execute the command:
awk: syntax error near line 1
awk: bailing out near line 1
Try to solved it many times already but still getting the same error. Please advise.
Thank you.
Best Regards,
Shirley
Great code.Can you please say what this part of the code is doing?
a[$1]=$2{}1
With Regards
Dileep Pattayath
shirleyeow:
Hi Klashxx,
I get the below error when execute the command:
awk: syntax error near line 1
awk: bailing out near line 1
Try to solved it many times already but still getting the same error. Please advise.
Thank you.
Best Regards,
Shirley
Try out this !
nawk -F"=" '/DIRECTORY/ {print $1"=D:/DETAILS/"a["LANGUAGE"]"/"a["DIALECT"]"/"tolower(a["FILE"]);next}a[$1]=$2{}1' file.txt
Hi Klashxx,
Yup, is Solaris box.
Hi DILEEP410,
The command is ok but it didnt return the path that I want, the LANGUAGE, DIALECT and FILE value are missing. Please advise.
RESULT:
[10001]
NAME=FRAGMENT
LANGUAGE=1
DIALECT=0
GENDER=NONE
FILE=TEST1
DIRECTORY=D:/DETAILS///
END
Thank you.
Best Regards,
Shirley
Use :
/usr/xpg4/bin/awk -F\= ' /DIRECTORY/ {print $1"=D:/DETAILS/"a["LANGUAGE"]"/"a["DIALECT"]"/"tolower(a["FILE"]);next}a[$1]=$2{}1' sourceFile
shirleyeow:
Hi Klashxx,
Yup, is Solaris box.
Hi DILEEP410,
The command is ok but it didnt return the path that I want, the LANGUAGE, DIALECT and FILE value are missing. Please advise.
RESULT:
[10001]
NAME=FRAGMENT
LANGUAGE=1
DIALECT=0
GENDER=NONE
FILE=TEST1
DIRECTORY=D:/DETAILS///
END
Thank you.
Best Regards,
Shirley
I tried it in solaris 5.8 and is working fine as you can see the result below
nawk -F"=" '/DIRECTORY/ {print $1"=D:/DETAILS/"a["LANGUAGE"]"/"a["DIALECT"]"/"tolower(a["FILE"]);next}a[$1]=$2{}1' file.txt
[10001]
NAME=FRAGMENT
LANGUAGE=1
DIALECT=0
GENDER=NONE
FILE=TEST1
DIRECTORY=D:/DETAILS/1/0/test1
END
[10002]
NAME=FRAGMENT
LANGUAGE=1
DIALECT=0
GENDER=NONE
FILE=TEST2
DIRECTORY=D:/DETAILS/1/0/test2
END
[10003]
NAME=FRAGMENT
LANGUAGE=1
DIALECT=0
GENDER=NONE
FILE=TEST3
DIRECTORY=D:/DETAILS/1/0/test3
END
Just assigns $2 to the associate array a, for latter use in the print statement.
Empty curly brackets {} are used to avoid double printing, and 1 to print all lines except those that match the /DIRECTORY/ pattern.
Regards
Hi Klashxx and DILEEP410,
The command is running ok now and I able to get the result that I want.
Thank you so much for your help. Really appreciate it.
Thank you.
Best Regards,
Shirley
nawk 'BEGIN{OFS=FS="="}
{
if ($1=="LANGUAGE")
{
print
t=sprintf("D:/DETAILS/%s",$2)
}
else if($1=="DIALECT")
{
print
t=sprintf("%s/%s",t,$2)
}
else if($1=="FILE")
{
print
t=sprintf("%s/%s",t,tolower($2))
}
else if($1=="DIRECTORY")
{
$2=sprintf("%s.txt",t)
print
}
else
print
}' file
summer_cherry:
nawk 'BEGIN{OFS=FS="="}
{
if ($1=="LANGUAGE")
{
print
t=sprintf("D:/DETAILS/%s",$2)
}
else if($1=="DIALECT")
{
print
t=sprintf("%s/%s",t,$2)
}
else if($1=="FILE")
{
print
t=sprintf("%s/%s",t,tolower($2))
}
else if($1=="DIRECTORY")
{
$2=sprintf("%s.txt",t)
print
}
else
print
}' file
Hi, if you want go that way , avoid unnecessary code:
awk 'BEGIN{OFS=FS="="}
/LANGUAGE/{t=sprintf("%s",$2)}
/DIALECT/ {t=sprintf("%s/%s",t,$2)}
/FILE/ {t=sprintf("%s/%s",t,tolower($2))}
/DIRECTORY/ {$2=sprintf("D:/DETAILS/%s.txt",t)}
1' file
Regards.