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 08-05-2009, 12:56 PM   #1
mierdatuti
Member
 
Registered: Aug 2008
Posts: 61

Rep: Reputation: 15
all in one line


Hi,

I have one files where I need to extract the date.
I do:

Code:
grep -i start file | sed -e 's/.*start="\([^ ]*\) .*/\1/' > fileoutput
I get:
20090805030000
20090806070000
20090806090000
20090806093000
20090806100000
........

Know I would like to extract this character:

20090805030000 to 090805-0300
20090806070000 to 090806-0700
20090806090000 to 090806-0900

For this I use:
Code:
grep -i start ${ruta}la6 | sed -e 's/.*start="\([^ ]*\) .*/\1/' | cut -c 3-8 > temporalA
grep -i start ${ruta}la6 | sed -e 's/.*start="\([^ ]*\) .*/\1/' | cut -c 9-12 | sed "s/^/-/g" > temporalB
And then I paste them.

The problem is that I can't generete auxiliary files. Could you help me?

Many thanks and sorry for my english!
 
Old 08-05-2009, 02:17 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 110Reputation: 110
Code:
grep -i start file | sed 's/.*start="[0-9]\{2\}\([0-9]\{6\}\)\([0-9]\{4\}\).*/\1-\2/'
 
Old 08-05-2009, 02:23 PM   #3
mierdatuti
Member
 
Registered: Aug 2008
Posts: 61

Original Poster
Rep: Reputation: 15
Talking

Quote:
Originally Posted by Hko View Post
Code:
grep -i start file | sed 's/.*start="[0-9]\{2\}\([0-9]\{6\}\)\([0-9]\{4\}\).*/\1-\2/'
Many thanks guru!!!!!!
It works.
 
Old 08-05-2009, 03:40 PM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Have you looked at the gawk language? I think your whole task could be quite easily done with a simple gawk program.

If you could post a few lines from your input file, we could probably put together such a program for you. For example, your first script could be done like this:
Code:
BEGIN {IGNORECASE=1;FS=""}
/start *=/ {
   if (match($0,/([[:digit:]]+)/, Date)) {
      $0=Date[0]
      printf "%s-%s\n", $3 $4 $5 $6 $7 $8, $9 $10 $11 $12;
   }
}
For this data:
Code:
$ cat date.txt
Start=20090805030000
End=20090806070000
Start = 20090806090000
Start= 20090806093000
Start =20090806100000
I get this output:
Code:
$ gawk -f date.awk date.txt
090805-0300
090806-0900
090806-0900
090806-1000
Note that the above is a "quick-and-dirty" example: More elegant code should be used for a production application.

Last edited by PTrenholme; 08-06-2009 at 12:09 PM. Reason: Brain f*** in printf
 
  


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
Removing new line characters on every line execpt first line bioinformatics_guy Linux - Newbie 4 10-21-2008 12:41 PM
grab the line below a blank line and the line above the next blank line awk or perl? Pantomime Linux - General 7 06-26-2008 08:13 AM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 PM

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

All times are GMT -5. The time now is 04:51 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