move datestamp to beginning of line where available


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting move datestamp to beginning of line where available
# 1  
Old 04-09-2010
move datestamp to beginning of line where available

My current output is as follows:
Code:
All Day Event
Someone's Birthday
Class 7:00 PM
Pick up dry cleaning 1:00 PM
Wake up 8:00 AM

I'd like the output to remain the same but have the times moved to the beginning of the line with a hyphen after it so it would look like,

Code:
All Day Event
Someone's Birthday
7:00 PM - Class 
1:00 PM - Pick up dry cleaning
8:00 AM - Wake up

I assume that this can be done with awk and regex? I'm just not sure how.

I tried searching but have no idea what I'm trying to do would even be called.

The closest I got was
Code:
awk '{if ( $NF == "PM" || $NF == "AM" ) { print $(NF-1),$NF,"-",$0 } else {print $0}}'


Last edited by chrish; 04-09-2010 at 04:53 PM..
# 2  
Old 04-09-2010
Code:
nawk '$(NF-1) ~ "\:[0-9][0-9]" {print $(NF-1),$NF,"-",$0}' infile

# 3  
Old 04-09-2010
Code:
sed 's/\(.*\) \([0-9][0-9:]* [PA]M\)/\2 - \1/'  myFile

# 4  
Old 04-09-2010
Quote:
Originally Posted by EAGL€
Code:
nawk '$(NF-1) ~ "\:[0-9][0-9]" {print $(NF-1),$NF,"-",$0}' infile

my output is showing up as
Code:
All Day Event - All Day Event
Someone's Birthday - Someone's Birthday
7:00 PM - Class 7:00 PM
1:00 PM - Pick up dry cleaning 1:00 PM
8:00 AM - Wake up 8:00 AM





Quote:
Originally Posted by vgersh99
Code:
sed 's/\(.*\) \([0-9][0-9:]* [PA]M\)/\2 - \1/'  myFile

That did it. Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to add one line in the beginning of the file?

Hi gurus, I need add one new line in the begining of current file. current file abc cde add xyz output file newline abc cde add xyz (6 Replies)
Discussion started by: ken6503
6 Replies

2. Shell Programming and Scripting

beginning less from line #

Hi from a script i want to to read a file beginning at line e.g. number 21 to the EOF. less +n21 temp.txt Bevor the result, it brings an empty page, so that i cant use for scripting. Any idea how the problem can be solved? Thanks in advance! IMPe (2 Replies)
Discussion started by: IMPe
2 Replies

3. Shell Programming and Scripting

comment a line of the patterns is a the beginning of the line

I need to comment the lines starting with pattern "exclude" or "exclude=". If the work exclude comes at any other part, ignore it. Also, ignore, excludes, excluded etc. Ie only comment the line starting with exclude. File contents. exclude exclude= hi I am excluded excludes excludes= ... (9 Replies)
Discussion started by: anil510
9 Replies

4. UNIX for Dummies Questions & Answers

vim copy line and paste at the beginning, middle, and end of another line

How would you do vim copy line and paste at the beginning, middle, and end of another line. I know yy copies the whole line and p pastes the whole line, but on its own separate line. Sometimes I would like to copy a line to the beginning, middle, or end of another line. I would think this would be... (3 Replies)
Discussion started by: cokedude
3 Replies

5. UNIX for Dummies Questions & Answers

How to specify beginning-of-line/end-of-line characters inside a regex range

How can I specify special meaning characters like ^ or $ inside a regex range. e.g Suppose I want to search for a string that either starts with '|' character or begins with start-of-line character. I tried the following but it does not work: sed 's/\(\)/<do something here>/g' file1 ... (3 Replies)
Discussion started by: jawsnnn
3 Replies

6. Shell Programming and Scripting

Space at beginning of the line

How can I delete spaces at the begining of all lines of my file ? (2 Replies)
Discussion started by: Sara_84
2 Replies

7. UNIX for Dummies Questions & Answers

Regex for beginning of line until a comma

What is a regex for "the dalai lama, his holiness the" that would just grab "the dalai lama" and one that would just grab "his holiness the"? Both should exclude the comma.. I was trying '^.*' and many variants with no luck. (6 Replies)
Discussion started by: glev2005
6 Replies

8. Shell Programming and Scripting

Place variables at the beginning of each line

Hello all, I am very new to the shell scripting and I hope someone can help me with this. I have thousands of files with certain format of information and I need to do this for all my files. For each file, grab the numbers in the first and second rows and place them in the position 1 and 2... (8 Replies)
Discussion started by: GoldenFire
8 Replies

9. Shell Programming and Scripting

Unix Script with line number at beginning of each line.

Could anybody help me. I need to create a script that reads a text file from STDIN and prints out the file to STDOUT with line numbers at the beginning of each line. Thanks. (5 Replies)
Discussion started by: mascorro
5 Replies
Login or Register to Ask a Question