7

I have executed below command

cat /proc/loadavg && date

Actual Result:

0.00 0.00 0.00 1/803 26256
Fri Aug 26 09:00:56 EEST 2016

Expected Result:

0.00 0.00 0.00 1/803 26256 @@ Fri Aug 26 09:00:56 EEST 2016

I have tried sed and tr , but didn't work.

cat /proc/loadavg && date | sed 's/\n/ @@ /g'

cat /proc/loadavg && date | tr '\n' ' @@ '

Any idea, what I am missing?

2
  • I think sed removes the \n before it loads the line in the pattern space , then puts it back when it prints the pattern space to output, so you cant do a substtution on it in sed. Aug 26, 2016 at 7:00
  • 1
    just got a strike, based on answers, echo "`cat /proc/loadavg` @@ `date`".
    – Spike
    Aug 26, 2016 at 8:47

7 Answers 7

15

Your best bet is to use printf. You have two strings and you want to output them with some additional formatting. That's exactly what printf does.

$ printf "%s @@ %s\n" "$(cat /proc/loadavg)" "$(date)"

Your tr attempt does not work since tr modifies characters, not words. You could use it to replace the newlines with one single character though:

$ ( cat /proc/loadavg; date ) | tr '\n' '@'

... but it doesn't do quite what you need.

Your sed attempt does not work since the newline is stripped from the input to sed (i.e. sed -n '/\n/p' inputfile would never print anything).

You could still do it with sed if you read the second line (from date) with the N editing command while editing the first line (which will place a newline between them):

$ ( cat /proc/loadavg; date ) | sed 'N;s/\n/ @@ /'

... but I would personally prefer the printf solution.

8

You can do this:

echo `cat /proc/loadavg` @@ `date`
3
  • There are plenty of good answers already and yours has a bug causing @@ to show up between each word. Aug 26, 2016 at 16:30
  • Fixed by removing xargs and printf complexity. Aug 26, 2016 at 16:58
  • 1
    Now that is a good simple solution. :) Aug 26, 2016 at 17:09
7

Like this:

( cat /proc/loadavg && date ) | sed 'N; s/\n/ @@ /'

First, your attempts don't work because the pipe | applies only to date, not no both command. To work around that you need to run cat ... && date in a subshell, and then redirect the subshell's stdout.

Then tr '\n' ' @@ ' doesn't work because you can't replace a character with multiple characters.

And sed 's/\n/ @@ /g' doesn't work because sed only gets to see lines one at a time. To get it to see newlines you need to merge both lines of input in the same buffer. Which is what N does above.

1
  • 4
    this worked for me. to expand a little more on sed's N command. n prints the pattern space to output and pulls the next line into the pattern space - like sed would have done anyway. The N command pulls the next line into the pattern space but doesn't print the pattern, so you end up with both lines in the pattern space, which is why the substitution s/\n/ @@ / works Aug 26, 2016 at 7:25
3

This might help you,

( cat /proc/loadavg && date ) | awk 'ORS=" @@ "'
2
  • 1
    Note that your printf command will use the output from cat and date as a formatting string... If any part of those outputs contains a % formatting specifier, it will cause an error.
    – Kusalananda
    Aug 26, 2016 at 7:27
  • 2
    you didnt need to delete the printf command, per Kusalananda's comment, you could group the output into a separate arguments, i.e. printf <format_string> <string> <string> Aug 26, 2016 at 7:32
2

Just for something different...

(cat /proc/loadavg && date) | paste -d@ - - | sed 's/@/ @@ /' 

paste -d@ - - pastes the two line side-by-side with "@" in-between. Then sed replaces the single "@" with " @@ "

1
  • 2
    OK. for the record, you can do without sed e.g. ( cat /proc/loadavg && date ) | paste -d' @@ ' - /dev/null /dev/null /dev/null - Aug 26, 2016 at 21:09
1

The extra filtering you are doing to remove the new line is only being applied to the date command, therefore try

{ cat /proc/loadavg && date; } | tr -d '\n'
0

How about this?

export DELIMITER=' @@ '; cat /proc/loadavg | tr -d '\n'; echo -n "$DELIMITER" && date

Output: 0.34 0.39 0.40 1/1065 29353 @@ Fr 26. Aug 09:30:26 CEST 2016

It only removes the newline after the line output by cat, then echoes a delimiter string without a newline (-n) and finally appends the output of date.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.