LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-07-2010, 05:40 AM   #16
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400

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.
 
1 members found this post helpful.
Old 07-07-2010, 06:17 AM   #17
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,843

Rep: Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097
You will need to redirect the output of your command to another file:
Code:
awk '' file1 > file2
 
1 members found this post helpful.
Old 07-07-2010, 08:36 AM   #18
shakes82
LQ Newbie
 
Registered: Jun 2010
Posts: 21

Original Poster
Rep: Reputation: 15
Thank you Drunna and Grail.
 
Old 07-07-2010, 08:46 AM   #19
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,843

Rep: Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097
No probs ... don't forget to mark as SOLVED
 
Old 07-07-2010, 05:47 PM   #20
shakes82
LQ Newbie
 
Registered: Jun 2010
Posts: 21

Original Poster
Rep: Reputation: 15
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.

Last edited by shakes82; 07-07-2010 at 05:59 PM.
 
Old 07-07-2010, 05:57 PM   #21
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Code:
sed -e 's/\///g' -e 's/ //' -e 's/://g' -e 's/ /, /g' filename > newfilename
 
Old 07-07-2010, 06:01 PM   #22
shakes82
LQ Newbie
 
Registered: Jun 2010
Posts: 21

Original Poster
Rep: Reputation: 15
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
 
Old 07-07-2010, 06:05 PM   #23
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Code:
sed 's/$/, SYM/'
"$" means "at the end of the line"

or just another command string with "-e"
 
Old 07-07-2010, 06:09 PM   #24
shakes82
LQ Newbie
 
Registered: Jun 2010
Posts: 21

Original Poster
Rep: Reputation: 15
Thanks again pixellany, but does this change the year from 01/02/09 to 090102 also?
 
Old 07-07-2010, 06:09 PM   #25
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.
 
Old 07-07-2010, 06:11 PM   #26
shakes82
LQ Newbie
 
Registered: Jun 2010
Posts: 21

Original Poster
Rep: Reputation: 15
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.
 
Old 07-07-2010, 06:31 PM   #27
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Code:
awk -F" |/" '{print $3 $1 $2 $4" "$5" "$6}' filename | sed -e 's/://g' -e 's/ /, /g' -e 's/$/, SYM/' > newfilename
 
1 members found this post helpful.
Old 07-07-2010, 06:45 PM   #28
shakes82
LQ Newbie
 
Registered: Jun 2010
Posts: 21

Original Poster
Rep: Reputation: 15
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.
 
Old 07-07-2010, 06:48 PM   #29
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982
...by means of awk only:
Code:
awk -F"[ /]" 'BEGIN{OFS=","}{gsub(/:/,"",$4); print $3 $1 $2 $4, $5, $6, "SYM"}' file
 
Old 07-07-2010, 06:54 PM   #30
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,843

Rep: Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097
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 ':'

Last edited by grail; 07-07-2010 at 06:57 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk, sed, grep and paragraphs ThinkLinux Linux - Newbie 3 04-09-2010 01:22 PM
help with grep/sed/awk nikunjbadjatya Programming 8 02-17-2010 07:29 PM
bash - awk, sed, grep, ... advice schneidz Programming 13 08-25-2008 09:30 AM
awk/sed to grep the text ahpin Linux - Software 3 10-17-2007 12:34 AM
diffrence between grep, sed, awk and egrep Fond_of_Opensource Linux - Newbie 3 08-18-2006 08:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration