LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to get a command to run through each line of text (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-get-a-command-to-run-through-each-line-of-text-761377/)

shayno90 10-12-2009 10:11 AM

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!

acid_kewpie 10-12-2009 10:43 AM

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"

shayno90 10-13-2009 05:47 AM

Thanks acid kewpie, xargs was a much simpler command to output the time to UTC :)

acid_kewpie 10-13-2009 06:30 AM

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.

lutusp 10-13-2009 11:49 AM

Quote:

Originally Posted by acid_kewpie (Post 3717502)
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



All times are GMT -5. The time now is 07:33 AM.