LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-12-2009, 11:11 AM   #1
shayno90
Member
 
Registered: Oct 2009
Distribution: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 187
Blog Entries: 2

Rep: Reputation: 21
How to get a command to run through each line of text


Hi, just wondering can anyone tell me what is the appropriate way to get a line of code to run through each line, I know you have to pipe the file with the code but am having trouble with it. Here is a sample from the text (it has alot of lines!) I want to convert and the code to do it (would take ages to do line by line)

2009-10-01 02:53:07
2009-10-01 05:00:01
2009-10-01 07:59:09
2009-10-01 08:02:22
2009-10-01 08:05:57
2009-10-01 08:05:57
2009-10-01 08:05:57
2009-10-01 08:06:08
2009-10-01 08:06:08
2009-10-01 08:06:17


#! /bin/sh
dateandtime=`echo "datestime.txt" |date -d "2009-10-09 08:39:59" +%s

Output will be like
1255073999

I am not sure how to get it to read through each line? Any ideas anybody has would be appreciated!
 
Old 10-12-2009, 11:43 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,414

Rep: Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967
I'm sure one should use xargs, but my immediate way would be...

for i in $(cat file)
do
dateandtime=$(date -d "$i" +%s)
done

don't use backticks btw, use $(...) instead.

Ahh, xargs...

xargs -a file -I XXX date -d XXX +"XXX is %s as a unix timestamp"

Last edited by acid_kewpie; 10-12-2009 at 11:48 AM.
 
Old 10-13-2009, 06:47 AM   #3
shayno90
Member
 
Registered: Oct 2009
Distribution: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 187
Blog Entries: 2

Original Poster
Rep: Reputation: 21
Thanks acid kewpie, xargs was a much simpler command to output the time to UTC
 
Old 10-13-2009, 07:30 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,414

Rep: Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967
xargs is a command that's been a bit of a nemesis for a while now, finally forcing myself to try to understand and use it instead of a million for loops.
 
Old 10-13-2009, 12:49 PM   #5
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 101Reputation: 101
Quote:
Originally Posted by acid_kewpie View Post
xargs is a command that's been a bit of a nemesis for a while now, finally forcing myself to try to understand and use it instead of a million for loops.
If "xargs" can do the job, then one loop can also, and the resulting script can be repurposed. Any time more than one loop is needed, then "xargs" is not just bad judgment but out of the question.

Bash programming doesn't have to look like crap -- that's optional and at the discretion of the programmer:

Code:
for ((y = 1;y <= 12;y++))
do
   for ((x = 1;x <= 12;x++))
   do
      printf "%4d" $((x*y))
   done
   echo
done
Result of above script:

Code:
   1   2   3   4   5   6   7   8   9  10  11  12
   2   4   6   8  10  12  14  16  18  20  22  24
   3   6   9  12  15  18  21  24  27  30  33  36
   4   8  12  16  20  24  28  32  36  40  44  48
   5  10  15  20  25  30  35  40  45  50  55  60
   6  12  18  24  30  36  42  48  54  60  66  72
   7  14  21  28  35  42  49  56  63  70  77  84
   8  16  24  32  40  48  56  64  72  80  88  96
   9  18  27  36  45  54  63  72  81  90  99 108
  10  20  30  40  50  60  70  80  90 100 110 120
  11  22  33  44  55  66  77  88  99 110 121 132
  12  24  36  48  60  72  84  96 108 120 132 144
 
  


Reply

Tags
convert, utc, xargs


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
How to enter graphical mode (run level 5) command line (run lenel 3) edmondgyampoh Linux - Newbie 3 05-15-2009 07:33 PM
command line//text editor Fred Caro Linux - Newbie 6 02-18-2009 12:23 PM
trying to fix xwindows or just run command line... changing run levels dave247 Debian 2 11-18-2008 07:11 PM
Command line Text Terri Linux - General 8 04-05-2002 04:10 PM
command line text formating anyone? DavidPhillips Linux - General 2 11-25-2001 07:26 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration