Replace a word based upon the file content


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace a word based upon the file content
# 1  
Old 10-22-2018
Replace a word based upon the file content

I have a file which contains timestamp and date in the second column. If the line contains one of the word then it need to be replace like below. Any help is appreciated.

File:
Code:
  a smallint
    b timestamp
    c date
    d varchar

O/P:
Code:
    a smallint
    dateformat(b,'YYYY-MM-DD HH:NN:SS.sss')
    dateformat(c.'YYYY-MM-DD')
    d varchar

Moderator's Comments:
Mod Comment Code tags for data please


------ Post updated at 06:21 PM ------

Code:
awk '{if ($2=="dateformat') {$1=dataformat($1,'YYYY-MM-DD HH:NN:SS.sss')} else {$2=1}}

------ Post updated at 06:27 PM ------

Code:
awk '{if ($2=="dateformat") {$1=dataformat($1,'YYYY-MM-DD HH:NN:SS.sss')} else {$2=1}}' test.out

------ Post updated at 06:34 PM ------

sorry, I have entered dateformat instead of timestamp but still its not working

Code:
awk '{if ($2=="timestamp") {$1="dataformat($1,'YYYY-MM-DD')"} else {$2=1}}' test.out

------ Post updated at 07:40 PM ------

If I execute the statement in a separate awk statement then its working fine but If I add in the else condition then I am getting error.

Code:
awk '{if ($2=="timestamp") {$3="dataformat(";   }; print $3 $1 ",'\''YYYY-MM-DD HH'\:'NN'\:'SS'\.'sss)" else ($2=="date") {$3="dataformat(";   }; print $3 $1 ",'\''YYYY-MM-DD)" }' test.out

Error:
Code:
awk: {if ($2=="timestamp") {$3="dataformat(";   }; print $3 $1 ",'YYYY-MM-DD HH:NN:SS.sss)" else ($2=="date") {$3="dataformat(";   }; print $3 $1 ",'YYYY-MM-DD)" }
awk:                                                                                        ^ syntax error
awk: {if ($2=="timestamp") {$3="dataformat(";   }; print $3 $1 ",'YYYY-MM-DD HH:NN:SS.sss)" else ($2=="date") {$3="dataformat(";   }; print $3 $1 ",'YYYY-MM-DD)" }
awk:                                                                                                                                  ^ syntax error

------ Post updated at 08:45 PM ------

Code:
awk '{if ($2=="timestamp") {$3="dataformat("; }; print $3 $1 ",'\''YYYY-MM-DD HH'\:'NN'\:'SS'\.'sss)" else ($2=="date") {$3="dataformat("; }; print $3 $1 ",'\''YYYY-MM-DD)" }' test.out

Quote:
Error:
awk: {if ($2=="timestamp") {$3="dataformat("; }; print $3 $1 ",'YYYY-MM-DD HH:NN:SS.sss)" else ($2=="date") {$3="dataformat("; }; print $3 $1 ",'YYYY-MM-DD)" }
awk: ^ syntax error
awk: {if ($2=="timestamp") {$3="dataformat("; }; print $3 $1 ",'YYYY-MM-DD HH:NN:SS.sss)" else ($2=="date") {$3="dataformat("; }; print $3 $1 ",'YYYY-MM-DD)" }
awk: ^ syntax error

Last edited by Don Cragun; 10-22-2018 at 10:42 PM.. Reason: Add missing CODE tags again.
# 2  
Old 10-23-2018
Unfortunately, none of your attempts targets close to what you specify. Did you analyse the very precise error msgs, showing the exact location of the syntax fault? Are you sure about your fields' distribution (you specify two fields but reference $3)?

