Help using awk,sed and grep
Hi. I have some financial data in the the following format:
20090302 18:02:03 1.5 1.6 I want to change this to the following format using grep awk and sed: 20090302180203,1.5,1.6,SYM Please suggest how I can use the commands to get this formatting. Thank you. |
Hi,
Something like this? awk -F"[ :]" '{ print $1 $2 $3 $4","$5","$6",SYM" }' infile Hope this helps. |
Thanks Druuna but it didn't work. I am getting the following output with your suggestion:
,,,sym02 180203 1.5 1.6 Any other suggestions? Thanks for your time. |
Quote:
Anyway, I suspect the fields in the input file are not separated by blank spaces. Maybe tabs? |
Hi,
It works on my side. Code:
$ cat infile Tried it with awk: Works as well. Code:
awk --version |
@colucix: The fields are separated by a space. I think I am getting sym02 because it is taking 02 from 20090302 and adding sym before.
I am using fedora 13. awk version: 3.1.7 @druuna: I am using fedora 13. awk version: 3.1.7 I tried it again exactly as: awk -F"[ :]" '{ print $1 $2 $3 $4","$5","$6",SYM" }' infile with the same spacing and everything but I am getting the following output: ,sym0302180203,1.5,1.6 I think what it is adding ,sym infront and that is why 2009 is replaced by ,sym Can you please suggest what I can do to make it right? Thank you |
Hi,
Could you post a relevant example? Like colucix already said it looks like your input file is not the same as your example posted in post #1. The problem is not the awk command I posted with the example posted by you (shown by me and confirmed by colucix). I can come up with one thing that could be causing this: Is the infile a unix or a dos file? |
Following are a few lines of the data:
20090102 18:03:03 1.280550 1.281550 20090102 18:23:20 1.280570 1.281570 20090102 18:23:24 1.280270 1.281270 20090102 18:53:53 1.279970 1.280970 20090102 18:54:10 1.279810 1.280810 It is a *.txt (text) file. |
Hi,
Unix/linux works different then windows. The fact that the file has a .txt extension doesn't say anything at all. What does the following command show you: file infile.txt |
It shows me:
fx.txt: ASCII text, with CRLF line terminators Thanks |
Hi,
That is a file with dos/windows terminators (CRLF) and it is also the reason why it doesn't work. Most (all?) unix/linux tools do not work too well with dos/windows files. Here a link that gives a few examples of how to change a dos file to a unix file (do make a backup of the original before trying them out!!). HowTo: UNIX / Linux Convert DOS Newlines CR-LF to Unix/Linux Format Hope this helps. |
Thanks Druuna. I will try it and let you know how it goes. Thanks again.
|
You're welcome :)
|
Thanks alot Druuna. It worked perfectly. I appreciate your help. Thanks for your time.
|
Since I am new with unix, can you please suggest how I can save the changes to the file after using the command. Thanks
|
All times are GMT -5. The time now is 02:38 PM. |