add timestamp to top command batch output - bash

I need to add timestamp to the beginning of each line printed in the following output.
# top -b |grep -w 'db2sysc\|java'
4756 db2inst1 20 0 2525m 875m 830m S 0 11.0 1:33.57 db2sysc
4951 root 20 0 1416m 689m 7276 S 0 8.7 1:27.86 java
4756 db2inst1 20 0 2525m 875m 830m S 1 11.0 1:33.61 db2sysc
4951 root 20 0 1416m 689m 7276 S 0 8.7 1:27.87 java
would like it like this
1366138603 4756 db2inst1 20 0 2525m 875m 830m S 0 11.0 1:33.57 db2sysc
1366138603 4951 root 20 0 1416m 689m 7276 S 0 8.7 1:27.86 java
1366138692 4756 db2inst1 20 0 2525m 875m 830m S 1 11.0 1:33.61 db2sysc
1366138692 4951 root 20 0 1416m 689m 7276 S 0 8.7 1:27.87 java
Thank you for any help.

I'd suggest using awk instead of grep:
top -b | awk '/db2sysc|java/ {print systime(), $0}'

Related

Open txt file inside windows 10 container

I have a simple docker container that is based on windows image:
FROM mcr.microsoft.com/windows:1903
WORKDIR /app1/
ENTRYPOINT powershell.exe
I run it interactively, using:
docker run -it -v c:\app1:c:\app1 test-image:1.0
There is a file called 1.txt inside app1 folder.
When I run:
.\app1\1.txt
I see no notepad.exe process, but instead I can spot OpenWith process:
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
78 5 1056 4452 0.02 1940 1 CExecSvc
74 5 5360 3792 0.02 1904 1 cmd
81 5 904 1364 0.00 1844 1 CompatTelRunner
156 10 6532 6088 0.00 1728 1 conhost
97 7 1196 4980 0.05 1896 1 conhost
286 13 1836 4976 0.27 984 1 csrss
37 6 1348 3356 0.06 524 1 fontdrvhost
0 0 60 8 0 0 Idle
831 22 4748 13844 0.17 460 1 lsass
546 25 13156 28920 0.17 1952 1 OfficeClickToRun
420 24 7400 28844 0.13 2472 1 OpenWith
376 22 6732 27168 0.13 2536 1 OpenWith
I suspect that some mapping might be missing, event though assoc shows that .txt file is associated with notepad.exe:
assoc .txt
.txt=txtfile
ftype txtfile
txtfile=%SystemRoot%\system32\NOTEPAD.EXE %1
What might be the problem here? Am I missing some register value?

Not able to delete the blank or empty space from a command text output file on UNIX

The command '(sleep 4 ; echo q) | topas -P |tee /tmp/top' on AIX produces this output in a command text file and I am not able to remove the blank spaces from it. I have tried, sed/perl and awk commands to print only the lines containing characters however nothing has helped. Do I need to convert this file to a ASCII text format to use sed/per/grep or awk to remove the empty lines?.
$ file /tmp/top
/tmp/top: commands text
$ head -n33 /tmp/top
DATA TEXT PAGE PGFAULTS
USER PID PPID PRI NI RES RES SPACE TIME CPU% I/O OTH COMMAND
root 9044256 20447712 60 20 2.43M 304K 2.43M 0:00 2.1 0 253 topas
root 14942646 8913178 60 20 72.0M 37.8M 72.0M 0:42 0.2 0 1 TaniumCl
root 20447712 21889434 60 20 148K 312K 508K 0:00 0.2 0 0 ksh
root 21955056 20447712 60 20 216K 36.0K 216K 0:00 0.1 0 3 sed
root 24838602 20447712 60 20 120K 8.00K 120K 0:00 0.1 0 1 tee
root 9830690 10355194 60 20 120K 4.00K 120K 0:00 0.1 0 0 sleep
root 12255642 13893896 60 41 57.5M 39.8M 57.5M 33:42 0.1 0 0 mmfsd
root 10355194 20447712 60 20 148K 312K 508K 0:00 0.1 0 0 ksh
root 9109790 4063622 39 20 12.9M 3.68M 12.9M 5:19 0.1 0 0 rmcd
root 13697394 4063622 60 20 8.27M 55.9M 8.27M 17:18 0.1 0 0 backup_a
root 20906328 1 60 20 1.81M 0 1.81M 3:15 0.0 0 0 nfsd
root 4260244 1 60 20 620K 88.0K 620K 41:23 0.0 0 0 getty
root 1573172 0 37 41 960K 0 960K 15:17 0.0 0 0 gil
nagios 9240876 4063622 60 20 23.7M 736K 23.7M 9:43 0.0 0 0 ncpa_pas
root 4391332 1 60 20 12.5M 252K 12.5M 4:43 0.0 0 0 secldapc
a_RTHOMA 8323456 12059082 60 20 636K 3.06M 1016K 0:00 0.0 0 0 sshd
root 8388902 4063622 60 20 1.76M 1.05M 1.76M 7:03 0.0 0 0 clcomd
root 3539312 1 60 20 448K 0 448K 5:07 0.0 0 0 lock_rcv
root 3670388 1 60 20 448K 0 448K 4:18 0.0 0 0 sec_rcv
root 5767652 4063622 48 8 392K 324K 392K 2:49 0.0 0 0 xntpd
root 6816242 1 60 20 1.19M 0 1.19M 1:05 0.0 0 0 rpc.lock
root 459026 0 16 41 640K 0 640K 2:19 0.0 0 0 reaffin
root 23921008 1 60 20 1.00M 0 1.00M 4:36 0.0 0 0 n4cb
lpar2rrd 23200112 25625020 64 22 868K 120K 868K 0:00 0.0 0 0 vmstat
root 7143896 1 40 41 448K 0 448K 0:48 0.0 0 0 nfsWatch
root 6160840 1 60 20 448K 0 448K 0:09 0.0 0 0 j2gt
Looks like your output file has some ASCII characters so it's better to print only those lines which starting from letters do like:
awk '/^[a-zA-Z]/' Input_file

Sed: delete lines without pattern except first

How to delete lines that do not match the pattern except the first line?
To delete lines except first i use
sed '1!d'
To delete lines that do not match patter i use
sed '/pattern/!d'
How can i use both of conditions? Things like
sed '1!/pattern/!d'
doesn't work, says unknown command: '/'
As example (if pattern="rcu")
from input
PID PPID PRI NI VSZ RSS STAT TIME CMD
1 0 19 0 33664 4832 Ss 00:00:07 /sbin/init splash
2 0 19 0 0 0 S 00:00:00 [kthreadd]
3 2 39 -20 0 0 I< 00:00:00 [rcu_gp]
4 2 39 -20 0 0 I< 00:00:00 [rcu_par_gp]
8 2 39 -20 0 0 I< 00:00:00 [mm_percpu_wq]
9 2 19 0 0 0 S 00:00:04 [ksoftirqd/0]
get
PID PPID PRI NI VSZ RSS STAT TIME CMD
3 2 39 -20 0 0 I< 00:00:00 [rcu_gp]
4 2 39 -20 0 0 I< 00:00:00 [rcu_par_gp]
Thanks
Output first line and all lines which contain rcu:
sed -n '1p; /rcu/p' file
You could also make a script that reads the file and “extracts” first line that don’t match the pattern (to another temporary file or to the string), make other operations like deleting lines that you want to be deleted and paste that line back in the same place in the file.
As you have tagged awk
awk '{if ($0~/rcu/) print}'
Demo :
$ cat file1.txt
PID PPID PRI NI VSZ RSS STAT TIME CMD
1 0 19 0 33664 4832 Ss 00:00:07 /sbin/init splash
2 0 19 0 0 0 S 00:00:00 [kthreadd]
3 2 39 -20 0 0 I< 00:00:00 [rcu_gp]
4 2 39 -20 0 0 I< 00:00:00 [rcu_par_gp]
8 2 39 -20 0 0 I< 00:00:00 [mm_percpu_wq]
9 2 19 0 0 0 S 00:00:04 [ksoftirqd/0]
$ awk '{if ($0~/rcu/) print}' file1.txt
3 2 39 -20 0 0 I< 00:00:00 [rcu_gp]
4 2 39 -20 0 0 I< 00:00:00 [rcu_par_gp]
$
If when you say except the first if you mean except the first line of the input even if it doesn't match the regexp then this is what you want:
$ awk '/rcu/ || NR==1' file
PID PPID PRI NI VSZ RSS STAT TIME CMD
3 2 39 -20 0 0 I< 00:00:00 [rcu_gp]
4 2 39 -20 0 0 I< 00:00:00 [rcu_par_gp]
or if you mean except the first line that doesn't match the regexp then this is what you want:
$ awk '/rcu/ || !c++' file
PID PPID PRI NI VSZ RSS STAT TIME CMD
3 2 39 -20 0 0 I< 00:00:00 [rcu_gp]
4 2 39 -20 0 0 I< 00:00:00 [rcu_par_gp]

ConEmu/Cygwin: 'ls -ls' command line does not return an aligned output