There are umpteen ways to skin a cat. How far would this get you?
Code:
awk -v SQ="'" '
$2 == "timestamp"       {$0 = "dataformat(" SQ "YYYY-MM-DD HH:NN:SS.sss)" SQ}
$2 == "date"            {$0 = "dataformat(" SQ "YYYY-MM-DD)" SQ}
1
' file
a smallint
dataformat('YYYY-MM-DD HH:NN:SS.sss)'
dataformat('YYYY-MM-DD)'
d varchar


EDIT: So close to this (which I saw only now...).

Last edited by RudiC; 10-23-2018 at 07:55 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to create file and file content based existing information?

Hi Gurus, I am SQL developer and new unix user. I need to create some file and file content based on information in two files. I have one file contains basic information below file1 and another exception file file2. the rule is if "zone' and "cd" in file1 exists in file2, then file name is... (13 Replies)
Discussion started by: Torhong
13 Replies

2. Shell Programming and Scripting

Shell Script to Dynamically Extract file content based on Parameters from a pdf file

Hi Guru's, I am new to shell scripting. I have a unique requirement: The system generates a single pdf(/tmp/ABC.pdf) file with Invoices for Multiple Customers, the format is something like this: Page1 >> Customer 1 >>Invoice1 + invoice 2 >> Page1 end Page2 >> Customer 2 >>Invoice 3 + Invoice 4... (3 Replies)
Discussion started by: DIps
3 Replies

3. Shell Programming and Scripting

Sed: replace content from file with the content from file

Hi, I am having trouble while using 'sed' with reading files. Please help. I have 3 files. File A, file B and file C. I want to find content of file B in file A and replace it by content in file C. Thanks a lot!! Here is a sample of my question. e.g. (file A: a.txt; file B: b.txt; file... (3 Replies)
Discussion started by: dirkaulo
3 Replies

4. Shell Programming and Scripting

Split the file based on the content

Arun kumar something somehting Enterting in to the line . . . . Some text text Finshing the sentence Some other text . . . . Again something somehting Enterting in to the line . . . . . . Again text text Finshing the sentence (6 Replies)
Discussion started by: arukuku
6 Replies

5. Shell Programming and Scripting

Help with replace column one content based on reference file

Input file 1 testing 10 20 1 A testing 20 40 1 3 testing 23 232 2 1 testing 10 243 2 . . Reference file 1 final 3 used . . Output file (1 Reply)
Discussion started by: perl_beginner
1 Replies

6. Shell Programming and Scripting

Help with replace column one content based on reference file

Input file 1 testing 10 20 1 A testing 20 40 1 3 testing 23 232 2 1 testing 10 243 2 . . Reference file 1 final 3 used . . Output file (2 Replies)
Discussion started by: perl_beginner
2 Replies

7. Shell Programming and Scripting

Replace a word after a particular word in a file

Hi, I want to replace a word in a file which occurs after a particular word. For example : $cat file.txt CASE WHEN AND c1 = 'I' AND c2= '2' THEN 1 WHEN AND c1= 'I' AND c2= '0' THEN 2 So in this example i want to replace... (4 Replies)
Discussion started by: ashwin3086
4 Replies

8. Shell Programming and Scripting

Change file content based on data

I have a Transaction File coming into the system. In this file, in all records the relevant data is as follows- Position 1:10 -> Transaction Code Position 252:255 -> 4 digit business code Now based on these 2 fields I have to alter value in Transaction code (Position 1:10)... (6 Replies)
Discussion started by: varunrbs
6 Replies

9. Shell Programming and Scripting

Dynamic naming based on file content

I was just thinking if there is a way where i can dynamically rename files based on the actual file content. I have a load of pdf's which have been named wrongly. (We normally put date first, then brief description, then title) So can a script be written wherin, it pulls out the date and title... (2 Replies)
Discussion started by: deaddevil
2 Replies

10. UNIX for Dummies Questions & Answers

find filename based on file content

:confused: There is a flat file on my system which contains email addreses of people in my company. This file is utilized when sending notifications for various things. However nobody knows where this file is located or what it is named. The only thing we know is the email address of a user who... (4 Replies)
Discussion started by: kollerj
4 Replies
Login or Register to Ask a Question