Appending a text to the top of each line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Appending a text to the top of each line
# 1  
Old 03-21-2018
Appending a text to the top of each line

Platform : Oracle Linux 6.8
Shell : bash

I have a file which has lines like below. These are SELECT queries (SQL)

In each line, I want the word just after FROM keyword to be copied and printed on the top along with the word PROMPT.
The words after FROM clause below are table names. So, they come like <schemaName>.<TableName> .

Both raw and expected output shown below


-- Raw file

Code:
SELECT AREA_ID, CODE, DESCRIPTION FROM SCOTT.SFO_AREA_MST;

SELECT EMP_ID, CODE, NAME, AREA_MST_ID, PARENT_ID FROM SCOTT.SFO_AREA_DTL where order_date > sysdate - 5 ;

SELECT OUTLET_AREA_ID, OUTLET_MST_ID, AREA_ID FROM APP_USR.SFO_OUTLET_AREA_MAPPING;

SELECT AREA_ID, AREA_USER_ID, AREA_MST_ID FROM SCOTT.SFO_USER_AREA_MAPPING;

SELECT EMP_ID, USER_ID, OUTLET_ID, AREA_ID, DEALER_ID FROM HR.EMP_MAPPING;

-- Expected output

Code:
PROMPT SCOTT.SFO_AREA_MST
SELECT AREA_ID, CODE, DESCRIPTION FROM SCOTT.SFO_AREA_MST;

PROMPT SCOTT.SFO_AREA_DTL
SELECT EMP_ID, CODE, NAME, AREA_MST_ID, PARENT_ID FROM SCOTT.SFO_AREA_DTL ;

PROMPT APP_USR.SFO_OUTLET_AREA_MAPPING
SELECT OUTLET_AREA_ID, OUTLET_MST_ID FROM APP_USR.SFO_OUTLET_AREA_MAPPING;

PROMPT SCOTT.SFO_USER_AREA_MAPPING
SELECT AREA_ID, AREA_USER_ID, AREA_MST_ID FROM SCOTT.SFO_USER_AREA_MAPPING;

PROMPT HR.EMP_MAPPING
SELECT EMP_ID, USER_ID, OUTLET_ID, AREA_ID, DEALER_ID FROM HR.EMP_MAPPING;

# 2  
Old 03-21-2018
Hello John K,

Could you please try following and let me know if this helps you.
Code:
awk 'NF{gsub(/where.*|;$/,"");print "PROMPT",$NF ORS $0";"}'  Input_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 03-21-2018
Thank You very much Ravinder
It works really well for my above requirement.


What if I have few words after the table name like the WHERE clause below.
Your above solution will not work for that(understandably).
What needs to be done to get it working

--Raw output

SELECT EMP_ID, CODE, NAME, AREA_MST_ID, PARENT_ID FROM SCOTT.HRTB_MASTER WHERE PSS_CREATE_DATE between SYSDATE-30 and sysdate;


--expected output

Code:
PROMPT SCOTT.HRTB_MASTER
SELECT EMP_ID, CODE, NAME, AREA_MST_ID, PARENT_ID FROM SCOTT.HRTB_MASTER WHERE PSS_CREATE_DATE between SYSDATE-30 and sysdate;

# 4  
Old 03-21-2018
Hello John K,

Could you please try following and let me know if this helps you.

Code:
awk '{val=$0;gsub(/.*FROM | WHERE.*/,"",val);print "PROMPT",val ORS $0}'  Input_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 03-21-2018
You could extend Ravindersingh13's proposal to
- work on upper and lower case keywords, both of which are allowed in SQL
- eliminate the trailing semicolon if where clause is missing
- keep empty lines for "paragraph separation"
like
Code:
awk 'NF {val=$0;gsub(/.*[fF][rR][oO][mM] | [wW][hH][eE][rR][eE].*|;$/,"",val);print "PROMPT",val} 1'  file

This User Gave Thanks to RudiC For This Post:
# 6  
Old 03-21-2018
Hello John_K,

