Visit Our UNIX and Linux User Community


Replacing first word while extracting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing first word while extracting
# 1  
Old 08-28-2013
Replacing first word while extracting

Hello All,

I am extracting a part of file. the file looks as follows

Code:
USING CHARACTER SET UTF8 DEFINE JOB 

(

DEFINE SCHEMA Flat_File_Schema
(
  cntnt_id VARCHAR(10)
);

DEFINE OPERATOR o_mload
 TYPE update
 SCHEMA *
 ATTRIBUTES
  (
    VARCHAR TdpId           = @TdpId
  );
DEFINE OPERATOR o_tpump
 TYPE stream
 SCHEMA *
 ATTRIBUTES
  (
    VARCHAR TdpId           = @TdpId
  );
DEFINE OPERATOR o_bteq
 TYPE inserter
 SCHEMA *
 ATTRIBUTES
  (
    VARCHAR TdpId           = @TdpId
  );
DEFINE OPERATOR o_fastload
 TYPE load
 SCHEMA *
 ATTRIBUTES
  (
    VARCHAR TdpId           = @TdpId
   ,VARCHAR UserName        = @UserName 
  );
DEFINE OPERATOR Read_Operator
 TYPE DATACONNECTOR PRODUCER
 SCHEMA Flat_File_Schema
 ATTRIBUTES
  (
    VARCHAR FileName         = @FileName 
);

APPLY
( 'INSERT INTO cntnt_cat_itm (
  cntnt_id
 ,cat_itm_nm
 ,prnt_cat_itm_nm
 ,updt_dt_tm
 ,load_dt_tm
 ,src_tbl_nm
 ,trans_type_cd
 ,jrnl_seq_nbr
 ,commit_locl_tm_zn
 ,batch_dt
 ,batch_id
 ) VALUES
(
 :cntnt_id
 
 
 );'
)
    IGNORE DUPLICATE ROWS
    SELECT * FROM OPERATOR (Read_Operator[@ReadInst]);
);

I want to extract the part of file highlighted in green color,
I did it this way

Code:
awk '/DEFINE SCHEMA/,/\)\;/ {if (NF==2) print}'  filename

Now I want to replace the first word of each line in extract with values increamenting t1 as filler, t2 as filler, t3 as filler
so in this case the output should look like this

t1 as filler VARCHAR

There will more more line in extract instead of only one as shown here
Please help me getting this.
# 2  
Old 08-28-2013
Like that?

Code:
bash-3.2$ awk 'BEGIN{x=1} /DEFINE SCHEMA/,/\)\;/ {if (NF==2) { $1="t"x" as filler"; print; x++ }}' filename
t1 as filler VARCHAR(10)

This User Gave Thanks to MR.bean For This Post:
# 3  
Old 08-28-2013
Code:
awk '{print "t"++i" as filler VARCHAR"}' extract_file

This User Gave Thanks to krishmaths For This Post:
# 4  
Old 08-28-2013
Hello MR.Bean

That was exactly what I was looking for.

Can you please take some time and explain me how this works.

Thanks in Advance
# 5  
Old 08-28-2013
Code:
bash-3.2$ awk 'BEGIN{x=1} /DEFINE SCHEMA/,/\)\;/ {if (NF==2) { $1="t"x" as filler"; print; x++ }}' filename

Code:
BEGIN{x=1}

Assign x with value "1". Then goes through the file.

Code:
/DEFINE SCHEMA/,/\)\;/

If it is within "DEFINE SCHEMA" and ");", it would be true (highlighted in red).

Code:
USING CHARACTER SET UTF8 DEFINE JOB 
 
