Sponsored Content
Top Forums Shell Programming and Scripting How to search for right word using awk? Post 302884735 by lv99 on Tuesday 21st of January 2014 01:49:05 PM
Old 01-21-2014
How to search for right word using awk?

Hi- I have issue with the code to get the input file reformat
--Goal is reformat and print out only column name one per line, leading space or trailing space will be removed...

--TABLE1.DDL as input file, and content of file as show below

Code:
REPLACE VIEW TABLE1
    (
     ID
    ,COL1
    ,COL2
    ,COL3
    ,COL4
    ,COL5
    ,COL6
    ,COL7
    )
AS LOCKING ROW FOR ACCESS
SELECT 
ID
CASE WHEN COL1 = 'Y' THEN '1' ELSE COL1 END,
CASE WHEN COL2 = 'Y' THEN '1' ELSE COL2 END ,
  CASE WHEN COL3 = 'Y' THEN '1' ELSE COL3 END,
CASE WHEN COL4 = 'Y' THEN '1' ELSE COL4 
END,
CASE WHEN COL5 = 'Y' THEN '1' ELSE COL5 END 
 (TITLE 'Field 5') 
 ,CASE WHEN COL6 = 'Y' THEN '1' ELSE COL6 END (TITLE 'Field 6'),
CASE WHEN COL7 = 'Y' THEN '1' ELSE COL7 END (TITLE 'Field 7') 
FROM TABLE_ABC
;

--code to reformat and print out the column in SELECT statement only:

Code:
DDL_NAME=TABLE1.DDL
        DDL_VIEW=TABLE1

        cat ${DDL_NAME}                  |
        awk '{ if( $0 ~ /\./ ) { print toupper($0) } else print $0 }' |
        sed -n "/${DDL_VIEW}/,/;/p"     |
        sed -e 's/Select/SELECT/g'              \
            -e 's/select/SELECT/g'              \
            -e 's/From/FROM/g'                  \
            -e 's/from/FROM/g'                  |
        sed -n '/SELECT/,/FROM /p'              |
        sed -n '/SELECT/,/FROM$/p'              |
        sed -e 's/SELECT//g'                    \
            -e '/FROM /,$d'                     \
            -e '/FROM$/,$d'                     |
         tr -d "\t"                             |
        sed -e "s/^ *//g"                       \
            -e "s/ *$//g"                       \
            -e "/^ *$/d"                        \
            -e 's/^,//'                         \
            -e 's/,$//'                         |
        awk '
        {
         if ( $0 ~ /CASE[ \t]/ && $0 !~ /END[\t]*$/ )
         {
                caseline=$0
                for (;;)
                {
                        getline currline
                        caseline=caseline" "currline
                        if ( currline ~ /END[\t]*$/ )
                        {
                                print caseline
                                break
                        }
                }
         }
         else
         {
                print $0
         }
        }'

result does not look right as what I wanted

--expect output:
Code:
ID
CASE WHEN COL1 = 'Y' THEN '1' ELSE COL1 END
CASE WHEN COL2 = 'Y' THEN '1' ELSE COL2 END
CASE WHEN COL3 = 'Y' THEN '1' ELSE COL3 END
CASE WHEN COL4 = 'Y' THEN '1' ELSE COL4 END
CASE WHEN COL5 = 'Y' THEN '1' ELSE COL5 END (TITLE 'Field 5') 
CASE WHEN COL6 = 'Y' THEN '1' ELSE COL6 END (TITLE 'Field 6')
CASE WHEN COL7 = 'Y' THEN '1' ELSE COL7 END (TITLE 'Field 7')

--test hung on COL5 and awk went into infinite loop. the COL3 also print on same line as COL2. that is wrong too.
Code:
ID
CASE WHEN COL1 = 'Y' THEN '1' ELSE COL1 END
CASE WHEN COL2 = 'Y' THEN '1' ELSE COL2 END  CASE WHEN COL3 = 'Y' THEN '1' ELSE COL3 END
CASE WHEN COL4 = 'Y' THEN '1' ELSE COL4 END
CASE WHEN COL5 = 'Y' THEN '1' ELSE COL5 END
(TITLE 'Field 5')