I've installed Cygwin v3.1.2 (0.340/5/3) and ConEmu v20191012 (64bits)
When I run the command line below
c:\temp>ls -ls
total 0
0 -rw-rw-r--+ 1 me None 0 Jan 5 12:40 fileA
0 -rw-rw-r--+ 1 me None 0 Jan 5 12:40 fileB
0 -rw-rw-r--+ 1 me None 0 Jan 5 12:40 fileC
I don't get the directory items aligned. But if I perform same action on another computer with Cygwin 2.11.1 (0.329/5/3) and ConEmu v20191012 (64bits), I'll get the expected result, as it's shown next:
$ ls -ls
total 0
0 -rwxrwx---+ 1 me UK+Group(513) 0 Jan 5 12:41 fileA
0 -rwxrwx---+ 1 me UK+Group(513) 0 Jan 5 12:41 fileB
0 -rwxrwx---+ 1 me UL+Group(513) 0 Jan 5 12:41 fileC
Any idea about how to solve this problem?
Thanks!
Once I've downloaded CyWwin 3.1.7, the issue does not exist anymore.
Thanks!

Append Output results

I'm running a validation software and I want all of the output sent to a text file and have the results of multiple files placed/appended to the same file. I thought my code was working, but I just discovered I'm only getting the results from 1 file output to the text file.
java -jar /Applications/epubcheck-3.0.1/epubcheck-3.0.1.jar ~/Desktop/Validator/*.epub 2>&1 | tee -a ~/Desktop/Validator/EPUBCHECK3_results.txt
open ~/Desktop/Validator/EPUBCHECK3_results.txt
EDIT
When I run the same .jar file using Windows command line it will process a batch of files and appeand the results appropriately. I would just do this, but it would mean having to switch work stations and transferring files to validate them. I would like to get this running through the Unix shell on my Mac system so that I don't have to do unnecessary work. Command line that IS working below:
FOR /f %%1 in ('dir /b "C:\Users\scrawfo\Desktop\epubcheck\drop epubs here\*.epub"') do (
echo %%1 >> epubcheck.txt
java -jar "C:\Users\scrawfo\Desktop\epubcheck\epubcheck-3.0.jar" "C:\Users\scrawfo\Desktop\epubcheck\drop epubs here\%%1" 2>> epubcheck.txt
echo. >> epubcheck.txt)
notepad epubcheck.txt
del epubcheck.txt
syntax provided by you is correct there might be some problem with java output or something Try Executing it without redirection
cat test
Output:-
This is Test File ...............
Next Executed Command with same syntax
ps l 2>&1 | tee -a test
Output:-
F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME
COMMAND 4 0 3287 1 20 0 4060 572 n_tty_ Ss+ tty2
0:00 /sbin/mingetty /dev/tty2 4 0 3289 1 20 0 4060 572
n_tty_ Ss+ tty3 0:00 /sbin/mingetty /dev/tty3 4 0 3291
1 20 0 4060 576 n_tty_ Ss+ tty4 0:00 /sbin/mingetty
/dev/tty4 4 0 3295 1 20 0 4060 576 n_tty_ Ss+ tty5
0:00 /sbin/mingetty /dev/tty5 4 0 3297 1 20 0 4060 572
n_tty_ Ss+ tty6 0:00 /sbin/mingetty /dev/tty6 4 0 19086
1 20 0 4060 572 n_tty_ Ss+ tty1 0:00 /sbin/mingetty
/dev/tty1 4 0 20837 20833 20 0 108432 2148 wait Ss pts/0
0:00 -bash 4 0 21471 20837 20 0 108124 1036 - R+ pts/0
0:00 ps l 0 0 21472 20837 20 0 100908 664 pipe_w S+ pts/0
0:00 tee -a test
Checked File
cat test
Output:-(Appended properly)
This is Test File ...............
F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND 4 0
3287 1 20 0 4060 572 n_tty_ Ss+ tty2 0:00
/sbin/mingetty /dev/tty2 4 0 3289 1 20 0 4060 572
n_tty_ Ss+ tty3 0:00 /sbin/mingetty /dev/tty3 4 0 3291
1 20 0 4060 576 n_tty_ Ss+ tty4 0:00 /sbin/mingetty
/dev/tty4 4 0 3295 1 20 0 4060 576 n_tty_ Ss+ tty5
0:00 /sbin/mingetty /dev/tty5 4 0 3297 1 20 0 4060 572
n_tty_ Ss+ tty6 0:00 /sbin/mingetty /dev/tty6 4 0 19086
1 20 0 4060 572 n_tty_ Ss+ tty1 0:00 /sbin/mingetty
/dev/tty1 4 0 20837 20833 20 0 108432 2148 wait Ss pts/0
0:00 -bash 4 0 21471 20837 20 0 108124 1036 - R+ pts/0
0:00 ps l 0 0 21472 20837 20 0 100908 664 pipe_w S+ pts/0
0:00 tee -a test

Resources