Skip to content

File Content

Commands for reading file content, counting statistics, transforming text, and splitting files into pieces.

Concatenate files and print on the standard output. Returns CatLine objects with LineNumber, Content, and FileName properties.

cat [OPTIONS] [FILE...]
FlagDescription
-nNumber all output lines
-bNumber non-blank output lines (overrides -n)
-sSqueeze consecutive blank lines into one
-EDisplay $ at the end of each line
-TDisplay ^I for tab characters

PsBash.CatLine with properties:

PropertyTypeDescription
LineNumberintLine number in the file
ContentstringRaw line content
FileNamestringSource file path (empty for pipeline input)
BashTextstringFormatted output including line numbers and transformations
Terminal window
# Display file contents
cat README.md
Terminal window
# Number all lines
cat -n script.ps1
1 #!/usr/bin/env pwsh
2 Import-Module PsBash
3
4 ls -la | grep '.log'
Terminal window
# Number only non-blank lines and show line endings
cat -b -E notes.txt
Terminal window
# Squeeze blank lines and show tabs
cat -s -T Makefile
Terminal window
# Access typed properties
$lines = cat src/PsBash.psm1
$lines[0].Content # raw text of line 1
$lines.Count # total line count
$lines | Where-Object { $_.Content -match 'function' }
Terminal window
# Concatenate multiple files
cat header.txt body.txt footer.txt

Output the first part of files. In pipeline mode, head is a pipeline bridge: it passes through the original typed objects unchanged.

head [-n COUNT] [FILE...]
FlagDescription
-nNumber of lines to output (default: 10)
  • File mode: PsBash.CatLine objects
  • Pipeline mode: passes through whatever type it receives
Terminal window
# First 10 lines of a file (default)
head README.md
Terminal window
# First 3 lines
head -n 3 README.md
Terminal window
# Pipeline bridge -- returns LsEntry objects, not strings
$top5 = ls -la | sort -k5 -rn | head -n 5
$top5[0].SizeBytes # real integer
$top5[0].Name # real string
Terminal window
# Pipeline bridge -- returns PsEntry objects
$procs = ps aux | sort -k3 -rn | head 5
$procs[0].CPU # real double
$procs[0].Command # real string

Output the last part of files. Like head, tail is a pipeline bridge that preserves original object types.

tail [-n COUNT] [FILE...]
FlagDescription
-nNumber of lines to output (default: 10). Prefix with + to start from line N.
  • File mode: PsBash.CatLine objects
  • Pipeline mode: passes through whatever type it receives
Terminal window
# Last 10 lines of a file (default)
tail server.log
Terminal window
# Last 5 lines
tail -n 5 server.log
Terminal window
# From line 3 onward (skip first 2 lines)
tail -n +3 data.csv
Terminal window
# Pipeline bridge -- returns LsEntry objects
$smallest = ls -la | sort -k5 -n | tail -n 3
$smallest[0].SizeBytes # real integer

Concatenate and print files in reverse line order. Returns TextOutput objects.

tac [-s SEPARATOR] [FILE...]
FlagDescription
-sUse SEPARATOR instead of newline as the record separator

PsBash.TextOutput with a BashText property.

Terminal window
# Reverse lines of a file
tac changelog.txt
Terminal window
# Reverse pipeline input
cat data.txt | tac
Terminal window
# Custom separator
tac -s '---' sections.txt

Print newline, word, and byte counts. Returns WcResult objects with typed integer properties.

wc [OPTIONS] [FILE...]
FlagDescription
-lPrint only the line count
-wPrint only the word count
-cPrint only the byte count

When no flags are given, all three counts are printed.

PsBash.WcResult with properties:

