print an output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting print an output
# 1  
Old 12-02-2010
print an output

A txt file is having the columns like this
Code:
TABLENAME                COLUMNS             DATATYPE
TAB1                     COL1                INTEGER
                         COL2                INTEGER
                         COL3                INTEGER
                         COL4                INTEGER

printf should be
Code:
CREATE TABLE TAB1 (COL INTEGER,COL2,INTEGER,COL3,INTEGER,COL4 INTEGER);

can any body help for the output?
Thanks

Last edited by Scott; 12-03-2010 at 04:32 PM.. Reason: Code tags, please...
# 2  
Old 12-02-2010
Code:
sed -n '
  1d
  s/^\([^ ]\{1,33\} \)\(.*\)/CREATE TABLE \1(\2,/
  :loop
  ${
    s/,$/ );/
    p
    }
  N
  s/\n\(.*\)/ \1,/
  b loop
 ' schema_file.txt

Narrative: sed (not printing automatically) tosses the first line, takes the first word and space from the second line to start the create with one column and a comma, then piles on all following lines with a comma, and at EOF removes the final comma and prints the result.

You might get too long a line for some SQL clients. Assumes seond line first white space is space or spaces, no leading white space, and does nothing to remove trailing white space.
# 3  
Old 12-02-2010
Hello Pickket,
Thanks for your reply.i could not able to get exact out put .i tried the following thing.
Code:
sed -n '
  1d
  s/^\([^ ]\{1,33\} \)\(.*\)/CREATE TABLE \1(\2,/
  :loop
  ${
    s/,$/ );/
    p
    }
  N
  s/\n\(.*\)/ \1,/
  b loop' test.txt> > > > > > > > > >
TAB1            COL1    INTEGER                 COL2    INTEGER,                COL3    INTEGER,                COL4    INTEGER,  );

Regards

Last edited by Scott; 12-03-2010 at 04:33 PM.. Reason: Code tags
# 4  
Old 12-02-2010
Something threw off the first line substitute, perhaps tab not blank?

Narrative: substitute for beginning of line (start capture) not-space in number 1 to 33 folllowed by a space (end first capture) (capture rest of line), replacement is "CREATE TABLE " + first capture + "(" + second capture + ",".

Put your code in "code brackets" in the post, before the moderator dings you! It's that little button above the post box just left of the php labeled icon.
# 5  
Old 12-03-2010
Hello Pakket,

I tried the same but still out put is not accurate.
Code:
sed -n '
  1d
  s/^\([^ ]\{1,33\} \)\(.*\)/CREATE TABLE "("\1(\2","/
  :loop
  ${
    s/,$/ );/
    p
    }
  N
  s/\n\(.*\)/ \1,/
  b loop
 ' schema_file.txt
TAB1 COL1 INTEGER COL2 INTEGER, COL3 INTEGER, COL4 INTEGER, );


Last edited by Scott; 12-03-2010 at 04:33 PM.. Reason: Code tags
# 6  
Old 12-03-2010
Run your input through cat -vte and post it, so I can see the pattern and line numbers.

Why did you add ", sed does not need.

Last edited by DGPickett; 12-03-2010 at 04:47 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print output as columns

Normal grep is not working to get the output. Sample Input: newjob: abc command name: a=b+c newjob: bbc command name: c=r+v newjob:ddc newjob:kkc command name: c=l+g newjob:mdc newjob:ldc newjob:kjc command name: u=dl+g newjob:lkdc newjob:lksdc command name: o=udl+g (6 Replies)
Discussion started by: onesuri
6 Replies

2. Programming

Print only some strings from an output

Hi, Here is an example: I have a grep line: grep -i -r -H "$WORD" "$DIRECTORY"with an output like this: /media/dir/dir2//dir4/file.txt:/media/dir/dir2/dir3/file_16072008/es6.txt: "content of the file found from grep"/media/dir/dir2/dir3/dir4/file3.txt:/media/dir/dir2/dir3//file.txt:"other... (3 Replies)
Discussion started by: Hornys
3 Replies

3. UNIX for Dummies Questions & Answers

Any awk one liner to print df output?

Hi, OS = Solaris Can anyone advise if there is a one liner to print specific output from a df -k output? Running df from a command line, it sometimes gives me 2 lines for some volume. By re-directing the output to a file, it always gives 1 line for each. Below is an example output,... (4 Replies)
Discussion started by: newbie_01
4 Replies

4. Shell Programming and Scripting

Awk script to run a sql and print the output to an output file

Hi All, I have around 900 Select Sql's which I would like to run in an awk script and print the output of those sql's in an txt file. Can you anyone pls let me know how do I do it and execute the awk script? Thanks. (4 Replies)
Discussion started by: adept
4 Replies

5. Shell Programming and Scripting

Print first output

Hello, II did a grep and got a lot of values i.e aa bb cc dd ee and i want to print first only aa and afterward only bb etc... help! (8 Replies)
Discussion started by: LiorAmitai
8 Replies

6. Shell Programming and Scripting

filtering print output

I have these data below and i want my output to print only the 3rd files. For example, in /opt/home/nyfix/.k5login, i want to print only "nyfix". /opt/home/nyfix/.k5login /opt/home/security/.k5login /opt/home/noc/.k5login what is the appropriate command in shell scripting ? (2 Replies)
Discussion started by: linuxgeek
2 Replies

7. Shell Programming and Scripting

du to print some of output

The output of du -b file* | awk '{print $1}' is: 1501090 1500538 I want the result to be a sum of this column. ie the o/p should be 3001628 Any idea? (3 Replies)
Discussion started by: xs2punit
3 Replies

8. UNIX for Dummies Questions & Answers

awk {print $NF} output??

Hi, I am trying to debug an old script and have found the problem lies within this function: isIdoc() { # validate the file type fileType=`file $1 | awk '{print $NF}'` && echo 0 || echo 1 } My question is, how can I determine what is in the variable $fileType ? The program is... (1 Reply)
Discussion started by: vervette
1 Replies

9. Shell Programming and Scripting

using perl or awk to print output

suppose u have file File A A -> G C->D A -> R P->A File B A=1 C=2 D=3 E=4 F=5 G=6 H=7 I=8 K=9 L=10 M=11 (5 Replies)
Discussion started by: cdfd123
5 Replies

10. UNIX for Dummies Questions & Answers

How do i print this output all on the same line?

I have this simple script which gives info on HBA ports. How do i get it all to print on the same line? !#/bin/ksh TMP_INFOFILE=/tmp/tmpfile if ; then rm -f $TMP_INFOFILE touch $TMP_INFOFILE fi PORT_INFOFILE=/tmp/aa if ; then rm -f $PORT_INFOFILE ... (1 Reply)
Discussion started by: rcon1
1 Replies
Login or Register to Ask a Question