Converting form field to table format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Converting form field to table format
# 1  
Old 03-07-2012
Converting form field to table format

May data
Code:
   Name = Andi
Address = none
   Phone = 82728

   Name = Peter
Address = none
   Phone = 98799

The expected output
Code:
Name,Address,Phone
Andi,none,82728
Peter,none,98799

what i have done
Code:
$ awk -F"=" '{print $2}' data.txt |paste -sd ,
 Andi, none, 82728,, Peter, none, 98799

Thx in Adv.
# 2  
Old 03-07-2012
Try:
Code:
awk -F'=[ \t]*|\n' '{print $2,$4,$6}' OFS=, RS= infile

# 3  
Old 03-08-2012
Quote:
Originally Posted by Scrutinizer
Try:
Code:
awk -F'=[ \t]*|\n' '{print $2,$4,$6}' OFS=, RS= infile

thank you can not imagine if we can use regex as field separator,

sometimes the script is not working i check there is special character for each record set
Image
# 4  
Old 03-08-2012
Quote:
Originally Posted by before4
May data
Code:
   Name = Andi
Address = none
   Phone = 82728

   Name = Peter
Address = none
   Phone = 98799

The expected output
Code:
Name,Address,Phone
Andi,none,82728
Peter,none,98799

what i have done
Code:
$ awk -F"=" '{print $2}' data.txt |paste -sd ,
 Andi, none, 82728,, Peter, none, 98799

Thx in Adv.
Code:
# awk -vfs=',' 'BEGIN{printf "%s%s%s\n","Name"fs,"Address"fs,"Phone";}{
if($0!~/^ *$/) a[++x]=$NF;else b[x]=x-1};END{for(i=1;i<=x;i++)if(i-1==b[i]&&b[i]!=0||i==x)printf "%s\n",a[i];
else{printf "%s"fs,a[i]}}' infile
Name,Address,Phone
Andi,none,82728
Peter,none,98799

regards
ygemici
This User Gave Thanks to ygemici For This Post:
# 5  
Old 03-08-2012
Quote:
Originally Posted by before4
thank you can not imagine if we can use regex as field separator,

sometimes the script is not working i check there is special character for each record set
Image
Yes, the previous solution only works if the empty line between the records is completely empty (two linefeeds).
Try this instead:
Code:
awk -F '=[ \t]*' '$1~/Name/{p=$2;if(getline)p=p OFS $2;if(getline)print p,$2}' OFS=, infile

This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 03-09-2012
Quote:
Originally Posted by ygemici
Code:
# awk -vfs=',' 'BEGIN{printf "%s%s%s\n","Name"fs,"Address"fs,"Phone";}{
if($0!~/^ *$/) a[++x]=$NF;else b[x]=x-1};END{for(i=1;i<=x;i++)if(i-1==b[i]&&b[i]!=0||i==x)printf "%s\n",a[i];
else{printf "%s"fs,a[i]}}' infile
Name,Address,Phone
Andi,none,82728
Peter,none,98799

regards
ygemici
Thank you.. but i can got the wrong output and also did not work for Name field more than 1 word( ie. Andi ruby) and when the field has empty value
Code:
$ awk -V | head -1
GNU Awk 4.0.0

$ cat data.txt
   Name = Andi
Address = none
   Phone = 82728

   Name = Peter
Address = none
   Phone = 98799

$ awk -vfs=',' 'BEGIN{printf "%s%s%s\n","Name"fs,"Address"fs,"Phone";}{ \
if($0!~/^ *$/) a[++x]=$NF;else b[x]=x-1};END{for(i=1;i<=x;i++)if(i-1==b[i]&&b[i]!=0||i==x)printf "%s\n",a[i]; \
else{printf "%s"fs,a[i]}}' data.txt
Name,Address,Phone
Andi,none,82728,                                         ,Peter,none,98799


Quote:
Originally Posted by Scrutinizer
Yes, the previous solution only works if the empty line between the records is completely empty (two linefeeds).
Try this instead:
Code:
awk -F '=[ \t]*' '$1~/Name/{p=$2;if(getline)p=p OFS $2;if(getline)print p,$2}' OFS=, infile

it's working , i tried to understand the code but can not get in.. i tried to modified for form that contain more than 3 fields ( name, address, phone,email etc.) but can not get the output it's only display the 3 fields.

Last edited by before4; 03-09-2012 at 05:15 AM..
# 7  
Old 03-09-2012
Quote:
Originally Posted by before4
Thank you.. but i can got the wrong output and also did not work for Name field more than 1 word( ie. Andi ruby) and when the field has empty value
Code:
$ awk -V | head -1
GNU Awk 4.0.0

$ cat data.txt
   Name = Andi
Address = none
   Phone = 82728

   Name = Peter
Address = none
   Phone = 98799

