Generating a CSV list from Linux 'ps'
You can use the following syntax to put your own delimiter:
ps -Ao "%U,%t,%a"
How to store ps command output in csv format?
You could try something like the following code I wrote:
ps -e -o %p, -o lstart -o ,%C, -o %mem -o ,%c > output.csv
Brief explanation:
The -o
option can be used multiple times in a ps
command to specify the format.
In order to control which separator is used we can use AIX format descriptors. We can specify our needed separators like, e.g. %p,
. Since AIX format descriptors are not available for every piece of data, but only for some of the data (for example in our case there are no AIX format descriptors for %mem
and for lstart
), we plant %mem
and lstart
around the available AIX format descriptors to achieve the comma separation. For example this site provides information about the ps
command for further readings.
output.csv example:
PID, STARTED,%CPU,%MEM,COMMAND
1,Mon Feb 25 00:00:01 2019, 0.0, 0.1,examplecommand1
2,Mon Feb 25 00:00:01 2019, 0.0, 0.0,examplecommand2
(...)
How can I modify following ps command to print in comma separated value (CSV) format?
Usa an Output Field Separator (OFS). Add OFS=", ";
to your awk command:
ps -e -o pcpu,pmem,args,pid --sort=pcpu | sed '5q' | awk ' {OFS=", "; print $1,$2,$3,$4 }'
or shorter:
ps -e -o pcpu,pmem,args,pid --sort=pcpu | awk 'NR<=5 {OFS=", "; print $1,$2,$3,$4 }'
Output (example):
%CPU, %MEM, COMMAND, PID
0.0, 0.0, [migration/0], 6
0.0, 0.0, [migration/1], 7
0.0, 0.0, [migration/2], 11
0.0, 0.0, [migration/3], 14
See: 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR
Bash Script to generate csv file from text
save this in a file, e.g. makecsv.rc:
#!/bin/sh
echo Type,Count,Name
x=0
for f in `cat`
do
x=`expr $x + 1`
echo Def,u$x,$f
done
then run as:
cat ../test.txt | ./makecsv.rc > ../test.csv
if needed, you do chmod +x makecsv.rc
The advantage is that the input/output file names are not hardcoded
How To Capture Unix 'Top' Command Output to a CSV file?
If you want to trim runs of whitespace and replace them with commas, try
top -b -n 3 | sed -n '8, 12{s/^ *//;s/ *$//;s/ */,/gp;};12q'
The sed
script performs the following substitutions on lines 8 through 12:
- Replace any leading space with nothing (otherwise you get an empty first column when the PID is right-aligned).
- Replace any trailing spaces with nothing (similarly to avoid empty fields after the data).
- Replace any remaining runs of adjacent spaces with a comma. Print this line.
Finally, on line 12, we are done, so we quit sed
.
The shell does not pay any attention to the name of the file you are redirecting into and generally, file extensions on Unix are informal decorations, not file type specifiers like they are on some platforms.
You could do echo hello >outputfile.ps
and the output would still be text, not PostScript (or a number of other possible interpretations of the .ps
file extension). In any event, the echo
command does not know that it is being redirected, because that is handled by the shell before the command runs, anyway. (Well, echo
is a shell built-in, so in theory there could be some coordination in this case.)
Output CSV file and net user command not aligned with PS command
You can construct an object that merges the AD User with the information coming from your CSV and the information from net user
. Here is an example of how you can approach it:
$csv = Import-CSV 'path\to\csvhere.csv'
$ADprops = @(
'DisplayName'
'mail'
'LastLogonDate'
'LastBadPasswordAttempt'
'AccountExpirationDate'
'PasswordLastSet'
'OfficePhone'
'Department'
'Manager'
)
$filter = @(
$ADprops
@{
Name = 'PC Name'
Expression = { $pcName }
}, @{
Name = 'Password Changeable'
Expression = { $changeable }
}, @{
Name = 'Password Expires'
Expression = { $expires }
}
)
Clear-Host
do
{
Write-Host " Enter the user ID: " -ForegroundColor Cyan -NoNewline
$UserName = Read-Host
Write-Host ""
$pcName = $csv.Where({ $_."User ID" -match $Username })."PC Name"
$expires, $changeable = net user $Username /domain |
Select-String -Pattern 'Password Changeable|Password Expires' |
ForEach-Object { ($_ -split '\s{2,}')[1] }
Get-ADUser -Identity $Username -Properties $ADprops |
Select-Object $filter
} while ($csv.'User ID' -notcontains $Username)
How to sort CSV data with Powershell?
sort
is an external command (Application) on Linux systems.
In other words, do not use the short name () but the full cmdlet name sort
Sort-Object
:
$Words |Sort-Object 'code word'
As commented by @postanote, details about the command precedence can be found in about_Command_Precedence. Reading this, might actually look confusing as the process is defined to occur in this order: Alias, Function, Cmdlet, any external command/script
(which should give the alias precedence over the external command sort
).
Apparently the sort
alias is simply not installed on Linux systems (presumably to prevent it from overruling the external native Linux command):
Windows (10)
Get-Command sort
CommandType Name Version Source
----------- ---- ------- ------
Alias sort -> Sort-Object
Linux (Pi OS)
Get-Command sort
CommandType Name Version Source
----------- ---- ------- ------
Application sort 0.0.0.0 /usr/bin/sort
This means that you might also resolve this issue by manually overruling the external sort
command with a sort
alias:
Set-Alias sort Sort-Object
Need to store the output of an API into a .csv file
$Weather.hko | Export-Csv "out.csv"
If needed, you can set the delimiter via -Delimiter ";"
.
> Get-Content .\out.csv
"BulletinTime","Temperature","RH","HomeMaxTemperature","HomeMinTemperature"
"201907021440","29.1","86","32","26"
If you want to append information you can use the -Append
switch.
Related Topics
Hook Functions for Linux Filesystem
Xdotool Commands Bound to Key Shortcuts Doesnot Work
How to Mount a Remote Linux Folder in Windows Through Ssh
What Happens When You Run a Program
Hiding User Input on Terminal in Linux Script
Standard Library Abi Compatibility
Determine If There Is Data Left on the Socket and Discard It
Using Assertion in the Linux Kernel
Pipe Output to Use as the Search Specification for Grep on Linux
How to Get the First Column of Comm Output
Cmake Doesn't Know Where Is Qt4 Qmake
Bash: Add String to the End of the File Without Line Break
Attaching and Mounting Existing Ebs Volume to Ec2 Instance Filesystem Issue
How to Wait for Server Restart Using Ansible
Run a Shell Script from Docker-Compose Command, Inside the Container