PropertyTypeDescription
LinesintNumber of newlines
WordsintNumber of whitespace-delimited words
BytesintNumber of bytes
FileNamestringSource file path
BashTextstringFormatted count output
Terminal window
# Full counts for a file
wc README.md
42 187 1084 README.md
Terminal window
# Line count only
wc -l src/PsBash.psm1
Terminal window
# Count lines from pipeline
cat README.md | grep 'bash' | wc -l
Terminal window
# Multiple files produce a total row
wc -l src/*.ps1
Terminal window
# Access typed properties
$stats = wc README.md
$stats.Lines # 42
$stats.Words # 187
$stats.Bytes # 1084

Number lines of files. By default, blank lines are not numbered.

nl [-ba] [FILE...]
FlagDescription
-baNumber all lines, including blank lines

Without -ba, blank lines are passed through without a number.

PsBash.TextOutput with a BashText property.

Terminal window
# Number non-blank lines
nl script.ps1
1 Import-Module PsBash
2 $files = ls -la
3 echo "Found $($files.Count) files"
Terminal window
# Number all lines including blanks
nl -ba script.ps1
1 Import-Module PsBash
2 $files = ls -la
3
4 echo "Found $($files.Count) files"
Terminal window
# Number pipeline input
cat README.md | nl

Reverse characters of each line. Returns TextOutput objects.

rev [FILE...]

PsBash.TextOutput with a BashText property.

Terminal window
# Reverse characters in a file
rev names.txt
Terminal window
# Reverse pipeline input
echo "hello world" | rev
dlrow olleh
Terminal window
# Reverse multiple files
rev file1.txt file2.txt

Print sequences of printable characters (ASCII 0x20-0x7E) found in files. Useful for extracting readable text from binary data.

strings [-n LENGTH] [FILE...]
FlagDescription
-nMinimum string length (default: 4)

PsBash.TextOutput with a BashText property.

Terminal window
# Extract printable strings from a binary
strings program.exe
Terminal window
# Only strings of 8+ characters
strings -n 8 program.exe
Terminal window
# Extract from pipeline input
cat binary.dat | strings -n 6

Wrap each input line to fit in a specified width.

fold [-w WIDTH] [-s] [-b] [FILE...]
FlagDescription
-wMaximum line width (default: 80)
-sBreak at spaces instead of mid-word
-bCount bytes rather than columns (default for ASCII)

PsBash.TextOutput with a BashText property.

Terminal window
# Wrap at 40 columns
fold -w 40 essay.txt
Terminal window
# Wrap at spaces to avoid breaking words
fold -w 60 -s paragraph.txt
Terminal window
# Wrap pipeline input
cat README.md | fold -w 72 -s

Convert tab characters to spaces.

expand [-t TABWIDTH] [FILE...]
FlagDescription
-tTab stop width (default: 8)

PsBash.TextOutput with a BashText property.

Terminal window
# Convert tabs to 8 spaces (default)
expand Makefile
Terminal window
# Convert tabs to 4 spaces
expand -t 4 src/main.go
Terminal window
# Expand pipeline input
cat config.yaml | expand -t 2

Convert spaces to tabs.

unexpand [-t TABWIDTH] [-a] [FILE...]
FlagDescription
-tTab stop width (default: 8)
-aConvert all sequences of spaces, not just leading spaces

Without -a, only leading spaces are converted.

PsBash.TextOutput with a BashText property.

Terminal window
# Convert leading spaces to tabs (default: 8-wide)
unexpand source.py
Terminal window
# Convert leading spaces to 4-wide tabs
unexpand -t 4 source.py
Terminal window
# Convert all space runs, not just leading
unexpand -a -t 4 data.txt

Split a file into pieces. Output files are written to the current directory.

split [-l LINES] [-d] [-a LENGTH] [FILE [PREFIX]]
FlagDescription
-lNumber of lines per output file (default: 1000)
-dUse numeric suffixes (00, 01, …) instead of alphabetic (aa, ab, …)
-aSuffix length (default: 2)

The default output prefix is x, producing files xaa, xab, xac, … (or x00, x01, x02, … with -d).

No output objects. Files are written to disk.

Terminal window
# Split into 1000-line chunks (default)
split largefile.csv
# produces: xaa, xab, xac, ...
Terminal window
# Split into 100-line chunks with numeric suffixes
split -l 100 -d data.csv
# produces: x00, x01, x02, ...
Terminal window
# Custom prefix
split -l 50 data.csv chunk_
# produces: chunk_aa, chunk_ab, chunk_ac, ...
Terminal window
# Longer suffixes for large files
split -l 10 -d -a 4 huge.log part_
# produces: part_0000, part_0001, part_0002, ...
Terminal window
# Split from pipeline
cat data.csv | split -l 500 - output_

head and tail are pipeline bridges. They do not create new objects. They pass through the exact objects they receive, preserving type information across the entire pipeline.

Terminal window
# CatLine objects survive through head
$lines = cat file.txt | head -5
$lines[0].PSObject.TypeNames[0] # PsBash.CatLine
$lines[0].Content # raw text
# LsEntry objects survive through tail
$files = ls -la | tail -3
$files[0].PSObject.TypeNames[0] # PsBash.LsEntry
$files[0].SizeBytes # real integer
# PsEntry objects survive through head
$procs = ps aux | sort -k3 -rn | head 5
$procs[0].PSObject.TypeNames[0] # PsBash.PsEntry
$procs[0].CPU # real double

This means you can chain bridge commands freely and still access typed properties at the end of the pipeline.