$ awk -vfs=',' 'BEGIN{printf "%s%s%s\n","Name"fs,"Address"fs,"Phone";}{ \
if($0!~/^ *$/) a[++x]=$NF;else b[x]=x-1};END{for(i=1;i<=x;i++)if(i-1==b[i]&&b[i]!=0||i==x)printf "%s\n",a[i]; \
else{printf "%s"fs,a[i]}}' data.txt
Name,Address,Phone
Andi,none,82728,                                         ,Peter,none,98799

1-)how about your input file?
Code:
# od -c input

2-) what is your system?

and
i modified code for other your requests(more than 1 word or empty)..
and i tried for gawk 4.0 but results are same as i think,
maybe your input file has contain different charset.
Code:
# cat testfile
Name =
Address = Oklahoma city
   Phone = 82728

   Name = Peter Surname
Address = none
   Phone = 98799

   Name = John
Address = NJ City
   Phone = 10000

   Name = Mr.Smith
Address =
   Phone = 5555555 555555

Code:
# gawk2 -V|grep "GNU AWK" -i
GNU Awk 4.0.0

Code:
# gawk2 -vfs=',' 'BEGIN{printf "%s%s%s\n","Name"fs,"Address"fs,"Phone";}
{if($0!~/^ *$/){s=gensub(".*=[\t ]*(.*)","\\1",$0);
if(match(s,"[^\t ]"))a[++x]=s;else a[++x]="XXX"}else b[x]=x-1}
END{for(i=1;i<=x;i++)if(i-1==b[i]&&b[i]!=0||i==x)printf "%s\n",a[i]; else printf "%s"fs,a[i]}' testfile
Name,Address,Phone
XXX,Oklahoma city,82728
Peter Surname,none,98799
John,NJ City,10000
Mr.Smith,XXX,5555555 555555

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

JSON structure to table form in awk, bash

Hello guys, I want to parse a JSON file in order to get the data in a table form. My JSON file is like this: { "document":{ "page": }, { "column": } ] }, { ... (6 Replies)
Discussion started by: Gescad
6 Replies

2. Web Development

Php help to copy form field if empty

I have an input form with several fields. What I would like to achieve is to auto populate or copy certain fields if they are empty when the form is submitted. I would like to use php if not then javascript but not jquery if possible - I have sort of had a go but I really have no idea... (4 Replies)
Discussion started by: barrydocks
4 Replies

3. Shell Programming and Scripting

Getting data in table form

Hi, I have a csv file from which i am fetching few columns as below: IFILE=/home/home1/Report1.csv OFILE=/home/home1/`date +"%m%d%y%H%M%S"`.dat if #Checks if file exists and readable then awk -F "," '(NR>4) {print $1,$6,$2,$3,$4,$5,$6}' ${IFILE} >> ${OFILE} fi cat $OFILE | mail... (7 Replies)
Discussion started by: Vivekit82
7 Replies

4. Linux

How do I format a Date field of a .CSV file with multiple commas in a string field?

I have a .CSV file (file.csv) whose data are all enclosed in double quotes. Sample format of the file is as below: column1,column2,column3,column4,column5,column6, column7, Column8, Column9, Column10 "12","B000QRIGJ4","4432","string with quotes, and with a comma, and colon: in... (3 Replies)
Discussion started by: dhruuv369
3 Replies

5. Shell Programming and Scripting

Cgi to dump xml data from form input field

Hi All, I am trying to write a shell script which takes parse the web form find the input field and dump the data of that field into one xml file. The form looks like, <input type="button" id="btnSave" value="Save" onclick="saveXmlData()"/> <form name="submitForm"... (1 Reply)
Discussion started by: jdp
1 Replies

6. Shell Programming and Scripting

Converting windows format file to unix format using script

Hi, I am having couple of files which i used to copy from windows to Linux, so now in case of text files (CTRL^M) appears at end of line. I know i can convert this windows format file to unix format file by running dos2unix. My requirement here is that i want to do it automatically using a... (5 Replies)
Discussion started by: sarbjit
5 Replies

7. Shell Programming and Scripting

Converting html table data into multiple variables.

Hi, Basically what I am trying to do is the following. I have created a shell script to grab timetabling information from a website using curl then I crop out only the data I need which is a table based on the current date. It leaves me with a file that has the table I want plus a small amount... (2 Replies)
Discussion started by: domsmith
2 Replies

8. Shell Programming and Scripting

help on formatting output (Table Form)

Data in File ABC:DEFGHI:123 ABCZYE:DEFI:123 ABCFGD:DEF:123 ABCEERRRRR:DEFGHI:123 Expected Format 1 ABC DEFGHIFE 123 2 ABCZYE DEFI 123 3 ABCFGD DEF 123 4 ABCEERRRRR DEFGHI 123 However when i enter the following... (2 Replies)
Discussion started by: blurboy
2 Replies

9. Shell Programming and Scripting

Converting %## back to special characters from an HTML form

I have an HTML form that sends email to a large list of users one at a time by matching an email address in peoplesoft to their username. It works great, except that special characters are converted to %## format. Is there a library of these I can use to sed them back (yes this is a crappy UNIX... (1 Reply)
Discussion started by: 98_1LE
1 Replies
Login or Register to Ask a Question