Home > Net >  How to merge result in shellscript
How to merge result in shellscript

Time:07-25

I want to merge results in shellscript.

ls -lrt
-rw-r--r--  1 cu  staff  11 Jul 23 23:57 aaa
-rw-r--r--  1 cu  staff   8 Jul 24 00:12 bbb
-rw-r--r--  1 cu  staff  33 Jul 24 02:31 ccc
-rw-r--r--  1 cu  staff  75 Jul 24 02:32 ddd

cksum `ls`
1108326690 11 aaa
1221844548 8 bbb
1213522119 33 ccc
3248460215 75 ddd

↓

-rw-r--r--  1 cu  staff  11 Jul 23 23:57 aaa 1108326690 11 aaa
-rw-r--r--  1 cu  staff   8 Jul 24 00:12 bbb 1221844548 8 bbb
-rw-r--r--  1 cu  staff  33 Jul 24 02:31 ccc 1213522119 33 ccc
-rw-r--r--  1 cu  staff  75 Jul 24 02:32 ddd 3248460215 75 ddd

I tried the command below but failed. (ls -l; cksum ls) | xargs

Please show me kindly how to merge these results.

CodePudding user response:

You would have to use { ls -lrt ; cksum ls ; } > outfile.txt first. Then, maybe read the contents from the outfile.txt using cat outfile.txt.

Or, do it all in one go with { ls -lrt ; cksum ls ; } > outfile.txt | cat

As to why (ls -l; cksum ls) | xargs didn't work, referring to j9s's answer :

Code:

( command1 ; command2 ; command3 ) | cat

{ command1 ; command2 ; command3 ; } > outfile.txt

The main difference between the two is that the first one splits of a child process, while the second one operates in the context of the main shell. This can have consequences regarding the setting and use of variables and other environment settings, as well as performance.

CodePudding user response:

This may be what you want:

paste  <(ls -lrt *) <(cksum `ls -rt`)

Note that this is intended to be used interactively (not inside a script) and may not work properly if filenames contain "weird" characters, or the current directory has subdirectories.

  • Related