LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Help using awk,sed and grep (https://www.linuxquestions.org/questions/programming-9/help-using-awk-sed-and-grep-818444/)

shakes82 07-07-2010 03:50 AM

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.

druuna 07-07-2010 03:56 AM

Hi,

Something like this?

awk -F"[ :]" '{ print $1 $2 $3 $4","$5","$6",SYM" }' infile

Hope this helps.

shakes82 07-07-2010 04:10 AM

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.

colucix 07-07-2010 04:26 AM

Quote:

Originally Posted by shakes82 (Post 4025990)
,,,sym02 180203 1.5 1.6

Hmm.. this means that the input you've used is not what you've shown in the original post, since there is no way for the string "sym02" to appear from the command suggested by druuna (if copied exactly). Also which version of awk are you running and which Linux/Unix release?

Anyway, I suspect the fields in the input file are not separated by blank spaces. Maybe tabs?

druuna 07-07-2010 04:26 AM

Hi,

It works on my side.
Code:

$ cat infile
20090302 18:02:03 1.5 1.6

$ awk -F"[ :]" '{ print $1 $2 $3 $4","$5","$6",SYM" }' infile
20090302180203,1.5,1.6,SYM

I just noticed I'm using mawk.

Tried it with awk: Works as well.

Code:

awk --version
GNU Awk 3.1.6

Which distro and which awk version are you using?

shakes82 07-07-2010 04:52 AM

@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

druuna 07-07-2010 04:57 AM

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?

shakes82 07-07-2010 05:00 AM

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.

druuna 07-07-2010 05:04 AM

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

shakes82 07-07-2010 05:06 AM

It shows me:
fx.txt: ASCII text, with CRLF line terminators

Thanks

druuna 07-07-2010 05:15 AM

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.

shakes82 07-07-2010 05:16 AM

Thanks Druuna. I will try it and let you know how it goes. Thanks again.

druuna 07-07-2010 05:18 AM

You're welcome :)

shakes82 07-07-2010 05:19 AM

Thanks alot Druuna. It worked perfectly. I appreciate your help. Thanks for your time.

shakes82 07-07-2010 05:31 AM

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 11:02 PM.