replace 9- 2ith 2009-


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace 9- 2ith 2009-
# 1  
Old 05-21-2009
replace 9- 2ith 2009-

hi guys,
i am tryin with this code:

cat final.csv | while read line
do
yr=`echo $line | cut -d"," -f2 | cut -d "-" -f1`
newyr=`expr "$yr" + 2000`
dash="-"
yr1=`echo $yr$dash`
newyr1=`echo $newyr$dash`
sed 's/$yr1/$newyr1/g' final.csv
done

final.csv:
1007004604,9-7
1007014100,9-7
1006004901,9-7
1006005000,9-7
1005017506,9-7
1006011004,9-7
1007017805,9-7
1007020200,9-7
1006012900,9-7
1007004222,9-7
1006006700,9-7
1005017119,9-7
1006006900,9-7
1006013404,9-7

where 9-year, 7-month
i want to replace year-9 as 2009 and month-7 as 07

please help me wid his piece of code!!!

thx a lot!!!
# 2  
Old 05-21-2009
Please provide sample data.
# 3  
Old 05-21-2009
if you have Python,
Code:
#!/usr/bin/env python
for line in open("file"):    
    line=line.strip().split(",")
    yr,mth = line[-1].split("-")
    if len(yr) <2: yr="0"+yr
    if len(mth) <2: mth="0"+mth
    print "%s,20%s-%s" %(line[0],yr,mth)

output
Code:
# ./test.py
1007004604,2009-07
1007014100,2009-07
1006004901,2009-07
1006005000,2009-07
1005017506,2009-07
1006011004,2009-07
1007017805,2009-07
1007020200,2009-07
1006012900,2009-07
1007004222,2009-07
1006006700,2009-07
1005017119,2009-07
1006006900,2009-07
1006013404,2009-07

# 4  
Old 05-22-2009
Code:
echo 1007004604,9-7 | sed 's/\([0-9]\)-\([0-9]\)/200\1-0\2/'

# 5  
Old 05-22-2009
Quote:
Originally Posted by summer_cherry
Code:
echo 1007004604,9-7 | sed 's/\([0-9]\)-\([0-9]\)/200\1-0\2/'

considering the possibility of the month can be 2 digits
Code:
# echo 1007004604,9-10 | sed 's/\([0-9]\)-\([0-9]\)/200\1-0\2/'
1007004604,2009-010

Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk script to extract a column, replace one of the header and replace year(from ddmmyy to yyyy)

I have a csv which has lot of columns . I was looking for an awk script which would extract a column twice. for the first occurance the header and data needs to be intact but for the second occurance i want to replace the header name since it a duplicate and extract year value which is in ddmmyy... (10 Replies)
Discussion started by: Kunalcurious
10 Replies

2. Red Hat

RHSA-2009:1580-02

Hi All, There's a new upgrade for apache software, RHSA-2009:1580-02. My red hat ent es 4 has only this httpd-manual-2.0.52-25.ent Do I really need to upgrade this? Is this just the manual or it has more functionality than that? Thanks for any comment you may add. (1 Reply)
Discussion started by: itik
1 Replies

3. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies
Login or Register to Ask a Question