Linux Commands: Difference between revisions
(→echo) |
(→sed: Using insert/append text commands i\ a\) |
||
Line 109: | Line 109: | ||
== sed == |
== sed == |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
=== Usage === |
|||
Some basic usage: |
|||
<source lang="bash"> |
<source lang="bash"> |
||
sed [OPTION]... {script-only-if-no-other-script} [input-file]... |
sed [OPTION]... {script-only-if-no-other-script} [input-file]... |
||
Line 116: | Line 125: | ||
</source> |
</source> |
||
Use of address commands <tt>a\text</tt>, <tt>i\text</tt>, <tt>c\text</tt>. The command is terminated by a '''*newline*'''. To insert a newline character, use <tt>\n</tt>: |
|||
⚫ | |||
<source lang="bash"> |
|||
⚫ | |||
$ cat mytext |
|||
⚫ | |||
First line |
|||
⚫ | |||
Second line |
|||
⚫ | |||
$ cat mysedscript |
|||
⚫ | |||
1 {i\inserted text |
|||
s/$/ (not anymore)/g} |
|||
$ sed -f mysedscript mytext |
|||
inserted text |
|||
First line (not anymore) |
|||
Second line |
|||
# All on one line: use echo -e to generate the newline that terminates the command i\ |
|||
$ echo -e "1 {i\\inserted text\ns/$/ (not anymore)/g}"| sed -f - mytext |
|||
inserted text |
|||
First line (not anymore) |
|||
Second line |
|||
#Same result without command \i: |
|||
$ sed "1 {s/^/inserted text\n/; s/$/ (not anymore)/}" mytext |
|||
</source> |
|||
=== Regular expressions=== |
=== Regular expressions=== |
Revision as of 21:34, 18 November 2008
dd
Convert and copy a file.
dd returns current copy status when sent an HUP signal. Type in another shell:
while :; sleep 5; do killall -s SIGHUP1 dd; done
This will force dd to update its status every 5sec.
dpkg
Package manager for Debian / Ubuntu. See Package Management#Ubuntu / Debian
echo
echo -e "Some text\n...on 2 lines..." # Enable interpretation of backslash escapes (must be quoted!)
find
find . -exec echo '{}' \; #semi-colon must be escaped!
find . -exec echo one '{}' \; -exec echo two '{}' \; #to execute several commands on one match
grep
grep -Rsl PATTERN [FILE] # Recursive, no error output, only list filename
grep BASIC-REG-EXP-PATTERN [FILE] # Use classic regexp (like "dma\|DMA")
egrep EXT-REG-EXP-PATTERN [FILE] # Same as grep -E. Use extended regexp (like "dma|DMA")
fgrep FIXED-STRINGS-REG-EXP [FILE] # Same as grep -F. Pattern is a list of strings to match.
grep -n PATTERN [FILE] # Print matched line numbers.
grep -- "-s" [FILE] # Search for text "-s"
grep -e "-s" [FILE] # Search for text "-s" - alternative solution
grep -R -include=*.in PATTERN * # Search recursively through folders, limiting to files matching pattern "*.in"
grep -R PATTERN *.in # Idem, but matching pattern "*.in" also applies to folders.
netcat
TCP-IP swiss army knife (equivalent of the telnet program. Check wikipedia:netcat. Also known as command nc).
netstat
Print network connections, routing tables, interface statistics, masqurade connections, and multicast memberships
netstat -atpn #All, tcp, socket program PID, numeric
netstat -rn #Kernel route table, numberic
When listing sockets (default output), you'll get an output like:
% netstat -at Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 *:time *:* LISTEN tcp 0 0 localhost:mysql *:* LISTEN tcp 0 0 andLinux.local:43449 windows-host:x11 ESTABLISHED |
% netstat -atn Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:37 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN tcp 0 0 192.168.11.150:43449 192.168.11.1:6000 ESTABLISHED |
- Local Address
- * or 0.0.0.0 means that the process accepts connection from any interface.
- 127.0.0.1 means it only accepts connection on localhost loopback (and so only connection that originates from local PC as well).
- Any other IP address means that the process listen on the given port at the given IP address
recode
recode /cl../cr <dos.txt >mac.txt
recode /cr.. <mac.txt >unix.txt
recode ../cl <unix.txt >dos.txt
recode charset1/surface1..charset2/surface2 <input.txt >output.txt
recode /QP.. <qp.txt >normal.txt # To convert quoted-printable text
charset | surface | ||
---|---|---|---|
us | ASCII (7 bits) | /cr | Carriage return as end of line (Mac text) |
l1 | ISO Latin-1 (ISO-8859-1, Western Europe, 8 bits) | /cl | Carriage return line feed as end of line (DOS text) |
EUCJP | EUC-JP for Japanese (Unix) | / | Line feed as end of line (Unix text) |
SJIS | Shift-JIS for Japanese (Microsoft) | /d1 | Human readable bytewise decimal dump |
ISO2022JP | Mail encoding for Japanese (7 bits) | /x1 | Human readable bytewise hexidecimal dump |
u2 | UCS-2 (Universal Character Set, 2 bytes) | /64 | Base64 encoded text |
u8 | UTF-8 (Universal Transformation Format, 8 bits) | /QP | Quoted-Printable encoded text |
rpm
sed
References
- The SED Homepage on SourceForge
- The SED FAQ
- The SED man page
- Sed, a stream editor
- The sed one-liners
Usage
Some basic usage:
sed [OPTION]... {script-only-if-no-other-script} [input-file]...
sed -n # Silent - suppress automatic printing of pattern space
sed -r # Use extended regular expression
sed -i "s/foo/bar/" *.txt # In-place file modification
Use of address commands a\text, i\text, c\text. The command is terminated by a *newline*. To insert a newline character, use \n:
$ cat mytext
First line
Second line
$ cat mysedscript
1 {i\inserted text
s/$/ (not anymore)/g}
$ sed -f mysedscript mytext
inserted text
First line (not anymore)
Second line
# All on one line: use echo -e to generate the newline that terminates the command i\
$ echo -e "1 {i\\inserted text\ns/$/ (not anymore)/g}"| sed -f - mytext
inserted text
First line (not anymore)
Second line
#Same result without command \i:
$ sed "1 {s/^/inserted text\n/; s/$/ (not anymore)/}" mytext
Regular expressions
The information below is only illustrative. See e.g. Wikipedia page for reference information. The list below is actually for extended regular expressions, which can be obtained in sed using option -r (sed -r).
Regexp | Description |
---|---|
. | Match any character |
gray|grey | Match gray or grey |
gr(a|e)y | Match gray or grey |
gr[ae]y | Match gray or grey |
file[^0-2] | Match file3 or file4, but not file0, file1, file2. |
colou?r | (zero or one) - Match Color or Colour. |
ab*c | (zero or more) - Match ac, abc, abbc, .... |
ab+c | (one or more) - Match abc, abbc, abbbc, .... |
a{3,5} | (at least m and not more than n times) - Match aaa, aaaa, aaaaa. |
^on single line$ | (start and end of line) - Match on single line on a single line. |
When using standard (non-extended) regular expression, some special meta-characters (like the parenthesis ( ), or braces { }) must be quoted with backslash \.
socat
Command-line utility that establishes two bidirectional byte streams and transfers data between them ([1]). socat is the more powerful version of netcat. Check the homepage.
socat -ly 'TCP4-LISTEN:143,reuseaddr,fork' PROXY:ton.imap.server:143|TCP:134.27.168.36:8080
ProxyCommand socat - 'PROXY:%h:%p,proxyauth=user:pass|SSL,verify=0|PROXY:my.server:443,proxyauth=user:pass|TCP:big.brother.proxy:8080' # Using v2.0.0 beta
socat can be easily used as a replacement of telnet:
socat tcp:<host>:<port> - # <port> can be a port number or service name (telnet,imap...)
Some useful command-line options:
socat -ly # Writes messages to syslog instead of stderr; severity as defined with option -d
socat -v # Writes the transferred data to their target stream but also to stderr (text mode)
Miscellaneous
- [htop
- an improved top command
- iconv
- locale encoding conversions
- konwert
- fancy encoding conversions
- mimencode
- binary file conversion for the mail.;mkfifo
- make FIFOs (named pipes)
- pv, pipeview, pipebench
- monitor the progress of data through a pipe
- reformime
- MIME E-mail reformatting tool
- stat
- display file or file system status
- strace
- trace system calls and signals
- tee
- read from standard input and write to standard output and files
- tree
- List the contents of directories in a tree-like format.
- uuencode, uudecode
- binary file conversion for Unix.
- watch
- Execute a program periodically, showing output full screen
- xclip
- xclip is a command line interface to the X11 clipboard. It can also be used for copying files, as an alternative to sftp/scp, thus avoiding password prompts when X11 forwarding has already been setup. Check this guide.
- XSel
- XSel is a command-line program for getting and setting the contents of the X selection (aka clipboard). Normally this is only accessible by manually highlighting information and pasting it with the middle mouse button.
- xselection
- Another package similar to xsel. But I can't get it work correctly on OpenSUSE...