0

I have a scenario where I need to execute date command and ls -lrth|wc -l command at the same time.

I read somewhere on google that I can do it in the way shown below using the semicolon

ls -lrth | wc -l | ; date 

This works super fine!
But the problem is when I want to extract the output of this. This gives a two line output with the output of ls -lrth |wc -l in the first line and the second line has the date output like shown below

$ cat test.txt 
39
Mon Oct 26 16:11:20 IST 2015

But it seems like linux is treating these two lines as if its on the same line.
I want this to be formatted to something like this

39,Mon Oct 26 16:11:20 IST 2015

For doing this I am not able to separately access these two lines (not even with tail or head).

Thanks in advance.

EDIT
Why I think linux is treating this as a same line because when I do this as shown below,

 $ ls -lrth| wc -l;date | head -1
39
Mon Oct 26 16:24:07 IST 2015

The above reason is for my assumption of the one line thing.

3 Answers 3

2

Have you already tried using an echo?

echo $(ls | wc -l) , $(date)

(or something similar, I don't have a Linux emulator here)

Sign up to request clarification or add additional context in comments.

Comments

1

If you want in your script ./script.sh

#!/bin/bash
a=$(ls -lrth | wc -l)
b=$(date)
out="$a,$b"
echo "$out"

EDIT

ls -lrth| wc -l;date | head -1

The semicolon simply separates two different commands ";"

Comments

0

Pipe to xargs echo -n (-n means no newline at end):

ls -lrth | wc -l | xargs echo -n ; echo -n ","; date

Testing:

$ ls -lrth | wc -l | xargs echo -n ; echo -n ","; date
11,Mon Oct 26 12:57:14 EET 2015

1 Comment

Alternatively, pipe to xargs printf "%i, "

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.