How to use sed to look for the particular pattren and convert?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to use sed to look for the particular pattren and convert?
# 1  
Old 10-17-2013
How to use sed to look for the particular pattren and convert?

Hi ,

How do i use sed on a tsv file and look for the date format mm/dd/yyyy and convert it to yyyy-mm-dd. There are around 15 colums in that file and two colums need to be converted. i tried using this but didnt work.Can some one tweek it.

Code:
 sed -e "s_\(..\)\-\(..\)\-\(..\)_\3-\1-\2_" My file.tsv

Example : 10/23/2013 to 2013-10-23
# 2  
Old 10-17-2013
Your sed command is looking for the format that you want to transform to, not from. Also, if you want to transform more than 1 instance you'll need to specify the replace as global.

Try:
Code:
sed -e "s_\(..\)/\(..\)/\(....\)_\3-\1-\2_g" file.tsv

# 3  
Old 10-17-2013
Quote:
Originally Posted by CarloM
Your sed command is looking for the format that you want to transform to, not from. Also, if you want to transform more than 1 instance you'll need to specify the replace as global.

Try:
Code:
sed -e "s_\(..\)/\(..\)/\(....\)_\3-\1-\2_g" file.tsv

tried but no luck
# 4  
Old 10-17-2013
That's not very descriptive. What's your input, and what output do you get?
# 5  
Old 10-17-2013
Quote:
Originally Posted by CarloM
That's not very descriptive. What's your input, and what output do you get?
my Input is a tsv file

raju [tab] id[tab] name[tab] address[tab]......date[tab] numberid[tab] updateddate[tab].....


but the out put remains the same
# 6  
Old 10-17-2013
Are you using Solaris/SunOS? If so, try /usr/xpg4/bin/sed.
# 7  
Old 10-17-2013
Quote:
Originally Posted by CarloM
Are you using Solaris/SunOS? If so, try /usr/xpg4/bin/sed.
i am using
CentOS release 6.2 (Final)
and i did notice it changing only one field of date (its ignores the first date field and changes the second ) and in the file i found some thing like if the month is fast 9 its appers in 2 digits like 10 and same to date else its single digit

Code:
sed -e "s_\(..\)/\(.\)/\(....\)_\3-\1-\2_g


Last edited by vikatakavi; 10-17-2013 at 04:23 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove lines with Pattren Matching

Hi , I need to remove the lines that matches the pattern TABLEEXCLUDE *.AQ$_*_F ; * is wildcard, it can be any word. For example, I have following file: TABLEEXCLUDE THOT.AQ$_PT_ADDR_CLEANUP_QTAB2_F ; TABLEEXCLUDE THOT.AQ$_MICRO_SERVICE_QT_F ; TEST TABLEEXCLUDE... (1 Reply)
Discussion started by: rcc50886
1 Replies

2. Shell Programming and Scripting

Replace a string on specific lines which match a pattren

Hi Experts, I have a file which contains a pattern multiple times i.e. matchthispattren. If a line is matched with this pattern. I want a number in 1234567890 to 123456789 in that line. (Basically remove the last digit from that number. Please help. Thanks, Varun (1 Reply)
Discussion started by: varun22486
1 Replies

3. Shell Programming and Scripting

Convert string using sed

I have a couple structure definitions in my input code. For example: struct node { int val; struct node *next; }; or typedef struct { int numer; int denom; } Rational; I used the following line to convert them into one line and copy it twice. sed '/struct*{/{:l... (3 Replies)
Discussion started by: James Denton
3 Replies

4. UNIX for Dummies Questions & Answers

Get the previous word from the search pattren

Hi, How do i find the previous worlds from the searched pattrens? Input:- Create or replace procedure some tesx. search work is procedure(case insencitive). output:- Create or replace (8 Replies)
Discussion started by: manasa_vs
8 Replies

5. Shell Programming and Scripting

Convert sed to perl

Can anyone convert this from sed to perl: sed -n '/\var\/log/p' /etc/syslog.conf I think Ive looked at this to much....urgh.. Thanks Ben (5 Replies)
Discussion started by: bigben1220
5 Replies

6. Shell Programming and Scripting

find and replace pattren in file

Hi, I have the input file having data as follow: file1.txt 001 aaa_1:abcd 002 bbb_2:abcd I want output as, 001xabcd 002xabcd Here iam trying to replace "{1 space}{alphanumeric string with underscore}{:}" with characrter "x". I tried to achieve this using sed;but Iam not getting this... (5 Replies)
Discussion started by: gopalss
5 Replies

7. Shell Programming and Scripting

negate * with in pattren matching...

Hi Every one I have a file in the following manner... AAAAAA*PERFORM WRITEQ BBDFDD*PERFOMF WRITEQ FFFF *PERFOMF WRITEQ i want to find the lines which donot have * in 7th position.. I have tried this but some problem i think... grep '......*WRITEQ' INpFIle... any 6 chars not... (7 Replies)
Discussion started by: pbsrinivas
7 Replies

8. Shell Programming and Scripting

Problem with pattren Matching

I have a set of programs and there coressponding MAPSETs i tried grep on the all the programs and got the following out put from this i want to extract only the Program Name and Mapset name {i.e. the word in (' ') after MAPSET } There or some cases where u have no ( after MAPSET that need... (7 Replies)
Discussion started by: pbsrinivas
7 Replies

9. Shell Programming and Scripting

Search for a Pattren in the files.

Hi Guys, Can you please helpme with this: I would like to read all the files in a directory and need to search for a pattern, Can we use the grep at folder level. Thanks in advance. :) Sat. (2 Replies)
Discussion started by: sbasetty
2 Replies

10. Shell Programming and Scripting

How to convert grep to sed??

Hi, I am hing problem in grep.. So I need convert following code to sed based one.. grep -ie "error|exception" $LOG_DIR/Node$i\Log.txt >> $LOG_ERR_REP thats is I want to serach error and exception (ignore case) and write to other file . how to do? (5 Replies)
Discussion started by: redlotus72
5 Replies
Login or Register to Ask a Question