Replace pattern from nth field from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace pattern from nth field from a file
# 1  
Old 07-26-2015
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 :

Code:
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:
Code:
1,A,Completed,Jun 02 19:36,Jun 30 20:00
2,BBB,Failed,Jul 04 05:12,Jul 21 19:06
3,CCCCC,New,Jul 21 03:03,Jul 26 12:57 
4,DDDDD,Pending,,

Code:
07.21_03.03 is in format of MM.DD_HH.mm

In the above file 4th field is date which is in MM.DD_HH.mm format and I need to convert it to as it to into MON DD HH:mm . Note It can have blank value or space as well.
MM means Month like for 07 means July
DD means day like 21 means 21th day of that month
HH means hour
MIN means minute
MON means Month in 3 letter like for July it should be Jul


Any idea how I can do that in unix shell script with sed or awk or any other command ?
# 2  
Old 07-26-2015
What code have you tried?
# 3  
Old 07-26-2015
..2..

I have tried below command but it is replacing complete string not specified pattern.
I am not sure how to check for sub string here so please help.

Code:
awk -F, '{ OFS="," ; if ($4 ~ /07/) {$4="\"Jul\""}; if ($4 ~ /06/) {$5="\"Jun\""}; }1' File_Name

# 4  
Old 07-26-2015
awk -f amit.awk myFileGoesHere where amit.awk is:
Code:
BEGIN {
  FS=OFS=","
  split("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec", mon, FS)
}
$4 {
    split($4, a, "[._]")
    $4 = sprintf("%s %02d %02d:%02d", mon[a[1]+0], a[2], a[3], a[4])
}
1


Last edited by vgersh99; 07-26-2015 at 04:44 PM.. Reason: [._]
# 5  
Old 07-26-2015
..@..

Thanks vgersh99

It worked fine.

It would be very helpful if you can please explain what [._.] is doing and how it split 4th column into array and value assign to each array ?
# 6  
Old 07-26-2015
man awk yields:
Code:
       split(s, a [, r [, seps] ])
                               Split the string s into the  array  a  and  the
                               separators array seps on the regular expression
                               r, and return the number of fields.   If  r  is
                               omitted,  FS is used instead.  The arrays a and
                               seps are cleared first.  seps[i] is  the  field
                               separator matched by r between a[i] and a[i+1].
                               If r is a single space, then leading whitespace
                               in  s goes into the extra array element seps[0]
                               and trailing whitespace  goes  into  the  extra
                               array  element  seps[n],  where n is the return
                               value  of  split(s,  a,  r,  seps).   Splitting
                               behaves   identically   to   field   splitting,
                               described above.

# 7  
Old 07-26-2015
Quote:
Originally Posted by Amit Joshi
Thanks vgersh99

It worked fine.

It would be very helpful if you can please explain what [._.] is doing and how it split 4th column into array and value assign to each array ?
In reference to "what [._.] is doing" which vgersh99 didn't fully explain...

Note that the extended regular expression vgersh99 used was [._] (not [._.], although in this specific case it would do the same thing but take slightly longer to do it). That expression used as the third parameter to split() says that each occurrence of the <period> and <underscore> characters is to be used as a field separator when scanning the string found in the 4th field on that input line to split out substrings into elements in the array named a[].
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search term in nth field and replace kth column

Hi, I have a text file which looks like this a.txt A,12,Apple,Red B,33,Banana,Yellow C,66,Sky,Blue I need to search for a particular field(s) in particular column(s) and for that matching line need to replace the nth column. Sample scenario 1: Search for 66 in second field and Sky in... (5 Replies)
Discussion started by: wahi80
5 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

Replacing nth field with nth_text for each line in a file

Hi All, I am very new to shell scripting and tried to search this in the forum but no luck. Requirment: I have an input file which is comma separated. I need to replace the value in 4th column with another value. This has to happen for all the lines in the file. Sample data: Input... (2 Replies)
Discussion started by: arunkumarsd
2 Replies

4. Shell Programming and Scripting

Replace a value of Nth field of nth row

Using Awk, how can I achieve the following? I have set of record numbers, for which, I have to replace the nth field with some values, say spaces. Eg: Set of Records : 4,9,10,55,89,etc I have to change the 8th field of all the above set of records to spaces (10 spaces). Its a delimited... (1 Reply)
Discussion started by: deepakwins
1 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 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 File1 ==== 1|vamu|payer|2007-12-02 02:01:30|bcbs|... (5 Replies)
Discussion started by: machomaddy
5 Replies

7. Shell Programming and Scripting

Extract a nth field from a comma delimited file

Hi, In my file (which is "," delimited and text qualifier is "), I have to extract a particualr field. file1: 1,"aa,b",4 expected is the 2nd field: aa,b I tried the basic cut -d "," -f 2 file 1, this gave me aa alone instead aa,b. A small hint ot help on this will be very... (5 Replies)
Discussion started by: machomaddy
5 Replies

8. Shell Programming and Scripting

how to find the nth field value in delimiter file in unix using awk

Hi All, I wanted to find 200th field value in delimiter file using awk.? awk '{print $200}' inputfile I am getting error message :- awk: The field 200 must be in the range 0 to 199. The source line number is 1. The error context is {print >>> $200 <<< } using... (4 Replies)
Discussion started by: Jairaj
4 Replies

9. Shell Programming and Scripting

search pattern and replace x-y characters in nth line after every match

Hi, I am looking for any script which can do the following. have to read a pattern from fileA and copy it to fileB. fileA: ... ... Header ... ... ..p1 ... ... fileB: .... .... Header (3 Replies)
Discussion started by: anilvk
3 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