what should I change to get the expect result? still try to learn awk Smilie should it look for comma to format a line? and we don't have gawk.

Last edited by Corona688; 01-21-2014 at 02:56 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Word search in awk

If I have data as below : 1,ABC,XXXX 2,ABC000,YYYY 3,DEF,AAAA 4,ABC0,ZZZZ I want to get records whose 2nd col exactly match with word 'ABC' I am using command below : awk -F"," '$2~/ABC/' Filename It is retrieving records as - 1,ABC,XXXX 2,ABC000,YYYY 4,ABC0,ZZZZ But in... (8 Replies)
Discussion started by: videsh77
8 Replies

2. Shell Programming and Scripting

How can i open a directory and search a word using awk....

Hi guys,I m new one in awk scripting....i want to search a word with in a directory...in that directory haviing many files...if i give a work ,it should open the directory and open each file for to search that particular word....is it possible in awk...tell me guys its really urgent.... (2 Replies)
Discussion started by: stalin2020
2 Replies

3. Shell Programming and Scripting

search a word and print specific string using awk

Hi, I have list of directory paths in a variable and i want to delete those dirs and if dir does not exist then search that string and get the correct path from xml file after that delete the correct directory. i tried to use grep and it prints the entire line from the search.once i get the entire... (7 Replies)
Discussion started by: dragon.1431
7 Replies

4. Shell Programming and Scripting

Search the word to be deleted and delete lines above this word starting from P1 to P3

Hi, I have to search a word in a text file and then I have to delete lines above from the word searched . For eg suppose the file is like this: Records P1 10,23423432 ,77:1 ,234:2 P2 10,9089004 ,77:1 ,234:2 ,87:123 ,9898:2 P3 456456 P1 :123,456456546 P2 abc:324234 (2 Replies)
Discussion started by: vsachan
2 Replies

5. UNIX for Dummies Questions & Answers

Script to search for a particular word in files and print the word and path name

Hi, i am new to unix shell scripting and i need a script which would search for a particular word in all the files present in a directory. The output should have the word and file path name. For example: "word" "path name". Thanks for the reply in adv,:) (3 Replies)
Discussion started by: virtual_45
3 Replies

6. Shell Programming and Scripting

To search a word in particular column using awk

I have a data in a file like this 1 praveen bmscollege 2 shishira bnmit 3 parthiva geethamce I want to search "praveen" using awk command i tried like this but i did not get awk `$2="praveen" {print $0} ` praveen.lst can anyone help me solving this problem in... (2 Replies)
Discussion started by: praveenhegde
2 Replies

7. Shell Programming and Scripting

Search for the word and exporting 35 characters after that word using shell script?

I have a file input.txt which have loads of weird characters, html tags and useful materials. I want to display 35 characters after the word description excluding weird characters like $$#$#@$#@***$# and without html tags in the new file output.txt. Help me. Thanx in advance. My final goal is to... (11 Replies)
Discussion started by: sachit adhikari
11 Replies

8. Shell Programming and Scripting

Search for the word and exporting 35 characters after that word using shell script

I have a file input.txt which have loads of weird characters, html tags and useful materials. I want to display 35 characters after the word "description" excluding weird characters like $&lmp and without html tags in the new file output.txt. Help me. Thanx in advance. I have attached the input... (4 Replies)
Discussion started by: sachit adhikari
4 Replies

9. Shell Programming and Scripting

Search for a specific word and print only the word from the input file

Hi, I have a sample file as shown below, I am looking for sed or any command which prints the complete word only from the input file. Ex: $ cat "sample.log" I am searching for a word which is present in this file We can do a pattern search using grep but I need to cut only the word which... (1 Reply)
Discussion started by: mohan_kumarcs
1 Replies

10. UNIX for Beginners Questions & Answers

How to search for a word in column header that fully matches the word not partially in awk?

I have a multicolumn text file with header in the first row like this The headers are stored in an array called . which contains I want to search for each elements of this array from that multicolumn text file. And I am using this awk approach for ii in ${hdr} do gawk -vcol="$ii" -F... (1 Reply)
Discussion started by: Atta
1 Replies
All times are GMT -4. The time now is 05:48 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy