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/)

druuna 07-07-2010 05:40 AM

Hi,

Assuming you mean the awk command:

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

This leaves the original (infile.txt) as is and puts all changed entries in newfile.txt.

If you do need the output to be in the original file (after checking if all is ok with the above given command): mv newfile.txt infile.txt

BTW: Do not use the same name for the output and input file (i.e. awk '{ ... }' infile > infile), you will end up with an empty file!!

Hope this helps.

grail 07-07-2010 06:17 AM

You will need to redirect the output of your command to another file:
Code:

awk '' file1 > file2

shakes82 07-07-2010 08:36 AM

Thank you Drunna and Grail.

grail 07-07-2010 08:46 AM

No probs ... don't forget to mark as SOLVED :)

shakes82 07-07-2010 05:47 PM

Hi Druuna, grail, colucix ....I need some more help....
They gave me some more data is a slightly different format:
01/02/09 18:03:03 1.280550 1.281550
01/02/09 18:23:20 1.280570 1.281570
01/02/09 18:23:24 1.280270 1.281270
01/02/09 18:53:53 1.279970 1.280970
01/02/09 18:54:10 1.279810 1.280810
01/02/09 18:54:11 1.279780 1.280780
01/02/09 18:54:11 1.279770 1.280770
01/02/09 19:04:45 1.279500 1.280500
01/02/09 19:05:22 1.279500 1.280500
01/02/09 19:05:58 1.279500 1.280500

So now this data has the '/' character too in the first column. If I use:
awk -F"[ :]" '{ print $1 $2 $3 $4","$5","$6",SYM" }' infile.txt > newfile.txt
Then it only removes the ':' and not the '/' character. I tried something like:
awk -F"[ :,/]" '{ print $1 $2 $3 $4","$5","$6",SYM" }' infile.txt > newfile.txt, but it is not working.

Please let me know how I can get the above data in the following format:
090102180303,1.280550,1.281550,SYM

So in the first column I need to get rid of the '/' character and get the year first 09 then the month 01 and then the date 02. Then I need to remove both the separators, remove the space between first and second column. Eg.
01/02/09 18:03:03 1.280550 1.281550
to:
090102180303,1.280550,1.281550,SYM

Hoping to hear back soon. Thank you for your help.

pixellany 07-07-2010 05:57 PM

Code:

sed -e 's/\///g' -e 's/ //' -e 's/://g' -e 's/ /, /g' filename > newfilename

shakes82 07-07-2010 06:01 PM

Thanks pixellany, but I need to add "SYM" at the end to and change the year format in the beginning. Example:

01/02/09 18:03:03 1.280550 1.281550
to:
090102180303,1.280550,1.281550,SYM

pixellany 07-07-2010 06:05 PM

Code:

sed 's/$/, SYM/'
"$" means "at the end of the line"

or just another command string with "-e"

shakes82 07-07-2010 06:09 PM

Thanks again pixellany, but does this change the year from 01/02/09 to 090102 also?

pixellany 07-07-2010 06:09 PM

OOPs--I just saw that you wanted to change the order of the date terms--my code does not do that.

AWK is ideal for changing the order of something (just change the order of the print statements), but first you'd have to isolate the date string.

shakes82 07-07-2010 06:11 PM

Can you please suggest how I can achieve that exactly. I am very new with unix/linux but need to get this done soon.
Thank you for your help and time.

pixellany 07-07-2010 06:31 PM

Code:

awk -F" |/" '{print $3 $1 $2 $4" "$5" "$6}' filename | sed -e 's/://g' -e 's/ /, /g' -e 's/$/, SYM/' > newfilename

shakes82 07-07-2010 06:45 PM

Thanks alot pixellany, it works well but just a slight problem. I am getting a space after each ','. So I am getting something like:
090102180303, 1.280550, 1.281550, SYM
instead of:
090102180303,1.280550,1.281550,SYM
Can you please suggest how I can take care of this issue?

Thanks again.

colucix 07-07-2010 06:48 PM

...by means of awk only:
Code:

awk -F"[ /]" 'BEGIN{OFS=","}{gsub(/:/,"",$4); print $3 $1 $2 $4, $5, $6, "SYM"}' file

grail 07-07-2010 06:54 PM

Hi shakes ... whilst it is obvious that you are new to the likes of awk and sed you really are supposed to try and learn from the information given, otherwise you will just be back when you get
stuck again.

Anyhoo, if you look at the sed provided by pixellany you will see a space after each comma that is replacing items like a space or end of line.
Personally I would just leave it all in awk:
Code:

awk -F" |/" '{print $3 $1 $2 $4","$5","$6",SYM"}' filename > newfilename
As you can see this was not that different from the first solution presented to you.

Good luck

Edit: My bad ... go with colucix's solution as mine doesn't replace the ':'


All times are GMT -5. The time now is 05:17 PM.