Also adding to RudiC's point, you could use following command too in case your strings Where OR WHERE OR where could work by this command.
Code:
awk '{val=toupper($0);gsub(/.*FROM | WHERE.*/,"",val);print "PROMPT",val ORS $0}'   Input_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 03-21-2018
You can try this sed too
Code:
sed '/^$/!{h;s/.*[Ff][Rr][Oo][Mm] \([^ ;]*\).*/PROMPT \1/;G;}' infile


Last edited by ctac_; 03-21-2018 at 02:22 PM.. Reason: Missing filename
This User Gave Thanks to ctac_ For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grabbing top line of text in a file and setting it to a variable

If I have a txt file with test.txt somelineoftext and I want to set that line of text to variable in a script: so #!/bin/bash var='' becomes #!/bin/bash var='somelineoftext' (3 Replies)
Discussion started by: digitalviking
3 Replies

2. UNIX for Dummies Questions & Answers

sed - appending text

Hey all I am trying to append a file called datebook.txt. I want to append the end of each line containing the name Fred with three ***. I believe I need to make the * loose its meta character meaning in addition to using the append command. I have tried several variations of this command and I... (3 Replies)
Discussion started by: citizencro
3 Replies

3. Shell Programming and Scripting

Increasing a number and appending it to next line of a text file

Hi all, I have text file having a number P100. what i need is when i run a script, it should add 1 to the above number and append it to the next line of a same text file.. when i use the script next time it should check the last line and add 1 to the last number and so on.. like the text... (5 Replies)
Discussion started by: smarty86
5 Replies

4. Shell Programming and Scripting

Appending the first word of each line to the end of each line

Hi Experts, Am relatively new to shell programming so would appreciate some help in this regard. I am looking at reading from a file, line by line, picking the first word of each line and appending it to the end of the line. Any suggestions? INPUT FILE - 3735051 :... (7 Replies)
Discussion started by: hj007
7 Replies

5. Shell Programming and Scripting

awk;sed appending line to previous line....

I know this has been asked before but I just can't parse the syntax as explained. I have a set of files that has user information spread out over two lines that I wish to merge into one: User1NameLast User1NameFirst User1Address E-Mail:User1email User2NameLast User2NameFirst User2Address... (11 Replies)
Discussion started by: walkerwheeler
11 Replies

6. Shell Programming and Scripting

sed: appending alternate line after previous line

Hi all, I have to append every alternate line after its previous line. For example if my file has following contents line 1: unix is an OS line 2: it is open source line 3: it supports shell programming line 4: we can write shell scripts Required output should be line1: unix is an OS it is... (4 Replies)
Discussion started by: rish_max
4 Replies

7. UNIX for Dummies Questions & Answers

appending text on top of another file

Dear All, I have two files One is script file in which I am writing commands to append a text in a normal file. I want to insert the text on top of the file. I dont want to use sed and awk commands nor temp file. is it possible? (3 Replies)
Discussion started by: anjali
3 Replies

8. Shell Programming and Scripting

Appending line ending with '}" to new line

Hello masters. I have a rather simple problem but its been killing me. I have a file "x" with only 1 line inside it. The line looks something like Now this is only part of the line. Its actually about 4000 characters. What i need to do is whenever there is a "}", i need to append the next... (4 Replies)
Discussion started by: aismann
4 Replies

9. Shell Programming and Scripting

Appending the line number and a seperator to each line of a file ?

Hi, I am a newb as far as shell scripting and SED goes so bear with me on this one. I want to basically append to each line in a file a delimiter character and the line's line number e.g Change the file from :- aaaaaa bbbbbb cccccc to:- aaaaaa;1 bbbbbb;2 cccccc;3 I have worked... (4 Replies)
Discussion started by: pjcwhite
4 Replies

10. Shell Programming and Scripting

Appending Text To Each Line That Matches Grep

I'm currently digging for a way to append a line to a text file where each line begins with the word "setmqaut". This is a continuation of my IBM MQSeries backup script I'm working on to make my life a little easier. What I would like to do is have each line that looks like this: setmqaut -m... (4 Replies)
Discussion started by: sysera
4 Replies
Login or Register to Ask a Question