Sponsored Content
Top Forums Shell Programming and Scripting Converting form field to table format Post 302605952 by ygemici on Friday 9th of March 2012 07:28:57 AM
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

 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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. 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

7. 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

8. 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

9. 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
IGAWK(1)							 Utility Commands							  IGAWK(1)

NAME
igawk - gawk with include files SYNOPSIS
igawk [ all gawk options ] -f program-file [ -- ] file ... igawk [ all gawk options ] [ -- ] program-text file ... DESCRIPTION
Igawk is a simple shell script that adds the ability to have ``include files'' to gawk(1). AWK programs for igawk are the same as for gawk, except that, in addition, you may have lines like @include getopt.awk in your program to include the file getopt.awk from either the current directory or one of the other directories in the search path. OPTIONS
See gawk(1) for a full description of the AWK language and the options that gawk supports. EXAMPLES
cat << EOF > test.awk @include getopt.awk BEGIN { while (getopt(ARGC, ARGV, "am:q") != -1) ... } EOF igawk -f test.awk SEE ALSO
gawk(1) Effective AWK Programming, Edition 1.0, published by the Free Software Foundation, 1995. AUTHOR
Arnold Robbins (arnold@skeeve.com). Free Software Foundation Nov 3 1999 IGAWK(1)
All times are GMT -4. The time now is 04:30 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy