You are not logged in.

#1 2015-08-05 22:15:59

j2lapoin
Banned
Registered: 2013-06-28
Posts: 102

make this loop faster if you dare [SOLVED]

Does anyones know a way to speedup this process.

I have 3 files that i want juxtaposed. example, 3 files with name lastname age. I want at person to get the names and lastname and age.

while [ $counter -lt $CountLine ]; do
  head1=`cat $Name | head -n $counter | tail -n 1`
  head2=`cat $Lastname | head -n $counter | tail -n 1`
  head3=`cat $Age | head -n $counter | tail -n 1`
  echo "$counter"
  echo "$head1 $head2 $head3" >> $DIR9
  let counter+=1
done

I want something faster cause with 7000 entry it last for 2 minutes.

Last edited by j2lapoin (2015-08-05 22:56:59)

Offline

#2 2015-08-05 22:20:52

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: make this loop faster if you dare [SOLVED]

I don't know what exactly do you mean. Can you provide some examples of these 3 files?
See also 'man paste'.

When posting configs, code or command output, please use [ code ] tags https://bbs.archlinux.org/help.php#bbcode

like this

It makes the code more readable and - in case of longer listings - more convenient to scroll through.

Offline

#3 2015-08-05 22:32:52

j2lapoin
Banned
Registered: 2013-06-28
Posts: 102

Re: make this loop faster if you dare [SOLVED]

3 files with 3 differents topic (name, lastname, age)

in the first file name name:               in the second file name lastname:                      in the last file name age
john                                                     lewis                                                                       12
peter                                                   bacon                                                                     89
cayle                                                   douglas                                                                  67
fred                                                     lapointe                                                                  33

by concatenation or juxtaposition, i want in the name file person, name lastname and age on the same line.

john lewis 12
peter bacon 89
cayle douglas 67
...
..
.

I made a first attempt, it was successfull but it take 2 minutes to go on,  way too long.

while [ $counter -lt $CountLine ]; do
  head1=`cat name | head -n $counter | tail -n 1`
  head2=`cat lastname | head -n $counter | tail -n 1`
  head3=`cat age | head -n $counter | tail -n 1`
  echo "$counter"
  echo "$head1 $head2 $head3" >> person
  let counter+=1
done

Offline

#4 2015-08-05 22:37:22

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: make this loop faster if you dare [SOLVED]

Did you read that man page?

$ paste name lastname age
john    lewis   12
peter   bacon   89
cayle   douglas 67
fred    lapointe        33
$ paste name lastname age | column -t
john   lewis     12
peter  bacon     89
cayle  douglas   67
fred   lapointe  33

Offline

#5 2015-08-05 22:42:27

j2lapoin
Banned
Registered: 2013-06-28
Posts: 102

Re: make this loop faster if you dare [SOLVED]

karol wrote:

Did you read that man page?

$ paste name lastname age
john    lewis   12
peter   bacon   89
cayle   douglas 67
fred    lapointe        33
$ paste name lastname age | column -t
john   lewis     12
peter  bacon     89
cayle  douglas   67
fred   lapointe  33

PERFECT! I forgot about it. thanks.

Offline

#6 2015-08-05 22:49:41

respiranto
Member
Registered: 2015-05-15
Posts: 479
Website

Re: make this loop faster if you dare [SOLVED]

If it shall be faster, the best way is probably to not use bash but for example C instead.
I have just realized, that what karol suggested, paste, should do exactly what you want.

Because like this, for every line of every file the commands you use do essentially the following in the background:
- open the file
- search for the nth line
- close the file

Furthermore you use three different programs for this, which does also slow down the operation, because they were not designed to be used with the respective others.


As far as I understand, you have three files, one of them has all the first names, another one all the last names, and the last one all the ages, where every nth line of one file corresponds to the nth line of any of the two others.
Your script does simplify these three files by merging them into one with three space separated columns, am I right?

So the question arising is  - why? Where do the former files come from? Did you create them yourself? If so, why don't directly create them in a different manner?

EDIT: way too slow.

Last edited by respiranto (2015-08-05 22:50:25)

Offline

#7 2015-08-05 22:52:29

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: make this loop faster if you dare [SOLVED]

Just to prove no magic is involved:

$ ls
age  lastname  name
$ head *
==> age <==
12
89
67
33

==> lastname <==
lewis
bacon
douglas
lapointe

==> name <==
john
peter
cayle
fred

Please remember to mark the thread as solved https://bbs.archlinux.org/viewtopic.php?id=130309

Offline

#8 2015-08-05 23:02:05

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,561
Website

Re: make this loop faster if you dare [SOLVED]

karol wrote:

See also 'man paste'.

To clarify, I'd strongly recommend executing that command rather than googling that phrase!

wink


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#9 2015-08-06 00:49:08

Buddlespit
Member
From: Chesapeake, Va.
Registered: 2014-02-07
Posts: 501

Re: make this loop faster if you dare [SOLVED]

Trilby wrote:

To clarify, I'd strongly recommend executing that command rather than googling that phrase!
wink

... and my wife made me google...

Offline

Board footer

Powered by FluxBB