hi,
I need a script which can format the below text file which contains comments
file1.txt
--------
//START
//Name: some value
//Date:
//Changes:.............
//.....................
//END
//START
//Date:
//Name: some value
//Changes:.............
//.....................
//END
Output should be:
Name |Date|Changes
Script should compare the column name and paste the output in above said manner.
I am trying this, can anybody please help me on this.
awk '
BEGIN{print "Name |Date|Changes"}
/Name:/ {$1="";n=$0}
/Date:/ {$1="";d=$0}
/Changes:/ {$1="";c=$0}
/END/ {print n " | " d " | " c;n=d=c=""}
' file1.txt
Here is my trial text with some more data added
file1.txt
---------
//START
//Name: some value
//Date:
//Changes:Change A
//more of change A
//END
//START
//Date:
//Name: some value
//Changes:This is change b
//And some more info
//END
And the awk program
fixfile.awk
-----------
BEGIN {
FS=":" ; OFS="|"
num=split("Name,Date,Changes",cols,",")
print cols[1],cols[2],cols[3]
}
{
$1=substr($1, 3);
s=$2
sub(/^ */, "", s);
if ($1 == "END") print res[1], res[2], res[3]
else {
if (res[3] != "" && NR=1) res[3]=res[3]" "$1
for(i=1; i<=num; i++) {
if (cols == $1) res=s;
}
}
And the result
$ awk -f fixfile.awk file1.txt
Name|Date|Changes
some value|23rd June 2009|Change A more of change A
some value|16th May 2010|This is change b And some more info
1 Like
Thank you very much for your replies.
hi rdcwayx,
The script given by you is not working as required. If the changes made exceeds more then one line, then it is not working.
hi Chubler_XL,
your script is working perfectly.
Now i need to extend this to more columns.
Can you please explain the logic written especially the below part:
$1=substr($1, 3);
s=$2
sub(/^ */, "", s);
---------- Post updated at 06:15 AM ---------- Previous update was at 02:58 AM ----------
hi Chubler_XL,
I have done some analysis and found that the below code is for the following purpose.
$1=substr($1, 3); #Would remove the two backslashes at the starting of the line
sub(/^ */, "", s); # Would remove the leading whitespaces in the result.
I have tried modifying the above code for four feilds. But it didnt work.
Sample file:
-----------
START
Name: some value
Date:
Function Name: kakjkldj
asdkjfkjlfj
Changes:Change A
more of change A
END
Ouput should be:
Name |Date|Function Name|Changes
Since am new to awk concepts, please help me out in understanding the logic written.....