Replace a data in a field if that does not contain a particular pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace a data in a field if that does not contain a particular pattern
# 1  
Old 01-24-2012
Replace a data in a field if that does not contain a particular pattern

Hi,
I have a date/time field in my file. I have to search in all the records and append a timestamp to it, if the timestamp is missing in that field. Is there a possible awk solution for this?

Field date format [YYYY-MM-DD HH24:MI:SS]

Code:
File1
====
1|vamu|payer|2007-12-02 02:01:30|bcbs|
2|vane|payer|2008-02-01|welcr|
 
Expected output
1|vamu|payer|2007-12-02 01:12:20|bcbs| (no change)
2|vane|payer|2008-02-01 00:00:00|welcr| (Time stamp added)

# 2  
Old 01-24-2012
Try:
Code:
awk -F"|" '!(index($4," ")){$4=$4 " 00:00:00"}1' OFS="|" file

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 01-24-2012
Thanks. Can you explain me what the index($4," ") does here? How it is searching and finding only the record which has no timestamp in the date/time field. How to implement it if I have two are more fields?
# 4  
Old 01-24-2012
Quote:
Originally Posted by machomaddy
Thanks. Can you explain me what the index($4," ") does here? How it is searching and finding only the record which has no timestamp in the date/time field.
It searches for a space in the 4th field.

Quote:
Originally Posted by machomaddy
How to implement it if I have two are more fields?
Post an example of the file and the desired output.
# 5  
Old 01-24-2012
Code:
 
File1====
1|vamu|payer|2007-12-02 02:01:30|bcbs|2|vane|payer|2008-02-01|welcr|2009-05-12|welcr 
Expected output
1|vamu|payer|2007-12-02 01:12:20|bcbs| (no change)
2|vane|payer|2008-02-01 00:00:00|welcr|2009-05-12 00:00:00|welcr CoP| (Time stamp added to two fields and add a string to one of a field )

Also, will the code you gave work if I have a space at the begning of the field, like
Code:
2|vane|payer| <space>2008-02-01|welcr|2009-05-12|welcr

# 6  
Old 01-24-2012
If some of the fields have a space at the beginning of the field you could do something like:
Code:
awk -F"|" '!(index($4," ") && index($4,":")) || !(index($4," ")){$4=$4 " 00:00:00"}1' OFS="|" file

If the field numbers of the dates are random you could change the field one by one....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace pattern from nth field from a file

I have posted this again as old post is closed and I am not able to reopen. so please consider this new post Input File : 1,A,Completed,06.02_19.36,Jun 30 20:00 2,BBB,Failed,07.04_05.12,Jul 21 19:06 3,CCCCC,New,07.21_03.03,Jul 26 12:57 4,DDDDD,Pending,, I wast output file as: ... (7 Replies)
Discussion started by: Amit Joshi
7 Replies

2. Shell Programming and Scripting

Replace pattern from nth field from a file

$ cat /cygdrive/d/Final2.txt 1,A ,Completed, 07.03_23.01 ,Jun 30 20:00 2,BBB,Pending,, 3,CCCCC,Pending,, 4,DDDDD,Pending,, 5,E,Pending,, 6,FFFF,Pending,, 7,G,Pending,, In the above file 4th field is date which is in MM.DD_HH.MIN format and I need to convert it to as it is there in 5th... (1 Reply)
Discussion started by: Amit Joshi
1 Replies

3. Shell Programming and Scripting

Displaying the first field if the second field matches the pattern using Perl

Hi, I am trying with the below Perl command to print the first field when the second field matches the given pattern: perl -lane 'open F, "< myfile"; for $i (<F>) {chomp $i; if ($F =~ /patt$/) {my $f = (split(" ", $i)); print "$f";}} close F' dummy_file I know I can achieve the same with the... (7 Replies)
Discussion started by: royalibrahim
7 Replies

4. Shell Programming and Scripting

Need help to replace a pattern on specific line in a data file

Hi, I want to replace specific pattern "-2.0000 2" by "1.0000 3" on a particular line (line #5) in a file 1.dat. I have about 50 more files similar to 1.dat in which I want to do this correction. Can you please suggest how can I make change only at this particular line of a file... (6 Replies)
Discussion started by: anuj06
6 Replies

5. UNIX for Dummies Questions & Answers

Serach pattern in one field and replace in another

Hi all, I have a TAB separated file like this: sample.rpt: 54 67 common/bin/my/home {{bla bla bla}} {bla bla} Replace Me 89 75 bad/rainy/day/out {{ some bla} } {some bla} Dontreplace Me ...... ...... I wish to do a regexp match on the 3rd... (2 Replies)
Discussion started by: newboy
2 Replies

6. Shell Programming and Scripting

Replace column that matches specific pattern, with column data from another file

Can anyone please help with this? I have 2 files as given below. If 2nd column of file1 has pattern foo1@a, find the matching 1st column in file2 & replace 2nd column of file1 with file2's value. file1 abc_1 foo1@a .... abc_1 soo2@a ... def_2 soo2@a .... def_2 foo1@a ........ (7 Replies)
Discussion started by: prashali
7 Replies

7. Shell Programming and Scripting

Replace field in one file with whole record data of another

Hello Group, I need to replace the city field in “File 1 (fileld 3), with the entire record line of “File 2” (including delimiters) where the “city” field (File 1, Field 3)matches city field (File 2, Field1). All of the other data in “File 1” should remain intact(Fields 1,2,4,5,6). Only field... (1 Reply)
Discussion started by: vestport
1 Replies

8. UNIX for Dummies Questions & Answers

Match pattern in a field, print pattern only instead of the entire field

Hi ! I have a tab-delimited file, file.tab: Column1 Column2 Column3 aaaaaaaaaa bbtomatoesbbbbbb cccccccccc ddddddddd eeeeappleseeeeeeeee ffffffffffffff ggggggggg hhhhhhtomatoeshhh iiiiiiiiiiiiiiii ... (18 Replies)
Discussion started by: lucasvs
18 Replies

9. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

10. Shell Programming and Scripting

find pattern and replace another field

HI all I have a problem, I need to replace a field in a file, but only in the lines that have some pattern, example: 100099C01101C00000000059394200701CREoperadora_TX 100099C01201C00000000000099786137OPERADORA_TX2 in the example above I need to change the first field from 1 to 2 only if... (3 Replies)
Discussion started by: sergiioo
3 Replies
Login or Register to Ask a Question