(
 
DEFINE SCHEMA Flat_File_Schema
(
cntnt_id VARCHAR(10)
);
 
DEFINE OPERATOR o_mload
 TYPE update
 SCHEMA *

The if statement below looks for lines with 2 fields (highlighted in blue) and replaces $1 (field 1 which is cntnt_id) as "tX as filler" where X is then number "1". This would then be incremented by 1 using x++ after. X shall be "2" which will be used for later if we find the next match.

Code:
if (NF==2) { $1="t"x" as filler"; print; x++ }
 
DEFINE SCHEMA Flat_File_Schema
(
cntnt_id VARCHAR(10)
);

# 6  
Old 08-28-2013
Thanks for a detailed explanation MR.Bean

---------- Post updated at 03:57 PM ---------- Previous update was at 03:03 PM ----------

Hello,
For the second part of file.

I need the extract of file highlighted in orange.

I tried this command

Code:
awk 'BEGIN{x=1} /VALUES/,/\)\;/ {if (NF==1) { $2="as t"x"" ;print; x++ }}' brdcst_media_cntnt.tpt.ctl

The command gives me output with
(
);

I just want the highlighted text.

Can anybody help please

Previous Thread | Next Thread
Test Your Knowledge in Computers #322
Difficulty: Easy
HTML stands for Hypertext Markdown Language.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing the first word if the word three match

Dear ALL, I have sample file : IDcentos-forum,bash,linuxCentOS,GNome IEfedora-milis,cli,linuxRedhat,KDE IRfreebsd-milis,aix,unixbsd,pyton required output: centos,bash,linuxCentOS,GNome fedora,cli,linuxRedhat,KDE freebsd,aix,unixbsd,pyton Can you help me pls.. (1 Reply)
Discussion started by: gnulyn
1 Replies

2. Shell Programming and Scripting

Replacing a particular word with another word in all the xml's under a particular directory with sed

Hi Folks, Could you please advise what will be the SED command to replace a word in all xml's under a particular directory for example let say I rite now at the following below location $ cd /ter/rap/config now under config directory there will be lots of xml file , now my objective is to... (1 Reply)
Discussion started by: punpun66
1 Replies

3. Shell Programming and Scripting

Extracting a word from a variable

Hi Guys, Need you quick assistance on the below, trying to extract a word from a variable i.e. acmi101acmi102acmi103acmi104 When i use the following code awk '{gsub(/cmi102/,"")};1' it leaves a space in the variable, need to get rid of the space that it leaves. Any ideas. the above... (3 Replies)
Discussion started by: eo29
3 Replies

4. Shell Programming and Scripting

extracting sentences that only contain a word

Hi guys Need your help how do I extract sentences with only a word i.e. today is hot hot very humid humid2 Sample output hot (6 Replies)
Discussion started by: jamestan
6 Replies

5. Shell Programming and Scripting

Replacing a word

Dear Friends, I am facing some problem with some hundred files. Hence to use these file I have to make a small change in each file. But its not possible to open each file and make changes manually. Hence I need guidance frm you to automate it. Example File name is "Queue_11.txt" reload sec=F... (6 Replies)
Discussion started by: anushree.a
6 Replies

6. UNIX for Dummies Questions & Answers

regular expression for replacing the fist word with a last word in line

I have a File with the below contents File1 I have no prior experience in unix. I have just started to work in unix. My experience in unix is 0. My Total It exp is 3 yrs. I need to replace the first word in each line with the last word for example unix have no prior experience in... (2 Replies)
Discussion started by: kri_swami
2 Replies

7. Shell Programming and Scripting

Replacing word and Incrementing

Hi I'm having difficulty in writing a script with searching a specified word using sed and replaces that word with numbers that is incremented I tried this: #!/bin/sh awk '{ for (i=2010; i<=NF; i++) sed 's/TBA/$i; }' filename.txt > outputfile.txt but it doesn't work. here is my desired... (1 Reply)
Discussion started by: sexyTrojan
1 Replies

8. Shell Programming and Scripting

Replacing of word

Hi all, I wanted to replace one word with another word pl help me to solve the same. example:- I wanted to replace RXOTX with RXOTRX in a perticuler file with hole. Regards, Ramesh (2 Replies)
Discussion started by: Ramesh Vellanki
2 Replies

9. Shell Programming and Scripting

replacing files in an EAR file after extracting

{Hello All, Here is what I am trying to do: I have an archive file: AppName.ear jar -tvf AppName.ear 60459 Thu May 01 09:29:36 EDT 2008 /SharedResources/Application/App.dat 67542 Thu May 01 09:29:36 EDT 2008 JDBCConn/Application/App.dat 14488 Thu May 01 09:29:36 EDT 2008 config.xml ... (0 Replies)
Discussion started by: chiru_h
0 Replies

10. Shell Programming and Scripting

Extracting from second word

Hi all, I need to extract the Particular string from the whole word,the input file is : 123,345,aaaa,555,....,.... I need all the record from 345 so i need to eliminate the first record. Output: 345,aaa,5555,....,.....,..... Thanks in advance. (3 Replies)
Discussion started by: ithirak17
3 Replies

Featured Tech Videos