Computing.Net > Forums > Unix > sed/awk search and replace

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

sed/awk search and replace

Reply to Message Icon

Name: cstone33
Date: July 5, 2007 at 12:33:24 Pacific
OS: HP-UX 11.11
CPU/Ram: 9000/800
Product: HP
Comment:

I need help with either sed/awk or something else for unix replacing a set of numbers that is in the 2nd row and 3rd column of a file. these numbers represent a date "073006", I need to add the slashes so it looks like "07/30/06".




Sponsored Link
Ads by Google

Response Number 1
Name: rais
Date: July 5, 2007 at 14:48:09 Pacific
Reply:

You can use 'substr' function of awk.

Do an: awk '{print substr($1,0,2)"/"substr($1,3,2)"/"substr($1,5,2);}'

Hope this helps.
Rais.


0

Response Number 2
Name: cstone33
Date: July 5, 2007 at 18:06:52 Pacific
Reply:

couldn't quite get it to work, here's more data, i have file A:

There is a title in this area
Entry Date: 20070629
More lines here

I need to get to 20070629 and change it to read 06/29/07 and then save file A to file B so I can ftp it another server. That's why I was asking how to get to the 2nd row in my file and the 3rd column.

thanks


0

Response Number 3
Name: ghostdog
Date: July 6, 2007 at 05:34:13 Pacific
Reply:

awk 'NR==2{$3=substr($3,1,2)"/"substr($3,3,2)"/"substr($3,5,2)}{print}' file


0

Response Number 4
Name: cstone33
Date: July 6, 2007 at 10:47:19 Pacific
Reply:

that almost worked, instead of getting 06/29/07, it made it 20/07/06, also it put slashes through out the file, can I limit it to just put slashes for just that one area? my file has over hundred lines and i need to edit just third line, but i need to make the change in the third line but keep everything just like it is for the first two lines as well as the rest of the file.

thanks again, i really appreciate all the help.


0

Response Number 5
Name: ghostdog
Date: July 6, 2007 at 18:24:17 Pacific
Reply:

no it will not put slashes throughout because NR==2, which means at record number 2. If you want it to be at third line, use NR==3. $3 means third column, so the example i give only make changes to the 3rd column. as for the date, you just have to substr the correct parts out.I have shown you in the example, so you should be able to apply it ..


0

Related Posts

See More



Response Number 6
Name: cstone33
Date: July 9, 2007 at 17:02:51 Pacific
Reply:

Okay, i'm almost there, 070531 when i do my
awk 'NR==1{$6=substr($6,3,4)"/"substr($6,5,6)"/"substr($6,1,2){print}' file gives me 0531/31/07, i can't seem to get rid of that first 31. if I change my 3,4 to 1,2 i get 07/31/07, so why is it that my 3,4 doesn't give me just the 05?


0

Response Number 7
Name: ghostdog
Date: July 9, 2007 at 18:17:08 Pacific
Reply:

you have to read the awk manual on how to use substr, not just blind experimenting. substr($6,3,4) means get from position 3 and then 4 characters after that. what you need is 2 characters , not 4.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: sed/awk search and replace

Search And Replace With Sed www.computing.net/answers/unix/search-and-replace-with-sed/3845.html

Search and replace string in files www.computing.net/answers/unix/search-and-replace-string-in-files/7857.html

Unix - Search and replace www.computing.net/answers/unix/unix-search-and-replace/6775.html