http://dfrench.hypermart.net/fancyIndex/Tools/Scripts/oneliners.html
http://osr600doc.sco.com/en/SHL_automate/CONTENTS.html
Blog Archive
Thursday, December 07, 2006
scripts
- One Line Shell Scripts
Path : Home : Tools : Scripts
This page contains one line ( or close to it ) shell script solutions to problems posted in the "comp.unix.shell" news group. Normally, a weekly selection of the best one line shell script solution is made and placed in this list.
Subject: extracting a pattern from file
Question posted by su Jun 3, 2005 1:50 pm
Data Format file
*****************************************************************
Table Name: Sales_Orders (dbid=6,objid=1115151018, lockscheme=Da)
Page Locks SH_PAGE UP_PAGE EX_PAGE
Grants: 0 0 7
Table Name: UMUM_UTIL_MGT (dbid=4, objid=1274956114,lockscheme=Al )
Page Locks SH_PAGE UP_PAGE EX_PAGE
Grants: 0 0 4
Table Name: UMUM_UTIL_NGT (dbid=4, objid=1274956123,lockscheme=Al )
Page Locks SH_PAGE UP_PAGE EX_PAGE
Grants: 0 0 3
*****************************************************************
Actually i tried hard to get the output in following format but no
luck.
output format
******************************************************************
Sales_Orders lockscheme=Da 0 0 7
UMUM_UTIL_MGT lockscheme=Al 0 0 4
UMUM_UTIL_NGT lockscheme=Al 0 0 3
******************************************************************
Response posted by Ed Morton Jun 3, 2005 2:59 pm
awk -v RS="" -v FS="[ ,)(]*" '{print $3,$6,$12,$13,$14}' file
Subject: remove last and first three lines from a file
Question posted by Prince - 22 Dec 2004 16:11:28 -0800
I have a file with following contents.
COL1
--------------------------------
This is row number one
This is row number two, spanning
multiple lines
This is row number three
3 record(s) selected.
After extracting the number of "record(s) selected" (I can use AWK here), I want to remove the first and last three lines from the file. The script is in ksh and I can use sed or awk or something similar (no perl though). What is the best way to do accomplish this? I want the result to look like the following, with no headers and trailers.
This is row number one
This is row number two, spanning
multiple lines
This is row number three
Response posted by Bill Marcum - Wed, 22 Dec 2004 20:39:15 -0500
awk 'NR>6{print last3[NR%3]} {last3[NR%3]=$0}'
Response posted by Janis Papanagnou - Thurs, Dec 23 2004 1:04 pm
Generalized form of Bill's solution, allowing the number of deleted lines at the beginning and end to be specifed as positional parameters.
awk -v f=${1:?} -v r=${2:?} 'NR>f+r {print buf[NR%r]} {buf[NR%r]=$0}'
Subject: rules on stdout and stderr
Question posted by Laurent Deniau 2004-12-02 08:12:04 PST
I would like to filter both stdout and stderr of a program and then print the filtered messages on their respective source. So I was wondering if there is a way to do something like:
handle == stdout && /regex/ { rule for stdout }
handle == stderr && /regex/ { rule for stderr }
and then
prog | filter.awk
would process both channels separately
-- stdout --> -- stdout -->
prog filter.awk shell
-- stderr --> -- stderr -->
Response posted by Stephane CHAZELAS 2004-12-03 02:15:45 PST
{
{
cmd 3>&- 4>&- | awk 'stdout filter' >&3 2>&4 3>&- 4>&-
} 2>&1 | awk 'stderr filter' >&2 3>&- 4>&-
} 3>&1 4>&2
Subject: Problem With Regular Expression
Question posted by Tony George 30 Nov 2004 16:46:15 -0500
... extract just the basic pathname from the full version extended pathname. For example:
/vobs/test@@/main/tony/1/pets/main/fun/jim/22/cats/main/steve/2/siamese/main/5
The actual filename I'm trying to extract here is
/vobs/test/pets/cats/siamese
What I want to get rid of are all the branch names and version numbers in between subdirectories and the final file name.
Response posted by Walter Briscoe 1 Dec 2004 08:44:17 +0000
sed 's:@@::;s:/main/[^1-9]*[1-9][0-9]*::g'
Response posted by Stephane Chazelas 1 Dec 2004 08:46:23 +0000
sed 's@/main[^0-9]*[0-9]\{1,\}@@g'
Subject: How to grep for text1 and text2 and text3 on three different lines?
Question posted by Suhas Date: 23 Nov 2004 12:46:38 -0800
I have an ascii report file has the following text:
Net
Tare
Gross
Note that the information is one 3 separate lines & not necessarily starting at the first column. How can I write a condition:
if "Net" is found AND "Tare" is found AND "Gross" is found THEN true ELSE false.
Response posted by Stephane CHAZELAS Tue, 23 Nov 2004 21:08:50 +0000
if awk '/Net/ {n=1} /Tare/ {t=1} /Gross/ {g=1} END {exit(3-n-t-g)}' < report.file
then
echo "3 ones found"
else
echo "not 3 ones found"
fi
The input redirection from the "report.file" was implied but not explicitly stated in the original newsgroup posting.
Subject: Fields postion based on a string ...
Question posted by papi 2004-11-14 16:22:16 PST
I have a program whose output consists of a variable number of fields, separated by spaces, out of which one has a known value (string). I would like to obtain the first field, the one before and the one after the known value one, as in the example below:
$ program
x y z m w v t
p r m s o q
and I would like to be left with:
x z w
p r s
based on the known "m", of course, which is a string.
Response posted by Ed Morton 2004-11-14 17:30:34 PST
awk '{for (i=1; i<=NF; i++) if ($i == "m") print $1, $(i-1), $(i+1) }'
Subject: Searching a string with a date in a file more previous line
Question posted by Shiva MahaDeva 2004-11-14 17:24:23 PST
I'm looking for a way to search a string an the actual date in the same line in a file, and show me the previous line too. How could I make this ?
cats 11-05-04
dogs 11-15-04
rats 11-20-04
bees 10-05-04
I want search in this file the second line (dogs and 11-15-04, show-me this line and the previous line):
cats 11-05-04
dogs 11-15-04
How could I make this ? Thanks in advance.
Response posted by Bill Marcum 2004-11-15 00:00:02 PST
awk '/string/{print prev;print $0} {prev=$0}'
Subject: sed/awk - 2 simple question
Question posted by Robert Tulke 2004-10-20 15:09:56 PST
i've a file with following content:
---snip
helpc.de
Domain Beantragung hatte DNS-Fehler
hessenbruch-personal.de
Domain Beantragung hatte DNS-Fehler
hms-lamprecht.de
Domain Beantragung hatte DNS-Fehler
..
..
..
---snap
but i need the second line, direct after first line with two tabs and delete the 3. line... looks like here is the right content
helpc.de Domain Beantragung hatte DNS-Fehler
hessenbruch-personal.de Domain Beantragung hatte DNS-Fehler
hms-lamprecht.de Domain Beantragung hatte DNS-Fehler
Response posted by Stephane CHAZELAS 2004-10-20 23:46:58 PST
paste -d '\t\t\0' - /dev/null - - < your-file.txt
Subject: expr input from a file
Question posted by Faeandar 2004-10-06 16:05:30 PST
I've got a file that has numbers on every line. I want to add every line and get a total.
Response posted by Chris F.A. Johnson 2004-10-06 16:42:43 PST
echo $(( `tr -s "\012 " "[+*]" < $filename` 0 ))
Response posted by Seb 2004-10-06 16:26:07 PST
awk '{sum += $0} END {print sum}' yourFile
Subject: Replace a line with SED / Regex?? Need help.
Question posted by WAS Admin 2004-10-04 19:02:51 PST
What I'm trying to do is find a specific line in an xml file and the replace the line after it. For example: The xml file name is web.xml. Below, the line with "parm1" is the one I'll need to change to read
parm2
The contents of this line could change from time to time but the line above it (the one containing WebRoot)will always be the same.
WebRoot
parm1
This is an xml file
Is there a way to use a regex to search for the "URNRoot " line and then replace the line below with what I want?
Response posted by William Park 2004-10-04 19:26:13 PST
sed '/WebRoot/{n;s/parm1/parm2/;}'
Subject: AIX Script to Summarize By First Column By Adding Values in Numerical Columns
Question posted by SAP BASIS Consultant 2004-09-29 11:06:54 PST
I have a file which looks in style as follows (It is the output of ps aux, though that it not important):
(The COLs are not part of the file..For description)
COL1 COL2 COL3 COL4 COL5 COL6
name1 xxx xxx xxx 1000 11
name1 xxx xxx xxx 50 40
name2 xxx xxx xxx 30 5
name1 xxx xxx xxx 50 10
name2 xxx xxx xxx 100 4
I would like to summarize the data by the first column, ignoring columns #2, #3 and #4, and adding the values in col#5 and #6. The output would be as follows: (For name1, 1100=1000+50+50, etc..)
name1 1100 61
name2 130 9
Response posted by Chris F.A. Johnson 2004-09-29 13:38:51 PST
awk '{x[$1]+=$5;y[$1]+=$6}END{for(n in x) printf "%s\t%d\t%d\n",n,x[n],y[n]}'
Subject: Sorting by basename of file
Question posted by Vikas Agnihotri 2004-09-27 11:01:19 PST
I have a file containing 2 full pathnames per line like
/full/path/.../to/file /another/full/.../path/to/file
How can I sort this file by the "basename" of the second filename on the line?
Response posted by Michael Tosch 2004-09-27 11:36:32 PST
awk -F/ '{print $NF"/"$0}' file | sort | cut -d/ -f2-
Subject: stupid awk q
Question posted by foo@bar.com 2004-09-07 14:38:35 PST
Okay, so I want to sum up a stream a piped in group of numbers. I would normally do this with awk...but the problem with either awk or how I'm using awk is that it spits out a running total until the final sum. I'd rather just have the final sum w/o a running total...ie I want the rough total memory footprint of apache so I run:
ps -e -o 'vsz fname' |
grep httpd |
awk '{s += $1}{print s}'
But if I want just the total I have to pipe it though 'tail -1' Am I missing something about awk that would avoiding the running total?
Response posted by Seb 2004-09-07 14:51:03 PST
Response posted by Chris F.A. Johnson 2004-09-07 14:51:13 PST
ps -e -o 'vsz fname' | awk '/httpd/ {s += $1} END {print s}'
Subject: BASH: reading file lines with spaces
Question posted by Mark A Framness 2004-09-06 11:41:57 PST
I have a file consisting of multiple lines.
FAX DESTINATION ONE@5551111
FAX DESTINATION TWO@5552222
FAX DESTINATION THREE@5553333
FAX DESTINATION FOUR@5554444
I want to read the above file and append each line to a sendfax command so a sendfax command like:
sendfax -d FAX DESTINATION ONE@5551111\
-d FAX DESTINATION TWO@5552222 \
-d FAX DESTINATION THREE@5553333 \
-d FAX DESTINATION FOUR@5554444 \
faxfile.pdf
is built and executed.
Response posted by Ed Morton 2004-09-06 17:25:10 PST
sendfax `awk '{printf " -d %s",$0}' file` faxfile.pdf
Subject: changing a string in all users home-directory
Question posted by Tim Moor 2004-08-25 23:23:18 PST
i have to change a string in the .profile-file of all my users. all .profiles are located within the /home directory. the string i'd like to change looks like this:
/bin/old
and should be replaced with
/bin/new
Response posted by Alexis Huxley 2004-08-25 23:58:11 PST
perl -pi -e 's/\/bin\/old\b/\/bin\/new/' /home/*/.profile
Response posted by Stephane Chazelas 2004-08-26 00:19:34 PST
perl -pi -e 's,/bin/old,/bin/new,g' /home/*/.profile
You may prefer using:
perl -pi.before-my-change -e 's,/bin/old,/bin/new,g' /home/*/.profile
To keep a copy of the previous .profile file in case there has
been a problem in the substitution.
duplicate the paragraph n times
Question posted by Prince Kumar 2004-08-18 17:46:54 PST
I have a file with the following contents.
%cat test.txt
start para para_1 [
text1
text2
end ]
start para para_2 [
text1
text2
end ]
I want to duplicate each of these paragraphs 5 times. Using sed/awk/perl, how would I achieve this?
Response posted by Janis Papanagnou 2004-08-18 18:57:41 PST
With square brackets as block boundaries:
awk 'BEGIN { RS=""; ORS="\n\n" }
/\[/,/\]/ { print ; print ; print ; print ; print }'
or using a complex block pattern to avoid symbol clashes:
awk 'BEGIN { RS=""; ORS="\n\n" }
/start para .*\[/,/end \]/
{ print ; print ; print ; print ; print }'
Response posted by Brendon Caligari 2004-08-18 19:25:53 PST
perl -pe 'undef $/; s/^start para.*?end ]$/("$&\n\n" x 4) . $&/mesg' filename
Response posted by John W. Krahn 2004-08-19 00:27:35 PST
perl -00ne'$a = $_; print $a for 1 .. 5' test.txt
Response posted by rakesh sharma 2004-08-19 02:05:00 PST
sed -e '
/^start para/,/^end ]/!b
H;/^end ]/!d
g;G;G;G;G
x;s/.*//;x
' test.txt
Replace newlines selectively ( using sed )
Question posted by Martin Schweikert 2004-08-03 07:47:01 PST
I would like to tidy up bdf (HP-UX) output. Example:
/dev/vgcondort/oracle
4096000 1797302 2155065 45%
/opt/DOCUMENTUM/condort/oracle/product
/dev/vgcondort/tmp 2048000 6839 1913614 0%
/opt/DOCUMENTUM/condort/tmp
/dev/vgcondort/share
409600 96442 293618 25%
The newlines followed by the spaces and a digit should be translated into a blank.
Response posted by Rakesh Sharma 2004-08-03 22:24:36 PST
sed -e '
$!N
s/\n *\([0-9]\)/ \1/;t
P;D
' yourfile
Subject:
Path : Home : Tools : Scripts
This page contains one line ( or close to it ) shell script solutions to problems posted in the "comp.unix.shell" news group. Normally, a weekly selection of the best one line shell script solution is made and placed in this list.
Subject: extracting a pattern from file
Question posted by su Jun 3, 2005 1:50 pm
Data Format file
*****************************************************************
Table Name: Sales_Orders (dbid=6,objid=1115151018, lockscheme=Da)
Page Locks SH_PAGE UP_PAGE EX_PAGE
Grants: 0 0 7
Table Name: UMUM_UTIL_MGT (dbid=4, objid=1274956114,lockscheme=Al )
Page Locks SH_PAGE UP_PAGE EX_PAGE
Grants: 0 0 4
Table Name: UMUM_UTIL_NGT (dbid=4, objid=1274956123,lockscheme=Al )
Page Locks SH_PAGE UP_PAGE EX_PAGE
Grants: 0 0 3
*****************************************************************
Actually i tried hard to get the output in following format but no
luck.
output format
******************************************************************
Sales_Orders lockscheme=Da 0 0 7
UMUM_UTIL_MGT lockscheme=Al 0 0 4
UMUM_UTIL_NGT lockscheme=Al 0 0 3
******************************************************************
Response posted by Ed Morton Jun 3, 2005 2:59 pm
awk -v RS="" -v FS="[ ,)(]*" '{print $3,$6,$12,$13,$14}' file
Subject: remove last and first three lines from a file
Question posted by Prince - 22 Dec 2004 16:11:28 -0800
I have a file with following contents.
COL1
--------------------------------
This is row number one
This is row number two, spanning
multiple lines
This is row number three
3 record(s) selected.
After extracting the number of "record(s) selected" (I can use AWK here), I want to remove the first and last three lines from the file. The script is in ksh and I can use sed or awk or something similar (no perl though). What is the best way to do accomplish this? I want the result to look like the following, with no headers and trailers.
This is row number one
This is row number two, spanning
multiple lines
This is row number three
Response posted by Bill Marcum - Wed, 22 Dec 2004 20:39:15 -0500
awk 'NR>6{print last3[NR%3]} {last3[NR%3]=$0}'
Response posted by Janis Papanagnou - Thurs, Dec 23 2004 1:04 pm
Generalized form of Bill's solution, allowing the number of deleted lines at the beginning and end to be specifed as positional parameters.
awk -v f=${1:?} -v r=${2:?} 'NR>f+r {print buf[NR%r]} {buf[NR%r]=$0}'
Subject: rules on stdout and stderr
Question posted by Laurent Deniau 2004-12-02 08:12:04 PST
I would like to filter both stdout and stderr of a program and then print the filtered messages on their respective source. So I was wondering if there is a way to do something like:
handle == stdout && /regex/ { rule for stdout }
handle == stderr && /regex/ { rule for stderr }
and then
prog | filter.awk
would process both channels separately
-- stdout --> -- stdout -->
prog filter.awk shell
-- stderr --> -- stderr -->
Response posted by Stephane CHAZELAS 2004-12-03 02:15:45 PST
{
{
cmd 3>&- 4>&- | awk 'stdout filter' >&3 2>&4 3>&- 4>&-
} 2>&1 | awk 'stderr filter' >&2 3>&- 4>&-
} 3>&1 4>&2
Subject: Problem With Regular Expression
Question posted by Tony George 30 Nov 2004 16:46:15 -0500
... extract just the basic pathname from the full version extended pathname. For example:
/vobs/test@@/main/tony/1/pets/main/fun/jim/22/cats/main/steve/2/siamese/main/5
The actual filename I'm trying to extract here is
/vobs/test/pets/cats/siamese
What I want to get rid of are all the branch names and version numbers in between subdirectories and the final file name.
Response posted by Walter Briscoe 1 Dec 2004 08:44:17 +0000
sed 's:@@::;s:/main/[^1-9]*[1-9][0-9]*::g'
Response posted by Stephane Chazelas 1 Dec 2004 08:46:23 +0000
sed 's@/main[^0-9]*[0-9]\{1,\}@@g'
Subject: How to grep for text1 and text2 and text3 on three different lines?
Question posted by Suhas Date: 23 Nov 2004 12:46:38 -0800
I have an ascii report file has the following text:
Net
Tare
Gross
Note that the information is one 3 separate lines & not necessarily starting at the first column. How can I write a condition:
if "Net" is found AND "Tare" is found AND "Gross" is found THEN true ELSE false.
Response posted by Stephane CHAZELAS Tue, 23 Nov 2004 21:08:50 +0000
if awk '/Net/ {n=1} /Tare/ {t=1} /Gross/ {g=1} END {exit(3-n-t-g)}' < report.file
then
echo "3 ones found"
else
echo "not 3 ones found"
fi
The input redirection from the "report.file" was implied but not explicitly stated in the original newsgroup posting.
Subject: Fields postion based on a string ...
Question posted by papi 2004-11-14 16:22:16 PST
I have a program whose output consists of a variable number of fields, separated by spaces, out of which one has a known value (string). I would like to obtain the first field, the one before and the one after the known value one, as in the example below:
$ program
x y z m w v t
p r m s o q
and I would like to be left with:
x z w
p r s
based on the known "m", of course, which is a string.
Response posted by Ed Morton 2004-11-14 17:30:34 PST
awk '{for (i=1; i<=NF; i++) if ($i == "m") print $1, $(i-1), $(i+1) }'
Subject: Searching a string with a date in a file more previous line
Question posted by Shiva MahaDeva 2004-11-14 17:24:23 PST
I'm looking for a way to search a string an the actual date in the same line in a file, and show me the previous line too. How could I make this ?
cats 11-05-04
dogs 11-15-04
rats 11-20-04
bees 10-05-04
I want search in this file the second line (dogs and 11-15-04, show-me this line and the previous line):
cats 11-05-04
dogs 11-15-04
How could I make this ? Thanks in advance.
Response posted by Bill Marcum 2004-11-15 00:00:02 PST
awk '/string/{print prev;print $0} {prev=$0}'
Subject: sed/awk - 2 simple question
Question posted by Robert Tulke 2004-10-20 15:09:56 PST
i've a file with following content:
---snip
helpc.de
Domain Beantragung hatte DNS-Fehler
hessenbruch-personal.de
Domain Beantragung hatte DNS-Fehler
hms-lamprecht.de
Domain Beantragung hatte DNS-Fehler
..
..
..
---snap
but i need the second line, direct after first line with two tabs and delete the 3. line... looks like here is the right content
helpc.de Domain Beantragung hatte DNS-Fehler
hessenbruch-personal.de Domain Beantragung hatte DNS-Fehler
hms-lamprecht.de Domain Beantragung hatte DNS-Fehler
Response posted by Stephane CHAZELAS 2004-10-20 23:46:58 PST
paste -d '\t\t\0' - /dev/null - - < your-file.txt
Subject: expr input from a file
Question posted by Faeandar 2004-10-06 16:05:30 PST
I've got a file that has numbers on every line. I want to add every line and get a total.
Response posted by Chris F.A. Johnson 2004-10-06 16:42:43 PST
echo $(( `tr -s "\012 " "[+*]" < $filename` 0 ))
Response posted by Seb 2004-10-06 16:26:07 PST
awk '{sum += $0} END {print sum}' yourFile
Subject: Replace a line with SED / Regex?? Need help.
Question posted by WAS Admin 2004-10-04 19:02:51 PST
What I'm trying to do is find a specific line in an xml file and the replace the line after it. For example: The xml file name is web.xml. Below, the line with "parm1" is the one I'll need to change to read
The contents of this line could change from time to time but the line above it (the one containing WebRoot)will always be the same.
Is there a way to use a regex to search for the "
Response posted by William Park 2004-10-04 19:26:13 PST
sed '/WebRoot/{n;s/parm1/parm2/;}'
Subject: AIX Script to Summarize By First Column By Adding Values in Numerical Columns
Question posted by SAP BASIS Consultant 2004-09-29 11:06:54 PST
I have a file which looks in style as follows (It is the output of ps aux, though that it not important):
(The COLs are not part of the file..For description)
COL1 COL2 COL3 COL4 COL5 COL6
name1 xxx xxx xxx 1000 11
name1 xxx xxx xxx 50 40
name2 xxx xxx xxx 30 5
name1 xxx xxx xxx 50 10
name2 xxx xxx xxx 100 4
I would like to summarize the data by the first column, ignoring columns #2, #3 and #4, and adding the values in col#5 and #6. The output would be as follows: (For name1, 1100=1000+50+50, etc..)
name1 1100 61
name2 130 9
Response posted by Chris F.A. Johnson 2004-09-29 13:38:51 PST
awk '{x[$1]+=$5;y[$1]+=$6}END{for(n in x) printf "%s\t%d\t%d\n",n,x[n],y[n]}'
Subject: Sorting by basename of file
Question posted by Vikas Agnihotri 2004-09-27 11:01:19 PST
I have a file containing 2 full pathnames per line like
/full/path/.../to/file /another/full/.../path/to/file
How can I sort this file by the "basename" of the second filename on the line?
Response posted by Michael Tosch 2004-09-27 11:36:32 PST
awk -F/ '{print $NF"/"$0}' file | sort | cut -d/ -f2-
Subject: stupid awk q
Question posted by foo@bar.com 2004-09-07 14:38:35 PST
Okay, so I want to sum up a stream a piped in group of numbers. I would normally do this with awk...but the problem with either awk or how I'm using awk is that it spits out a running total until the final sum. I'd rather just have the final sum w/o a running total...ie I want the rough total memory footprint of apache so I run:
ps -e -o 'vsz fname' |
grep httpd |
awk '{s += $1}{print s}'
But if I want just the total I have to pipe it though 'tail -1' Am I missing something about awk that would avoiding the running total?
Response posted by Seb 2004-09-07 14:51:03 PST
Response posted by Chris F.A. Johnson 2004-09-07 14:51:13 PST
ps -e -o 'vsz fname' | awk '/httpd/ {s += $1} END {print s}'
Subject: BASH: reading file lines with spaces
Question posted by Mark A Framness 2004-09-06 11:41:57 PST
I have a file consisting of multiple lines.
FAX DESTINATION ONE@5551111
FAX DESTINATION TWO@5552222
FAX DESTINATION THREE@5553333
FAX DESTINATION FOUR@5554444
I want to read the above file and append each line to a sendfax command so a sendfax command like:
sendfax -d FAX DESTINATION ONE@5551111\
-d FAX DESTINATION TWO@5552222 \
-d FAX DESTINATION THREE@5553333 \
-d FAX DESTINATION FOUR@5554444 \
faxfile.pdf
is built and executed.
Response posted by Ed Morton 2004-09-06 17:25:10 PST
sendfax `awk '{printf " -d %s",$0}' file` faxfile.pdf
Subject: changing a string in all users home-directory
Question posted by Tim Moor 2004-08-25 23:23:18 PST
i have to change a string in the .profile-file of all my users. all .profiles are located within the /home directory. the string i'd like to change looks like this:
/bin/old
and should be replaced with
/bin/new
Response posted by Alexis Huxley 2004-08-25 23:58:11 PST
perl -pi -e 's/\/bin\/old\b/\/bin\/new/' /home/*/.profile
Response posted by Stephane Chazelas 2004-08-26 00:19:34 PST
perl -pi -e 's,/bin/old,/bin/new,g' /home/*/.profile
You may prefer using:
perl -pi.before-my-change -e 's,/bin/old,/bin/new,g' /home/*/.profile
To keep a copy of the previous .profile file in case there has
been a problem in the substitution.
duplicate the paragraph n times
Question posted by Prince Kumar 2004-08-18 17:46:54 PST
I have a file with the following contents.
%cat test.txt
start para para_1 [
text1
text2
end ]
start para para_2 [
text1
text2
end ]
I want to duplicate each of these paragraphs 5 times. Using sed/awk/perl, how would I achieve this?
Response posted by Janis Papanagnou 2004-08-18 18:57:41 PST
With square brackets as block boundaries:
awk 'BEGIN { RS=""; ORS="\n\n" }
/\[/,/\]/ { print ; print ; print ; print ; print }'
or using a complex block pattern to avoid symbol clashes:
awk 'BEGIN { RS=""; ORS="\n\n" }
/start para .*\[/,/end \]/
{ print ; print ; print ; print ; print }'
Response posted by Brendon Caligari 2004-08-18 19:25:53 PST
perl -pe 'undef $/; s/^start para.*?end ]$/("$&\n\n" x 4) . $&/mesg' filename
Response posted by John W. Krahn 2004-08-19 00:27:35 PST
perl -00ne'$a = $_; print $a for 1 .. 5' test.txt
Response posted by rakesh sharma 2004-08-19 02:05:00 PST
sed -e '
/^start para/,/^end ]/!b
H;/^end ]/!d
g;G;G;G;G
x;s/.*//;x
' test.txt
Replace newlines selectively ( using sed )
Question posted by Martin Schweikert 2004-08-03 07:47:01 PST
I would like to tidy up bdf (HP-UX) output. Example:
/dev/vgcondort/oracle
4096000 1797302 2155065 45%
/opt/DOCUMENTUM/condort/oracle/product
/dev/vgcondort/tmp 2048000 6839 1913614 0%
/opt/DOCUMENTUM/condort/tmp
/dev/vgcondort/share
409600 96442 293618 25%
The newlines followed by the spaces and a digit should be translated into a blank.
Response posted by Rakesh Sharma 2004-08-03 22:24:36 PST
sed -e '
$!N
s/\n *\([0-9]\)/ \1/;t
P;D
' yourfile
Subject:
solaris one liners
Unix/Solaris: One-Liners
Source: http://www.kevlo.com/~ebs/unix_commands.txt
Listed here are a bunch of unix commands.
--> change file date stamp
touch –t 199906042020 filename
--> move partitions
ufsdump 0f - /dev/rdsk/c0t0s0s0 | (cd /home; ufsrestore xv -)
--> lay down file system with 1% minfree and inode density
newfs –m1 –i81920 /dev/rdsk/c0t0d0s0
--> check file system
fsck /dev/rdsk/c0t0d0s0
Q: starting sybase
login as sybase, run: ./install/RUN_SYBASE
Q: logging in as sybase sa
isql -U sa
--> dump a partition and pipe to gzip. Watch > 2GB limit
ufsdump 0f - /home | gzip - >/tmp/home.dump.gz
--> rewind offline a tape
mt –f /dev/rmt/0 rewoffl
--> only allow 300MB for user /tmp access
swap - /tmp tmpfs – yes SIZE=300M
--> verbose interactive restore
ufsrestore –ivf /dev/rmt/1
--> remove a printer from a class
lpadmin –p level5-line1 –r level5-line
--> truss a command
truss –-f -–o /tmp/log.txt
--> [DB] feed a script into sybase
isql –Urfe_xfer -Uuser -Ppassword -isqlscript >>blah.txt
--> make a printer class
lpadmin –p level5-line1 –c level5-line
--> remove level2-line2 printer from printer class level2-line
lpadmin -p level2-line2 -r level2-line
--> add level2-line3 to printer class
lpadmin -c level2-line -p level2-line3
--> [DB] how to change your password in isql
sp_password password, password-new
--> move a directory
tar cf - ./games | (cd /tmp; tar xvBpf - )
--> [DB] run a sybase script, and dump to file
$ISQL -i$SCRIPTFILE -U$USER -D$DATABASE -P$PASS_ENC >> $SCRIPTLOGFILE
--> move a directory to another server
tar cf - ./games | rsh brucey cd /tmp\; tar xvBpf -
--> check for SUID SGID files
ncheck -F ufs -s /dev/dsk/c3t0d0s
-- remove core files
find / -name core –exec rm –f {} \; -o –fstype nfs –prune
--> rebuild man pages
catman –w –M man-page-directory or /usr/lib/makewhatis
--> vi command to show special characters
: set list
--> adding an account
useradd -u 120 –g dls -d /apps/dls –s /bin/ksh -c "comment" -m dls
--> create a mysql database
mysqladmin -uroot -ppassword create ebs
--> starting mysql database
/etc/rc.d/init.d/mysql.server start
/usr/local/bin/safe_mysqld
--> Invoke CPAN module install
perl –MCPAN –eshell
--> dump to zip
ufsdump 0f - /filesystem | /opt/local/gzip - > /tmp/dump.gz
--> shutdown mysql databse
/usr/local/bin/mysqladmin shutdown -ppassword
/etc/rc.d/init.d/mysql.server stop
--> test the loading of a module
PERL_DL_DEBUG=255 perl -e 'use CGI;'
--> shows open files
fuser –cu /
--> Writing a Daemon:
1. edit /etc/services
add service and port.
2. edit /etc/inetd.conf
add in: edwardd stream tcp nowait root /bin/sh /bin/sh /home/sextone/bin/SERVER.mine
3. kill –HUP inetd.conf
--> how to mount a file system
mount /dev/dsk/c3t0d0s4 /apps/data/easysoft/DEVT
--> look at sar log
sar –f /var/adm/sa/sa24
--> write file checksums and size
cksum filename
--> show storage array info
ssaadm display /dev/rdsk/c1t5d2s0
--> show all disks on device d
luxadm display d
--> examine for a specific OS finerprint
nmap –sS -p 80 -O -v = examine OS
--> show print jobs
/usr/ucb/lpq –Plevel6
--> Scan for known ports. log it. do OS scan.
nmap –sS -F -o foo.log -v -O www.foo.com//24 =
--> show status of printer
/usr/ucb/lpc status
--> make a swap file:
dd if=/dev/zero of=swapfile bs=1024 count=65535
mkswap ./swapfile
chmod 600 ./swapfile
swapon ./swapfile
--> show open files for process
lsof –p PID
--> show open files for all TCP connections
lsof –iTCP
--> show open files for internet address
lsof -iTCP@10.20.2.9
--> as above
lsof -i @10.20.2.122
--> examine tcp ports
lsof -iTCP@sarah:1-50000
--> show open files for user.
lsof –u username
--> show processes that has the file in use.
lsof /apps/cms/ECMS-Server
--> show open files and retry every 5 seconds
lsof –p process-id –r 5
--> mount a floppy
mount -t vfat /dev/fd0 /mnt/floppy
--> check here for debugging processes and errno.h for errors
/usr/include
/usr/include/sys
/usr/include/sys/errno.h
--> scp a whole directory, preserve mods
sudo scp -prv devel webadmin@203.19.123.140:/home/httpd/cgi-bin
--> take processor 2 and 3 offline.
psradm -f 2 3
--> show processor stats verbose.
psrinfo –v
--> how to skip grant tables in mysql (over ride security)
/usr/local/libexec/mysqld -Sg
--> how to feed in an SQL program
mysql
--> rm all files in directories
find . -type f -exec rm {} \;
--> dump packets to a capture file
sudo snoop –o /tmp/tcp.txt cp
--> backup one liner
tar cvf - /home/ebs | gzip - > ebs.tar.gz
--> Look at selected packets in capture file
sudo snoop -i /tmp/tcp.txt
--> unzip and pipe to tar
gzip -dc
--> watch packets from two servers.
snoop sarah brucey
--> enable ip masquerading
/sbin/ipchains -P forward DENY
/sbin/ipchains -A forward -s 10.100.100.100/8 -j MASQ
-> view su log file
cat /var/adm/sulog
--> establish a default router or gateway.
echo "sagacity.com" > /etc/defaultrouter
echo "10.100.100.100 sagacity.com sagacity" >> /etc/hosts
change /etc/nsswitch.conf so that hosts has files, dns
edit resolv.conf put in
search .
nameserver 203.7.132.98
--> turn off automounter on /export/home.
vi /etc/auto_master, comment out /export/home
--> configuration file for sudoers
/opt/local/etc/sudoers
--> building ssh-1.2.27 on x86Solaris2.6 needed a few things:
/usr/openwin/bin in path
/usr/xpg4/bin in path
declare AR="/usr/xpg4/bin/ar"
declare NM_PATH="/usr/xpg4/bin/nm"
--> snoop network packets and get size and time stamp entries.
snoop -S -ta empa1
--> access perl CPAN
perl -MCPAN -e shell
install DBI
--> search for no password entries and lock all accounts.
for i in `passwd –sa | grep NP | awk ‘{print $1’`
do
echo “locking $i”
passwd –l $i
done
--> delete from a tar
tar --delete -f fs_backup_Sunday.tar home/ebs/tmp
--> Example on backing up files to tape. Must specify non rewinding, else you will over-write the files.
for file in `ls`
do
echo "sending $file to tape..."
echo `date`
tar cvpf /dev/rmt/0n $file
done
--> making/adding a partition.
1. use fdisk to make a parition.
2. mkfs -t ext2 -c /dev/hda11
3. mount -t ext2 /dev/hda11 /opt2
4. update /etc/fstab
--> rebuild the windex file
catman –w –M /usr/share/man
--> execute tar on remote host sarah and send tarball to standard output,
which becomes standard input for tar xvf – and the file gets dumped locally,
in this case on crawl. you have to cd to dir before tar or else you
will include path in tar
ssh maggie "cd $DIRNAME; tar cvf - $BASENAME" | (cd $TPATH; tar xvf - )
--> dump a remote filesystem and send it to local tape drive.
ssh -–x $fw /usr/sbin/ufsdump 0cf - $fs | dd obs=63k of=$TAPE
--> encrypt filename 1 and output to 1.crypt file
crypt < 1 > 1.crypt ; rm 1
--> decrypt filename 1.crypt and stdout to screen
crypt < 1.crypt
--> send a file to tape
tar cvpf /dev/rmt/0 filename
--> quicker way to search and replace in vi
: %s/existing/new/g
--> shows where and which shared library files an application uses.
ldd binary
--> shell script stuff:
# repeat a command 100 times
x=100
while [ $x -gt 0 ]
do
command
x=$(($x-1))
done
--> Something very important to remember about partitions
It is important to note that Cylinder 0 contains the disklabel, which
contains the partition table. A normal filesystem can be placed
starting at Cylinder 0, since it will not touch the disklabel.
If you lay down a raw device, for a database, over Cylinder 0,
then you will completely lose all your partitions. You will then
have to restore the disklabel, and backup from tape if you happen to do this.
--> move a partition
find . |cpio -pdm /apps
--> cron structure
min hour day-of-month month weekday command
--> PatchDiag Tool. Get patches from:
http://sunsolve.sun.com/private-cgi/patchpage.pl
patchdiag.xref is available at: http://sunsolve.sun.com/sunsolve/patchdiag/
/opt/local/bin/patchdiag -x /opt/local/lib/patchdiag.xref > patchdiag.`uname -n`
--> command showing system parameters
/usr/sbin/sysdef
--> Get Ambient Temperature of Server
/usr/platform/SUNW,Ultra-4/sbin/prtdiag -v
--> good ps formatting showing percent cpu first.
ps -edf -o pcpu,pid,user,arg
--> full details on ps
/usr/bin/ps –A -o user,pid,pcpu,pmem,vsz,rss,tty,s,stime,time,args
--> chown the hidden files as well.
find . -print -exec chown -R sextone:staff {} \;
--> The nsradmin command is a command-line based administrative
program for the NetWorker system. Normally nsradmin monitors
and modifies NetWorker resources over the network.
/usr/sbin/nsr/nsradmin
--> Spray a server
-c number of packets
-d delay in microseconds
-l pakcet size in bytes
/usr/sbin/spray -c 1 –d 20 -l 4096 maggie
--> Turn on bold.
bold=`tput smso`
offbold=`tput rmso`
echo "${bold}You must be the \"root\" user to run this script.${offbold}"
--> good way to send a dir to tape
tar cf /dev/rmt/0n directory
--> example of bringing up an interface
ifconfig hme0:1 inet 10.2.25.52 up
--> show all connections
netstat –f inet
--> rpcinfo makes an RPC call to an RPC server and reports
what it finds.
rpcinfo -b 390109 2 | sort -u
--> rewind a tape fast
< /dev/rmt/0
--> show loaded modules
/usr/sbin/modinfo
--> find world readable files and dirs
find / -type d –perm -2 –print
find . -type f –perm -2 -print
--> adding in a boot alias, eg:
boot sarahroot1 –s
nvalias sarahroot1 /sbus@1f,0/sunw,fas@e,8800000/sd@9,0:a
--> clever way to archive
tar cvf - `find . –print` >/tmp/dumpfile.tar
tar xvf -
--> tee to a file
echo "Start Date/Time: `date`" | tee -a $LOG_FILE
--> read a snoop file
snoop -i anz-telnet.snoop
--> write a snoop log (this will count the number of connections, which is pretty neat).
snoop –osnoop.log sarah
--> set default run level. 5 for gui.
/etc/inittab
--> show all exported filesystems
showmount -e crawl
--> shows all configurable variables for tcp interface.
sudo ndd -get /dev/tcp
- ?
eg:
sudo ndd -get /dev/tcp tcp_conn_req_max_q
128
ndd /dev/arp \?
ndd /dev/ip \?
ndd /dev/tcp \?
ndd /dev/udp \?
ndd /dev/icmp \?
--> set sticky bit on group files, only the owner can change the mode.
--> the +l is mandatory file and record locking while a program
--> is accessing that file.
chmod g+s,+l file
--> print duplex landscape 4 qudrant printing
mpage –t –l –4
--> install a patch
installpatch .
--> check to see if a patch has been installed
showrev –p |grep package name
--> unzip, untar in a /tmp directory
zcat 104708-16.tar.gz | ( cd /tmp; sudo tar xvf - )
--> check out revision level on ssa controller
/usr/sbin/ssaadm display controller
--> unzip and untar a file without having to create an intermediate tar file
sudo gzip -dc /tmp/270599/post-EOD.tar.gz |tar xvf -
--> selectively extract from a tar archive
tar xvf /tmp/iona.tar ./iona/.sh_history
--> send a bunch of files to tape
tar cf /tmp/rules.tar ruleb* objects.C *.W
--> examine section 5 of man
man -s 5 signal
--> shows signals and definitions of structures, eg sigaction
/usr/include/sys/signal.h
--> location of the limits file on solaris
/usr/include/limits.h
--> send an attachment via email from command prompt
uuencode file.tar.gz file.tar.gz | mailx –s “backup” root@crawl
--> zero a file
cat /dev/null > isam.log
--> good way to restore from cdrom a binary file
zcat < /cdrom/cdrom0/Solaris_2.6/Product/SUNWcsu/install/reloc.cpio.Z |
cpio –idm usr/lib/fs/ufs/ufsrestore
--> running su as a user then ssh
su - dls-PROD -c "/opt/local/bin/ssh drp-stagger \"cd /tmp; /bin/ls\" "
--> verify a newfs format
sudo newfs –Nv /dev/md/dsk/d96
--> making lost_found. must be 8192 bytes in size.
mkdir ./lost+found;chown root ./lost+found; chgrp root ./lost+found ;chmod 700 ./lost+found’; cd ./lost+found
nofiles=0 ; while [ "$nofiles" -le 650 ] ; do ; /usr/ucb/touch $nofiles ; nofiles=`expr $nofiles + 1` ; done
--> execute lynx
lynx -cfg /usr/lib/lynx.cfg
--> sed search example
sed '/Sep\ 25/!d; /castill/!d' /var/log/syslo
-->should only be used at the EEPROM
boot –r
--> should be used at single user mode
reboot -- -r
--> should be used in multiuser mode
touch /reconfigure
--> performing a remote dump
find MFASYS
|cpio -oc |gzip -c
|ssh brucey -l chaup dd obs=18k of=/dev/rmt/0n
- to extract -
cd /ssa/emphasys/sybase/dump
dd ibs=18k if=|gunzip -c |cpio –idc
--> boot block located here.
/usr/platform/`uname –i`/lib/fs/ufs
--> getting a server on the network
add hosts entry for IP address
clear configs: ifconfig pe0 unplumb
ifconfig pe0 10.20.2.27 netmask 255.0.0.0 up
route add default 10.20.0.1 1
verify the routing table: netstat –rn
add resolv.conf entry: domain rabobank.com.au nameserver 192.192.192.252
edit /etc/nsswitch.conf change hosts to files, dns
lesson here is to unplumb interface, and let ifconfig setup the routing.
if you specify an ip address and a netmask it will manage
the routing and the broadcasting.
--> find all, files associated with PID 22240
/usr/proc/bin/pfiles 22240
find file based on inode
find –i number
“ncheck –i number
--> good redirection example
./a.out trash
--> synchronize files from one server to another. This is useful for
synchronizing database dump files, binary files, etc. This is definitely a powerful tool.
rsync -avz -e ssh --rsync-path="/usr/local/bin/rsync" `pwd` myhost.com:/home/ebs/public_html
--> Example Awk Script
# run with awk -f/tmp/1.awk /etc/group
BEGIN { FS = ":" }
{ print $1 | "sort" }
{ nlines++ }
END { print nlines }
--> awk example.
awk '/#/ {print "Got a comment"}' /etc/hosts
--> delete every 2nd field in file
awk '{$2= ""; print}' datafile > datafile.new
--> awk average/standard deviation program
x1 += $1
x2 += $1*$1
END {
x1 = x1/NR
x2 = x2/NR
sigma = sqrt(x2 - x1*x1)
if (NR > 1) std_err = sigma/sqrt(NR - 1)
print "Number of points = " NR
print "Mean = " x1
print "Standard Deviation = " sigma
print "Standard Error = " std_err
Source: http://www.kevlo.com/~ebs/unix_commands.txt
Listed here are a bunch of unix commands.
--> change file date stamp
touch –t 199906042020 filename
--> move partitions
ufsdump 0f - /dev/rdsk/c0t0s0s0 | (cd /home; ufsrestore xv -)
--> lay down file system with 1% minfree and inode density
newfs –m1 –i81920 /dev/rdsk/c0t0d0s0
--> check file system
fsck /dev/rdsk/c0t0d0s0
Q: starting sybase
login as sybase, run: ./install/RUN_SYBASE
Q: logging in as sybase sa
isql -U sa
--> dump a partition and pipe to gzip. Watch > 2GB limit
ufsdump 0f - /home | gzip - >/tmp/home.dump.gz
--> rewind offline a tape
mt –f /dev/rmt/0 rewoffl
--> only allow 300MB for user /tmp access
swap - /tmp tmpfs – yes SIZE=300M
--> verbose interactive restore
ufsrestore –ivf /dev/rmt/1
--> remove a printer from a class
lpadmin –p level5-line1 –r level5-line
--> truss a command
truss –-f -–o /tmp/log.txt
--> [DB] feed a script into sybase
isql –Urfe_xfer -Uuser -Ppassword -isqlscript >>blah.txt
--> make a printer class
lpadmin –p level5-line1 –c level5-line
--> remove level2-line2 printer from printer class level2-line
lpadmin -p level2-line2 -r level2-line
--> add level2-line3 to printer class
lpadmin -c level2-line -p level2-line3
--> [DB] how to change your password in isql
sp_password password, password-new
--> move a directory
tar cf - ./games | (cd /tmp; tar xvBpf - )
--> [DB] run a sybase script, and dump to file
$ISQL -i$SCRIPTFILE -U$USER -D$DATABASE -P$PASS_ENC >> $SCRIPTLOGFILE
--> move a directory to another server
tar cf - ./games | rsh brucey cd /tmp\; tar xvBpf -
--> check for SUID SGID files
ncheck -F ufs -s /dev/dsk/c3t0d0s
-- remove core files
find / -name core –exec rm –f {} \; -o –fstype nfs –prune
--> rebuild man pages
catman –w –M man-page-directory or /usr/lib/makewhatis
--> vi command to show special characters
: set list
--> adding an account
useradd -u 120 –g dls -d /apps/dls –s /bin/ksh -c "comment" -m dls
--> create a mysql database
mysqladmin -uroot -ppassword create ebs
--> starting mysql database
/etc/rc.d/init.d/mysql.server start
/usr/local/bin/safe_mysqld
--> Invoke CPAN module install
perl –MCPAN –eshell
--> dump to zip
ufsdump 0f - /filesystem | /opt/local/gzip - > /tmp/dump.gz
--> shutdown mysql databse
/usr/local/bin/mysqladmin shutdown -ppassword
/etc/rc.d/init.d/mysql.server stop
--> test the loading of a module
PERL_DL_DEBUG=255 perl -e 'use CGI;'
--> shows open files
fuser –cu /
--> Writing a Daemon:
1. edit /etc/services
add service and port.
2. edit /etc/inetd.conf
add in: edwardd stream tcp nowait root /bin/sh /bin/sh /home/sextone/bin/SERVER.mine
3. kill –HUP inetd.conf
--> how to mount a file system
mount /dev/dsk/c3t0d0s4 /apps/data/easysoft/DEVT
--> look at sar log
sar –f /var/adm/sa/sa24
--> write file checksums and size
cksum filename
--> show storage array info
ssaadm display /dev/rdsk/c1t5d2s0
--> show all disks on device d
luxadm display d
--> examine for a specific OS finerprint
nmap –sS -p 80 -O -v
--> show print jobs
/usr/ucb/lpq –Plevel6
--> Scan for known ports. log it. do OS scan.
nmap –sS -F -o foo.log -v -O www.foo.com//24 =
--> show status of printer
/usr/ucb/lpc status
--> make a swap file:
dd if=/dev/zero of=swapfile bs=1024 count=65535
mkswap ./swapfile
chmod 600 ./swapfile
swapon ./swapfile
--> show open files for process
lsof –p PID
--> show open files for all TCP connections
lsof –iTCP
--> show open files for internet address
lsof -iTCP@10.20.2.9
--> as above
lsof -i @10.20.2.122
--> examine tcp ports
lsof -iTCP@sarah:1-50000
--> show open files for user.
lsof –u username
--> show processes that has the file in use.
lsof /apps/cms/ECMS-Server
--> show open files and retry every 5 seconds
lsof –p process-id –r 5
--> mount a floppy
mount -t vfat /dev/fd0 /mnt/floppy
--> check here for debugging processes and errno.h for errors
/usr/include
/usr/include/sys
/usr/include/sys/errno.h
--> scp a whole directory, preserve mods
sudo scp -prv devel webadmin@203.19.123.140:/home/httpd/cgi-bin
--> take processor 2 and 3 offline.
psradm -f 2 3
--> show processor stats verbose.
psrinfo –v
--> how to skip grant tables in mysql (over ride security)
/usr/local/libexec/mysqld -Sg
--> how to feed in an SQL program
mysql
--> rm all files in directories
find . -type f -exec rm {} \;
--> dump packets to a capture file
sudo snoop –o /tmp/tcp.txt cp
--> backup one liner
tar cvf - /home/ebs | gzip - > ebs.tar.gz
--> Look at selected packets in capture file
sudo snoop -i /tmp/tcp.txt
--> unzip and pipe to tar
gzip -dc
--> watch packets from two servers.
snoop sarah brucey
--> enable ip masquerading
/sbin/ipchains -P forward DENY
/sbin/ipchains -A forward -s 10.100.100.100/8 -j MASQ
-> view su log file
cat /var/adm/sulog
--> establish a default router or gateway.
echo "sagacity.com" > /etc/defaultrouter
echo "10.100.100.100 sagacity.com sagacity" >> /etc/hosts
change /etc/nsswitch.conf so that hosts has files, dns
edit resolv.conf put in
search .
nameserver 203.7.132.98
--> turn off automounter on /export/home.
vi /etc/auto_master, comment out /export/home
--> configuration file for sudoers
/opt/local/etc/sudoers
--> building ssh-1.2.27 on x86Solaris2.6 needed a few things:
/usr/openwin/bin in path
/usr/xpg4/bin in path
declare AR="/usr/xpg4/bin/ar"
declare NM_PATH="/usr/xpg4/bin/nm"
--> snoop network packets and get size and time stamp entries.
snoop -S -ta empa1
--> access perl CPAN
perl -MCPAN -e shell
install DBI
--> search for no password entries and lock all accounts.
for i in `passwd –sa | grep NP | awk ‘{print $1’`
do
echo “locking $i”
passwd –l $i
done
--> delete from a tar
tar --delete -f fs_backup_Sunday.tar home/ebs/tmp
--> Example on backing up files to tape. Must specify non rewinding, else you will over-write the files.
for file in `ls`
do
echo "sending $file to tape..."
echo `date`
tar cvpf /dev/rmt/0n $file
done
--> making/adding a partition.
1. use fdisk to make a parition.
2. mkfs -t ext2 -c /dev/hda11
3. mount -t ext2 /dev/hda11 /opt2
4. update /etc/fstab
--> rebuild the windex file
catman –w –M /usr/share/man
--> execute tar on remote host sarah and send tarball to standard output,
which becomes standard input for tar xvf – and the file gets dumped locally,
in this case on crawl. you have to cd to dir before tar or else you
will include path in tar
ssh maggie "cd $DIRNAME; tar cvf - $BASENAME" | (cd $TPATH; tar xvf - )
--> dump a remote filesystem and send it to local tape drive.
ssh -–x $fw /usr/sbin/ufsdump 0cf - $fs | dd obs=63k of=$TAPE
--> encrypt filename 1 and output to 1.crypt file
crypt < 1 > 1.crypt ; rm 1
--> decrypt filename 1.crypt and stdout to screen
crypt < 1.crypt
--> send a file to tape
tar cvpf /dev/rmt/0 filename
--> quicker way to search and replace in vi
: %s/existing/new/g
--> shows where and which shared library files an application uses.
ldd binary
--> shell script stuff:
# repeat a command 100 times
x=100
while [ $x -gt 0 ]
do
command
x=$(($x-1))
done
--> Something very important to remember about partitions
It is important to note that Cylinder 0 contains the disklabel, which
contains the partition table. A normal filesystem can be placed
starting at Cylinder 0, since it will not touch the disklabel.
If you lay down a raw device, for a database, over Cylinder 0,
then you will completely lose all your partitions. You will then
have to restore the disklabel, and backup from tape if you happen to do this.
--> move a partition
find . |cpio -pdm /apps
--> cron structure
min hour day-of-month month weekday command
--> PatchDiag Tool. Get patches from:
http://sunsolve.sun.com/private-cgi/patchpage.pl
patchdiag.xref is available at: http://sunsolve.sun.com/sunsolve/patchdiag/
/opt/local/bin/patchdiag -x /opt/local/lib/patchdiag.xref > patchdiag.`uname -n`
--> command showing system parameters
/usr/sbin/sysdef
--> Get Ambient Temperature of Server
/usr/platform/SUNW,Ultra-4/sbin/prtdiag -v
--> good ps formatting showing percent cpu first.
ps -edf -o pcpu,pid,user,arg
--> full details on ps
/usr/bin/ps –A -o user,pid,pcpu,pmem,vsz,rss,tty,s,stime,time,args
--> chown the hidden files as well.
find . -print -exec chown -R sextone:staff {} \;
--> The nsradmin command is a command-line based administrative
program for the NetWorker system. Normally nsradmin monitors
and modifies NetWorker resources over the network.
/usr/sbin/nsr/nsradmin
--> Spray a server
-c number of packets
-d delay in microseconds
-l pakcet size in bytes
/usr/sbin/spray -c 1 –d 20 -l 4096 maggie
--> Turn on bold.
bold=`tput smso`
offbold=`tput rmso`
echo "${bold}You must be the \"root\" user to run this script.${offbold}"
--> good way to send a dir to tape
tar cf /dev/rmt/0n directory
--> example of bringing up an interface
ifconfig hme0:1 inet 10.2.25.52 up
--> show all connections
netstat –f inet
--> rpcinfo makes an RPC call to an RPC server and reports
what it finds.
rpcinfo -b 390109 2 | sort -u
--> rewind a tape fast
< /dev/rmt/0
--> show loaded modules
/usr/sbin/modinfo
--> find world readable files and dirs
find / -type d –perm -2 –print
find . -type f –perm -2 -print
--> adding in a boot alias, eg:
boot sarahroot1 –s
nvalias sarahroot1 /sbus@1f,0/sunw,fas@e,8800000/sd@9,0:a
--> clever way to archive
tar cvf - `find . –print` >/tmp/dumpfile.tar
tar xvf -
--> tee to a file
echo "Start Date/Time: `date`" | tee -a $LOG_FILE
--> read a snoop file
snoop -i anz-telnet.snoop
--> write a snoop log (this will count the number of connections, which is pretty neat).
snoop –osnoop.log sarah
--> set default run level. 5 for gui.
/etc/inittab
--> show all exported filesystems
showmount -e crawl
--> shows all configurable variables for tcp interface.
sudo ndd -get /dev/tcp
- ?
eg:
sudo ndd -get /dev/tcp tcp_conn_req_max_q
128
ndd /dev/arp \?
ndd /dev/ip \?
ndd /dev/tcp \?
ndd /dev/udp \?
ndd /dev/icmp \?
--> set sticky bit on group files, only the owner can change the mode.
--> the +l is mandatory file and record locking while a program
--> is accessing that file.
chmod g+s,+l file
--> print duplex landscape 4 qudrant printing
mpage –t –l –4
--> install a patch
installpatch .
--> check to see if a patch has been installed
showrev –p |grep package name
--> unzip, untar in a /tmp directory
zcat 104708-16.tar.gz | ( cd /tmp; sudo tar xvf - )
--> check out revision level on ssa controller
/usr/sbin/ssaadm display controller
--> unzip and untar a file without having to create an intermediate tar file
sudo gzip -dc /tmp/270599/post-EOD.tar.gz |tar xvf -
--> selectively extract from a tar archive
tar xvf /tmp/iona.tar ./iona/.sh_history
--> send a bunch of files to tape
tar cf /tmp/rules.tar ruleb* objects.C *.W
--> examine section 5 of man
man -s 5 signal
--> shows signals and definitions of structures, eg sigaction
/usr/include/sys/signal.h
--> location of the limits file on solaris
/usr/include/limits.h
--> send an attachment via email from command prompt
uuencode file.tar.gz file.tar.gz | mailx –s “backup” root@crawl
--> zero a file
cat /dev/null > isam.log
--> good way to restore from cdrom a binary file
zcat < /cdrom/cdrom0/Solaris_2.6/Product/SUNWcsu/install/reloc.cpio.Z |
cpio –idm usr/lib/fs/ufs/ufsrestore
--> running su as a user then ssh
su - dls-PROD -c "/opt/local/bin/ssh drp-stagger \"cd /tmp; /bin/ls\" "
--> verify a newfs format
sudo newfs –Nv /dev/md/dsk/d96
--> making lost_found. must be 8192 bytes in size.
mkdir ./lost+found;chown root ./lost+found; chgrp root ./lost+found ;chmod 700 ./lost+found’; cd ./lost+found
nofiles=0 ; while [ "$nofiles" -le 650 ] ; do ; /usr/ucb/touch $nofiles ; nofiles=`expr $nofiles + 1` ; done
--> execute lynx
lynx -cfg /usr/lib/lynx.cfg
--> sed search example
sed '/Sep\ 25/!d; /castill/!d' /var/log/syslo
-->should only be used at the EEPROM
boot –r
--> should be used at single user mode
reboot -- -r
--> should be used in multiuser mode
touch /reconfigure
--> performing a remote dump
find MFASYS
|cpio -oc |gzip -c
|ssh brucey -l chaup dd obs=18k of=/dev/rmt/0n
- to extract -
cd /ssa/emphasys/sybase/dump
dd ibs=18k if=
--> boot block located here.
/usr/platform/`uname –i`/lib/fs/ufs
--> getting a server on the network
add hosts entry for IP address
clear configs: ifconfig pe0 unplumb
ifconfig pe0 10.20.2.27 netmask 255.0.0.0 up
route add default 10.20.0.1 1
verify the routing table: netstat –rn
add resolv.conf entry: domain rabobank.com.au nameserver 192.192.192.252
edit /etc/nsswitch.conf change hosts to files, dns
lesson here is to unplumb interface, and let ifconfig setup the routing.
if you specify an ip address and a netmask it will manage
the routing and the broadcasting.
--> find all, files associated with PID 22240
/usr/proc/bin/pfiles 22240
find file based on inode
find –i number
“ncheck –i number
--> good redirection example
./a.out trash
--> synchronize files from one server to another. This is useful for
synchronizing database dump files, binary files, etc. This is definitely a powerful tool.
rsync -avz -e ssh --rsync-path="/usr/local/bin/rsync" `pwd` myhost.com:/home/ebs/public_html
--> Example Awk Script
# run with awk -f/tmp/1.awk /etc/group
BEGIN { FS = ":" }
{ print $1 | "sort" }
{ nlines++ }
END { print nlines }
--> awk example.
awk '/#/ {print "Got a comment"}' /etc/hosts
--> delete every 2nd field in file
awk '{$2= ""; print}' datafile > datafile.new
--> awk average/standard deviation program
x1 += $1
x2 += $1*$1
END {
x1 = x1/NR
x2 = x2/NR
sigma = sqrt(x2 - x1*x1)
if (NR > 1) std_err = sigma/sqrt(NR - 1)
print "Number of points = " NR
print "Mean = " x1
print "Standard Deviation = " sigma
print "Standard Error = " std_err
Unix/Solaris: One-Liners
Source: http://www.kevlo.com/~ebs/unix_commands.txt
Listed here are a bunch of unix commands.
--> change file date stamp
touch –t 199906042020 filename
--> move partitions
ufsdump 0f - /dev/rdsk/c0t0s0s0 | (cd /home; ufsrestore xv -)
--> lay down file system with 1% minfree and inode density
newfs –m1 –i81920 /dev/rdsk/c0t0d0s0
--> check file system
fsck /dev/rdsk/c0t0d0s0
Q: starting sybase
login as sybase, run: ./install/RUN_SYBASE
Q: logging in as sybase sa
isql -U sa
--> dump a partition and pipe to gzip. Watch > 2GB limit
ufsdump 0f - /home | gzip - >/tmp/home.dump.gz
--> rewind offline a tape
mt –f /dev/rmt/0 rewoffl
--> only allow 300MB for user /tmp access
swap - /tmp tmpfs – yes SIZE=300M
--> verbose interactive restore
ufsrestore –ivf /dev/rmt/1
--> remove a printer from a class
lpadmin –p level5-line1 –r level5-line
--> truss a command
truss –-f -–o /tmp/log.txt
--> [DB] feed a script into sybase
isql –Urfe_xfer -Uuser -Ppassword -isqlscript >>blah.txt
--> make a printer class
lpadmin –p level5-line1 –c level5-line
--> remove level2-line2 printer from printer class level2-line
lpadmin -p level2-line2 -r level2-line
--> add level2-line3 to printer class
lpadmin -c level2-line -p level2-line3
--> [DB] how to change your password in isql
sp_password password, password-new
--> move a directory
tar cf - ./games | (cd /tmp; tar xvBpf - )
--> [DB] run a sybase script, and dump to file
$ISQL -i$SCRIPTFILE -U$USER -D$DATABASE -P$PASS_ENC >> $SCRIPTLOGFILE
--> move a directory to another server
tar cf - ./games | rsh brucey cd /tmp\; tar xvBpf -
--> check for SUID SGID files
ncheck -F ufs -s /dev/dsk/c3t0d0s
-- remove core files
find / -name core –exec rm –f {} \; -o –fstype nfs –prune
--> rebuild man pages
catman –w –M man-page-directory or /usr/lib/makewhatis
--> vi command to show special characters
: set list
--> adding an account
useradd -u 120 –g dls -d /apps/dls –s /bin/ksh -c "comment" -m dls
--> create a mysql database
mysqladmin -uroot -ppassword create ebs
--> starting mysql database
/etc/rc.d/init.d/mysql.server start
/usr/local/bin/safe_mysqld
--> Invoke CPAN module install
perl –MCPAN –eshell
--> dump to zip
ufsdump 0f - /filesystem | /opt/local/gzip - > /tmp/dump.gz
--> shutdown mysql databse
/usr/local/bin/mysqladmin shutdown -ppassword
/etc/rc.d/init.d/mysql.server stop
--> test the loading of a module
PERL_DL_DEBUG=255 perl -e 'use CGI;'
--> shows open files
fuser –cu /
--> Writing a Daemon:
1. edit /etc/services
add service and port.
2. edit /etc/inetd.conf
add in: edwardd stream tcp nowait root /bin/sh /bin/sh /home/sextone/bin/SERVER.mine
3. kill –HUP inetd.conf
--> how to mount a file system
mount /dev/dsk/c3t0d0s4 /apps/data/easysoft/DEVT
--> look at sar log
sar –f /var/adm/sa/sa24
--> write file checksums and size
cksum filename
--> show storage array info
ssaadm display /dev/rdsk/c1t5d2s0
--> show all disks on device d
luxadm display d
--> examine for a specific OS finerprint
nmap –sS -p 80 -O -v = examine OS
--> show print jobs
/usr/ucb/lpq –Plevel6
--> Scan for known ports. log it. do OS scan.
nmap –sS -F -o foo.log -v -O www.foo.com//24 =
--> show status of printer
/usr/ucb/lpc status
--> make a swap file:
dd if=/dev/zero of=swapfile bs=1024 count=65535
mkswap ./swapfile
chmod 600 ./swapfile
swapon ./swapfile
--> show open files for process
lsof –p PID
--> show open files for all TCP connections
lsof –iTCP
--> show open files for internet address
lsof -iTCP@10.20.2.9
--> as above
lsof -i @10.20.2.122
--> examine tcp ports
lsof -iTCP@sarah:1-50000
--> show open files for user.
lsof –u username
--> show processes that has the file in use.
lsof /apps/cms/ECMS-Server
--> show open files and retry every 5 seconds
lsof –p process-id –r 5
--> mount a floppy
mount -t vfat /dev/fd0 /mnt/floppy
--> check here for debugging processes and errno.h for errors
/usr/include
/usr/include/sys
/usr/include/sys/errno.h
--> scp a whole directory, preserve mods
sudo scp -prv devel webadmin@203.19.123.140:/home/httpd/cgi-bin
--> take processor 2 and 3 offline.
psradm -f 2 3
--> show processor stats verbose.
psrinfo –v
--> how to skip grant tables in mysql (over ride security)
/usr/local/libexec/mysqld -Sg
--> how to feed in an SQL program
mysql
--> rm all files in directories
find . -type f -exec rm {} \;
--> dump packets to a capture file
sudo snoop –o /tmp/tcp.txt cp
--> backup one liner
tar cvf - /home/ebs | gzip - > ebs.tar.gz
--> Look at selected packets in capture file
sudo snoop -i /tmp/tcp.txt
--> unzip and pipe to tar
gzip -dc
--> watch packets from two servers.
snoop sarah brucey
--> enable ip masquerading
/sbin/ipchains -P forward DENY
/sbin/ipchains -A forward -s 10.100.100.100/8 -j MASQ
-> view su log file
cat /var/adm/sulog
--> establish a default router or gateway.
echo "sagacity.com" > /etc/defaultrouter
echo "10.100.100.100 sagacity.com sagacity" >> /etc/hosts
change /etc/nsswitch.conf so that hosts has files, dns
edit resolv.conf put in
search .
nameserver 203.7.132.98
--> turn off automounter on /export/home.
vi /etc/auto_master, comment out /export/home
--> configuration file for sudoers
/opt/local/etc/sudoers
--> building ssh-1.2.27 on x86Solaris2.6 needed a few things:
/usr/openwin/bin in path
/usr/xpg4/bin in path
declare AR="/usr/xpg4/bin/ar"
declare NM_PATH="/usr/xpg4/bin/nm"
--> snoop network packets and get size and time stamp entries.
snoop -S -ta empa1
--> access perl CPAN
perl -MCPAN -e shell
install DBI
--> search for no password entries and lock all accounts.
for i in `passwd –sa | grep NP | awk ‘{print $1’`
do
echo “locking $i”
passwd –l $i
done
--> delete from a tar
tar --delete -f fs_backup_Sunday.tar home/ebs/tmp
--> Example on backing up files to tape. Must specify non rewinding, else you will over-write the files.
for file in `ls`
do
echo "sending $file to tape..."
echo `date`
tar cvpf /dev/rmt/0n $file
done
--> making/adding a partition.
1. use fdisk to make a parition.
2. mkfs -t ext2 -c /dev/hda11
3. mount -t ext2 /dev/hda11 /opt2
4. update /etc/fstab
--> rebuild the windex file
catman –w –M /usr/share/man
--> execute tar on remote host sarah and send tarball to standard output,
which becomes standard input for tar xvf – and the file gets dumped locally,
in this case on crawl. you have to cd to dir before tar or else you
will include path in tar
ssh maggie "cd $DIRNAME; tar cvf - $BASENAME" | (cd $TPATH; tar xvf - )
--> dump a remote filesystem and send it to local tape drive.
ssh -–x $fw /usr/sbin/ufsdump 0cf - $fs | dd obs=63k of=$TAPE
--> encrypt filename 1 and output to 1.crypt file
crypt < 1 > 1.crypt ; rm 1
--> decrypt filename 1.crypt and stdout to screen
crypt < 1.crypt
--> send a file to tape
tar cvpf /dev/rmt/0 filename
--> quicker way to search and replace in vi
: %s/existing/new/g
--> shows where and which shared library files an application uses.
ldd binary
--> shell script stuff:
# repeat a command 100 times
x=100
while [ $x -gt 0 ]
do
command
x=$(($x-1))
done
--> Something very important to remember about partitions
It is important to note that Cylinder 0 contains the disklabel, which
contains the partition table. A normal filesystem can be placed
starting at Cylinder 0, since it will not touch the disklabel.
If you lay down a raw device, for a database, over Cylinder 0,
then you will completely lose all your partitions. You will then
have to restore the disklabel, and backup from tape if you happen to do this.
--> move a partition
find . |cpio -pdm /apps
--> cron structure
min hour day-of-month month weekday command
--> PatchDiag Tool. Get patches from:
http://sunsolve.sun.com/private-cgi/patchpage.pl
patchdiag.xref is available at: http://sunsolve.sun.com/sunsolve/patchdiag/
/opt/local/bin/patchdiag -x /opt/local/lib/patchdiag.xref > patchdiag.`uname -n`
--> command showing system parameters
/usr/sbin/sysdef
--> Get Ambient Temperature of Server
/usr/platform/SUNW,Ultra-4/sbin/prtdiag -v
--> good ps formatting showing percent cpu first.
ps -edf -o pcpu,pid,user,arg
--> full details on ps
/usr/bin/ps –A -o user,pid,pcpu,pmem,vsz,rss,tty,s,stime,time,args
--> chown the hidden files as well.
find . -print -exec chown -R sextone:staff {} \;
--> The nsradmin command is a command-line based administrative
program for the NetWorker system. Normally nsradmin monitors
and modifies NetWorker resources over the network.
/usr/sbin/nsr/nsradmin
--> Spray a server
-c number of packets
-d delay in microseconds
-l pakcet size in bytes
/usr/sbin/spray -c 1 –d 20 -l 4096 maggie
--> Turn on bold.
bold=`tput smso`
offbold=`tput rmso`
echo "${bold}You must be the \"root\" user to run this script.${offbold}"
--> good way to send a dir to tape
tar cf /dev/rmt/0n directory
--> example of bringing up an interface
ifconfig hme0:1 inet 10.2.25.52 up
--> show all connections
netstat –f inet
--> rpcinfo makes an RPC call to an RPC server and reports
what it finds.
rpcinfo -b 390109 2 | sort -u
--> rewind a tape fast
< /dev/rmt/0
--> show loaded modules
/usr/sbin/modinfo
--> find world readable files and dirs
find / -type d –perm -2 –print
find . -type f –perm -2 -print
--> adding in a boot alias, eg:
boot sarahroot1 –s
nvalias sarahroot1 /sbus@1f,0/sunw,fas@e,8800000/sd@9,0:a
--> clever way to archive
tar cvf - `find . –print` >/tmp/dumpfile.tar
tar xvf -
--> tee to a file
echo "Start Date/Time: `date`" | tee -a $LOG_FILE
--> read a snoop file
snoop -i anz-telnet.snoop
--> write a snoop log (this will count the number of connections, which is pretty neat).
snoop –osnoop.log sarah
--> set default run level. 5 for gui.
/etc/inittab
--> show all exported filesystems
showmount -e crawl
--> shows all configurable variables for tcp interface.
sudo ndd -get /dev/tcp
- ?
eg:
sudo ndd -get /dev/tcp tcp_conn_req_max_q
128
ndd /dev/arp \?
ndd /dev/ip \?
ndd /dev/tcp \?
ndd /dev/udp \?
ndd /dev/icmp \?
--> set sticky bit on group files, only the owner can change the mode.
--> the +l is mandatory file and record locking while a program
--> is accessing that file.
chmod g+s,+l file
--> print duplex landscape 4 qudrant printing
mpage –t –l –4
--> install a patch
installpatch .
--> check to see if a patch has been installed
showrev –p |grep package name
--> unzip, untar in a /tmp directory
zcat 104708-16.tar.gz | ( cd /tmp; sudo tar xvf - )
--> check out revision level on ssa controller
/usr/sbin/ssaadm display controller
--> unzip and untar a file without having to create an intermediate tar file
sudo gzip -dc /tmp/270599/post-EOD.tar.gz |tar xvf -
--> selectively extract from a tar archive
tar xvf /tmp/iona.tar ./iona/.sh_history
--> send a bunch of files to tape
tar cf /tmp/rules.tar ruleb* objects.C *.W
--> examine section 5 of man
man -s 5 signal
--> shows signals and definitions of structures, eg sigaction
/usr/include/sys/signal.h
--> location of the limits file on solaris
/usr/include/limits.h
--> send an attachment via email from command prompt
uuencode file.tar.gz file.tar.gz | mailx –s “backup” root@crawl
--> zero a file
cat /dev/null > isam.log
--> good way to restore from cdrom a binary file
zcat < /cdrom/cdrom0/Solaris_2.6/Product/SUNWcsu/install/reloc.cpio.Z |
cpio –idm usr/lib/fs/ufs/ufsrestore
--> running su as a user then ssh
su - dls-PROD -c "/opt/local/bin/ssh drp-stagger \"cd /tmp; /bin/ls\" "
--> verify a newfs format
sudo newfs –Nv /dev/md/dsk/d96
--> making lost_found. must be 8192 bytes in size.
mkdir ./lost+found;chown root ./lost+found; chgrp root ./lost+found ;chmod 700 ./lost+found’; cd ./lost+found
nofiles=0 ; while [ "$nofiles" -le 650 ] ; do ; /usr/ucb/touch $nofiles ; nofiles=`expr $nofiles + 1` ; done
--> execute lynx
lynx -cfg /usr/lib/lynx.cfg
--> sed search example
sed '/Sep\ 25/!d; /castill/!d' /var/log/syslo
-->should only be used at the EEPROM
boot –r
--> should be used at single user mode
reboot -- -r
--> should be used in multiuser mode
touch /reconfigure
--> performing a remote dump
find MFASYS
|cpio -oc |gzip -c
|ssh brucey -l chaup dd obs=18k of=/dev/rmt/0n
- to extract -
cd /ssa/emphasys/sybase/dump
dd ibs=18k if=|gunzip -c |cpio –idc
--> boot block located here.
/usr/platform/`uname –i`/lib/fs/ufs
--> getting a server on the network
add hosts entry for IP address
clear configs: ifconfig pe0 unplumb
ifconfig pe0 10.20.2.27 netmask 255.0.0.0 up
route add default 10.20.0.1 1
verify the routing table: netstat –rn
add resolv.conf entry: domain rabobank.com.au nameserver 192.192.192.252
edit /etc/nsswitch.conf change hosts to files, dns
lesson here is to unplumb interface, and let ifconfig setup the routing.
if you specify an ip address and a netmask it will manage
the routing and the broadcasting.
--> find all, files associated with PID 22240
/usr/proc/bin/pfiles 22240
find file based on inode
find –i number
“ncheck –i number
--> good redirection example
./a.out trash
--> synchronize files from one server to another. This is useful for
synchronizing database dump files, binary files, etc. This is definitely a powerful tool.
rsync -avz -e ssh --rsync-path="/usr/local/bin/rsync" `pwd` myhost.com:/home/ebs/public_html
--> Example Awk Script
# run with awk -f/tmp/1.awk /etc/group
BEGIN { FS = ":" }
{ print $1 | "sort" }
{ nlines++ }
END { print nlines }
--> awk example.
awk '/#/ {print "Got a comment"}' /etc/hosts
--> delete every 2nd field in file
awk '{$2= ""; print}' datafile > datafile.new
--> awk average/standard deviation program
x1 += $1
x2 += $1*$1
END {
x1 = x1/NR
x2 = x2/NR
sigma = sqrt(x2 - x1*x1)
if (NR > 1) std_err = sigma/sqrt(NR - 1)
print "Number of points = " NR
print "Mean = " x1
print "Standard Deviation = " sigma
print "Standard Error = " std_err
Source: http://www.kevlo.com/~ebs/unix_commands.txt
Listed here are a bunch of unix commands.
--> change file date stamp
touch –t 199906042020 filename
--> move partitions
ufsdump 0f - /dev/rdsk/c0t0s0s0 | (cd /home; ufsrestore xv -)
--> lay down file system with 1% minfree and inode density
newfs –m1 –i81920 /dev/rdsk/c0t0d0s0
--> check file system
fsck /dev/rdsk/c0t0d0s0
Q: starting sybase
login as sybase, run: ./install/RUN_SYBASE
Q: logging in as sybase sa
isql -U sa
--> dump a partition and pipe to gzip. Watch > 2GB limit
ufsdump 0f - /home | gzip - >/tmp/home.dump.gz
--> rewind offline a tape
mt –f /dev/rmt/0 rewoffl
--> only allow 300MB for user /tmp access
swap - /tmp tmpfs – yes SIZE=300M
--> verbose interactive restore
ufsrestore –ivf /dev/rmt/1
--> remove a printer from a class
lpadmin –p level5-line1 –r level5-line
--> truss a command
truss –-f -–o /tmp/log.txt
--> [DB] feed a script into sybase
isql –Urfe_xfer -Uuser -Ppassword -isqlscript >>blah.txt
--> make a printer class
lpadmin –p level5-line1 –c level5-line
--> remove level2-line2 printer from printer class level2-line
lpadmin -p level2-line2 -r level2-line
--> add level2-line3 to printer class
lpadmin -c level2-line -p level2-line3
--> [DB] how to change your password in isql
sp_password password, password-new
--> move a directory
tar cf - ./games | (cd /tmp; tar xvBpf - )
--> [DB] run a sybase script, and dump to file
$ISQL -i$SCRIPTFILE -U$USER -D$DATABASE -P$PASS_ENC >> $SCRIPTLOGFILE
--> move a directory to another server
tar cf - ./games | rsh brucey cd /tmp\; tar xvBpf -
--> check for SUID SGID files
ncheck -F ufs -s /dev/dsk/c3t0d0s
-- remove core files
find / -name core –exec rm –f {} \; -o –fstype nfs –prune
--> rebuild man pages
catman –w –M man-page-directory or /usr/lib/makewhatis
--> vi command to show special characters
: set list
--> adding an account
useradd -u 120 –g dls -d /apps/dls –s /bin/ksh -c "comment" -m dls
--> create a mysql database
mysqladmin -uroot -ppassword create ebs
--> starting mysql database
/etc/rc.d/init.d/mysql.server start
/usr/local/bin/safe_mysqld
--> Invoke CPAN module install
perl –MCPAN –eshell
--> dump to zip
ufsdump 0f - /filesystem | /opt/local/gzip - > /tmp/dump.gz
--> shutdown mysql databse
/usr/local/bin/mysqladmin shutdown -ppassword
/etc/rc.d/init.d/mysql.server stop
--> test the loading of a module
PERL_DL_DEBUG=255 perl -e 'use CGI;'
--> shows open files
fuser –cu /
--> Writing a Daemon:
1. edit /etc/services
add service and port.
2. edit /etc/inetd.conf
add in: edwardd stream tcp nowait root /bin/sh /bin/sh /home/sextone/bin/SERVER.mine
3. kill –HUP inetd.conf
--> how to mount a file system
mount /dev/dsk/c3t0d0s4 /apps/data/easysoft/DEVT
--> look at sar log
sar –f /var/adm/sa/sa24
--> write file checksums and size
cksum filename
--> show storage array info
ssaadm display /dev/rdsk/c1t5d2s0
--> show all disks on device d
luxadm display d
--> examine for a specific OS finerprint
nmap –sS -p 80 -O -v
--> show print jobs
/usr/ucb/lpq –Plevel6
--> Scan for known ports. log it. do OS scan.
nmap –sS -F -o foo.log -v -O www.foo.com//24 =
--> show status of printer
/usr/ucb/lpc status
--> make a swap file:
dd if=/dev/zero of=swapfile bs=1024 count=65535
mkswap ./swapfile
chmod 600 ./swapfile
swapon ./swapfile
--> show open files for process
lsof –p PID
--> show open files for all TCP connections
lsof –iTCP
--> show open files for internet address
lsof -iTCP@10.20.2.9
--> as above
lsof -i @10.20.2.122
--> examine tcp ports
lsof -iTCP@sarah:1-50000
--> show open files for user.
lsof –u username
--> show processes that has the file in use.
lsof /apps/cms/ECMS-Server
--> show open files and retry every 5 seconds
lsof –p process-id –r 5
--> mount a floppy
mount -t vfat /dev/fd0 /mnt/floppy
--> check here for debugging processes and errno.h for errors
/usr/include
/usr/include/sys
/usr/include/sys/errno.h
--> scp a whole directory, preserve mods
sudo scp -prv devel webadmin@203.19.123.140:/home/httpd/cgi-bin
--> take processor 2 and 3 offline.
psradm -f 2 3
--> show processor stats verbose.
psrinfo –v
--> how to skip grant tables in mysql (over ride security)
/usr/local/libexec/mysqld -Sg
--> how to feed in an SQL program
mysql
--> rm all files in directories
find . -type f -exec rm {} \;
--> dump packets to a capture file
sudo snoop –o /tmp/tcp.txt cp
--> backup one liner
tar cvf - /home/ebs | gzip - > ebs.tar.gz
--> Look at selected packets in capture file
sudo snoop -i /tmp/tcp.txt
--> unzip and pipe to tar
gzip -dc
--> watch packets from two servers.
snoop sarah brucey
--> enable ip masquerading
/sbin/ipchains -P forward DENY
/sbin/ipchains -A forward -s 10.100.100.100/8 -j MASQ
-> view su log file
cat /var/adm/sulog
--> establish a default router or gateway.
echo "sagacity.com" > /etc/defaultrouter
echo "10.100.100.100 sagacity.com sagacity" >> /etc/hosts
change /etc/nsswitch.conf so that hosts has files, dns
edit resolv.conf put in
search .
nameserver 203.7.132.98
--> turn off automounter on /export/home.
vi /etc/auto_master, comment out /export/home
--> configuration file for sudoers
/opt/local/etc/sudoers
--> building ssh-1.2.27 on x86Solaris2.6 needed a few things:
/usr/openwin/bin in path
/usr/xpg4/bin in path
declare AR="/usr/xpg4/bin/ar"
declare NM_PATH="/usr/xpg4/bin/nm"
--> snoop network packets and get size and time stamp entries.
snoop -S -ta empa1
--> access perl CPAN
perl -MCPAN -e shell
install DBI
--> search for no password entries and lock all accounts.
for i in `passwd –sa | grep NP | awk ‘{print $1’`
do
echo “locking $i”
passwd –l $i
done
--> delete from a tar
tar --delete -f fs_backup_Sunday.tar home/ebs/tmp
--> Example on backing up files to tape. Must specify non rewinding, else you will over-write the files.
for file in `ls`
do
echo "sending $file to tape..."
echo `date`
tar cvpf /dev/rmt/0n $file
done
--> making/adding a partition.
1. use fdisk to make a parition.
2. mkfs -t ext2 -c /dev/hda11
3. mount -t ext2 /dev/hda11 /opt2
4. update /etc/fstab
--> rebuild the windex file
catman –w –M /usr/share/man
--> execute tar on remote host sarah and send tarball to standard output,
which becomes standard input for tar xvf – and the file gets dumped locally,
in this case on crawl. you have to cd to dir before tar or else you
will include path in tar
ssh maggie "cd $DIRNAME; tar cvf - $BASENAME" | (cd $TPATH; tar xvf - )
--> dump a remote filesystem and send it to local tape drive.
ssh -–x $fw /usr/sbin/ufsdump 0cf - $fs | dd obs=63k of=$TAPE
--> encrypt filename 1 and output to 1.crypt file
crypt < 1 > 1.crypt ; rm 1
--> decrypt filename 1.crypt and stdout to screen
crypt < 1.crypt
--> send a file to tape
tar cvpf /dev/rmt/0 filename
--> quicker way to search and replace in vi
: %s/existing/new/g
--> shows where and which shared library files an application uses.
ldd binary
--> shell script stuff:
# repeat a command 100 times
x=100
while [ $x -gt 0 ]
do
command
x=$(($x-1))
done
--> Something very important to remember about partitions
It is important to note that Cylinder 0 contains the disklabel, which
contains the partition table. A normal filesystem can be placed
starting at Cylinder 0, since it will not touch the disklabel.
If you lay down a raw device, for a database, over Cylinder 0,
then you will completely lose all your partitions. You will then
have to restore the disklabel, and backup from tape if you happen to do this.
--> move a partition
find . |cpio -pdm /apps
--> cron structure
min hour day-of-month month weekday command
--> PatchDiag Tool. Get patches from:
http://sunsolve.sun.com/private-cgi/patchpage.pl
patchdiag.xref is available at: http://sunsolve.sun.com/sunsolve/patchdiag/
/opt/local/bin/patchdiag -x /opt/local/lib/patchdiag.xref > patchdiag.`uname -n`
--> command showing system parameters
/usr/sbin/sysdef
--> Get Ambient Temperature of Server
/usr/platform/SUNW,Ultra-4/sbin/prtdiag -v
--> good ps formatting showing percent cpu first.
ps -edf -o pcpu,pid,user,arg
--> full details on ps
/usr/bin/ps –A -o user,pid,pcpu,pmem,vsz,rss,tty,s,stime,time,args
--> chown the hidden files as well.
find . -print -exec chown -R sextone:staff {} \;
--> The nsradmin command is a command-line based administrative
program for the NetWorker system. Normally nsradmin monitors
and modifies NetWorker resources over the network.
/usr/sbin/nsr/nsradmin
--> Spray a server
-c number of packets
-d delay in microseconds
-l pakcet size in bytes
/usr/sbin/spray -c 1 –d 20 -l 4096 maggie
--> Turn on bold.
bold=`tput smso`
offbold=`tput rmso`
echo "${bold}You must be the \"root\" user to run this script.${offbold}"
--> good way to send a dir to tape
tar cf /dev/rmt/0n directory
--> example of bringing up an interface
ifconfig hme0:1 inet 10.2.25.52 up
--> show all connections
netstat –f inet
--> rpcinfo makes an RPC call to an RPC server and reports
what it finds.
rpcinfo -b 390109 2 | sort -u
--> rewind a tape fast
< /dev/rmt/0
--> show loaded modules
/usr/sbin/modinfo
--> find world readable files and dirs
find / -type d –perm -2 –print
find . -type f –perm -2 -print
--> adding in a boot alias, eg:
boot sarahroot1 –s
nvalias sarahroot1 /sbus@1f,0/sunw,fas@e,8800000/sd@9,0:a
--> clever way to archive
tar cvf - `find . –print` >/tmp/dumpfile.tar
tar xvf -
--> tee to a file
echo "Start Date/Time: `date`" | tee -a $LOG_FILE
--> read a snoop file
snoop -i anz-telnet.snoop
--> write a snoop log (this will count the number of connections, which is pretty neat).
snoop –osnoop.log sarah
--> set default run level. 5 for gui.
/etc/inittab
--> show all exported filesystems
showmount -e crawl
--> shows all configurable variables for tcp interface.
sudo ndd -get /dev/tcp
- ?
eg:
sudo ndd -get /dev/tcp tcp_conn_req_max_q
128
ndd /dev/arp \?
ndd /dev/ip \?
ndd /dev/tcp \?
ndd /dev/udp \?
ndd /dev/icmp \?
--> set sticky bit on group files, only the owner can change the mode.
--> the +l is mandatory file and record locking while a program
--> is accessing that file.
chmod g+s,+l file
--> print duplex landscape 4 qudrant printing
mpage –t –l –4
--> install a patch
installpatch .
--> check to see if a patch has been installed
showrev –p |grep package name
--> unzip, untar in a /tmp directory
zcat 104708-16.tar.gz | ( cd /tmp; sudo tar xvf - )
--> check out revision level on ssa controller
/usr/sbin/ssaadm display controller
--> unzip and untar a file without having to create an intermediate tar file
sudo gzip -dc /tmp/270599/post-EOD.tar.gz |tar xvf -
--> selectively extract from a tar archive
tar xvf /tmp/iona.tar ./iona/.sh_history
--> send a bunch of files to tape
tar cf /tmp/rules.tar ruleb* objects.C *.W
--> examine section 5 of man
man -s 5 signal
--> shows signals and definitions of structures, eg sigaction
/usr/include/sys/signal.h
--> location of the limits file on solaris
/usr/include/limits.h
--> send an attachment via email from command prompt
uuencode file.tar.gz file.tar.gz | mailx –s “backup” root@crawl
--> zero a file
cat /dev/null > isam.log
--> good way to restore from cdrom a binary file
zcat < /cdrom/cdrom0/Solaris_2.6/Product/SUNWcsu/install/reloc.cpio.Z |
cpio –idm usr/lib/fs/ufs/ufsrestore
--> running su as a user then ssh
su - dls-PROD -c "/opt/local/bin/ssh drp-stagger \"cd /tmp; /bin/ls\" "
--> verify a newfs format
sudo newfs –Nv /dev/md/dsk/d96
--> making lost_found. must be 8192 bytes in size.
mkdir ./lost+found;chown root ./lost+found; chgrp root ./lost+found ;chmod 700 ./lost+found’; cd ./lost+found
nofiles=0 ; while [ "$nofiles" -le 650 ] ; do ; /usr/ucb/touch $nofiles ; nofiles=`expr $nofiles + 1` ; done
--> execute lynx
lynx -cfg /usr/lib/lynx.cfg
--> sed search example
sed '/Sep\ 25/!d; /castill/!d' /var/log/syslo
-->should only be used at the EEPROM
boot –r
--> should be used at single user mode
reboot -- -r
--> should be used in multiuser mode
touch /reconfigure
--> performing a remote dump
find MFASYS
|cpio -oc |gzip -c
|ssh brucey -l chaup dd obs=18k of=/dev/rmt/0n
- to extract -
cd /ssa/emphasys/sybase/dump
dd ibs=18k if=
--> boot block located here.
/usr/platform/`uname –i`/lib/fs/ufs
--> getting a server on the network
add hosts entry for IP address
clear configs: ifconfig pe0 unplumb
ifconfig pe0 10.20.2.27 netmask 255.0.0.0 up
route add default 10.20.0.1 1
verify the routing table: netstat –rn
add resolv.conf entry: domain rabobank.com.au nameserver 192.192.192.252
edit /etc/nsswitch.conf change hosts to files, dns
lesson here is to unplumb interface, and let ifconfig setup the routing.
if you specify an ip address and a netmask it will manage
the routing and the broadcasting.
--> find all, files associated with PID 22240
/usr/proc/bin/pfiles 22240
find file based on inode
find –i number
“ncheck –i number
--> good redirection example
./a.out trash
--> synchronize files from one server to another. This is useful for
synchronizing database dump files, binary files, etc. This is definitely a powerful tool.
rsync -avz -e ssh --rsync-path="/usr/local/bin/rsync" `pwd` myhost.com:/home/ebs/public_html
--> Example Awk Script
# run with awk -f/tmp/1.awk /etc/group
BEGIN { FS = ":" }
{ print $1 | "sort" }
{ nlines++ }
END { print nlines }
--> awk example.
awk '/#/ {print "Got a comment"}' /etc/hosts
--> delete every 2nd field in file
awk '{$2= ""; print}' datafile > datafile.new
--> awk average/standard deviation program
x1 += $1
x2 += $1*$1
END {
x1 = x1/NR
x2 = x2/NR
sigma = sqrt(x2 - x1*x1)
if (NR > 1) std_err = sigma/sqrt(NR - 1)
print "Number of points = " NR
print "Mean = " x1
print "Standard Deviation = " sigma
print "Standard Error = " std_err
A Handful of Useful awk One-Liners
A Handful of Useful awk One-Liners
Although awk can be used to write programs of some complexity,
many useful programs are not complicated. Here is a collection
of short programs that you might find handy and/or
instructive:
1.Print the total numnber of input lines:
END { print NR }
2.Print the tenth input line:
NR == 10
3.Print the last field of every input line:
{ print $NF }
4.Print the last field of the last input line:
{ field = $NF}
END { print field }
5.Print every input line with more than 4 fields:
NF > 4
6.Print every input line in which the last field is more than 4:
$NF > 4
7.Print the total number of fields in all input lines:
{ nf = nf + NF }
END { print nf }
8.Print the total number of lines that contain Beth:
/Beth/ { nlines = nlines + 1 }
END { print nlines }
9.Print the largest first fields and the line that contains it
( assumes some $1 is positive):
$1 > max { max = $1 ; maxlines = $0 }
END { print max, maxline)
10.Print every line that has at least one field:
NF > 0
11.Pritn every line longer than 80 characters:
length($0) > 80
12.Print the numer of fields in every line, followed by the line itself:
{ print NF, $0 }
13.Print the first two fields, in opposite order, of every line:
{ print $2, $1 }
14.Exchange the first two fields of every line and then print the line:
{ temp = $1 ; $1 = $2 ; $2 = temp ; print }
15.Print every line witg rge first field replaced by the line number:
{ $1 = NR ; print }
16.Print every line after erasing the second field:
{ $2 = ""; print }
17.Print in reverse order the fields of every line:
{ for (i=NF ; i>0 ; i=i-1) printf( "%s ", $1)
printf("\n")
}
18.Print the sums of the fields of every line:
{ sum = 0
for ( i=1 ; i<=NF ; i=i+1) sum = sum + $i
print sum
}
19.Ad up all fields in all lines and print the sum:
{ for ( i=1 ; i<=NF ; i=i+1 ) sum = sum + $i}
END { print sum }
20.Print every line after replacing each field by its absolute value:
{ for (i=1 ; i<=NF ; i=i+1) if ($i<0) $i=-$i
print
}
Source: The AWK Programming Language
Although awk can be used to write programs of some complexity,
many useful programs are not complicated. Here is a collection
of short programs that you might find handy and/or
instructive:
1.Print the total numnber of input lines:
END { print NR }
2.Print the tenth input line:
NR == 10
3.Print the last field of every input line:
{ print $NF }
4.Print the last field of the last input line:
{ field = $NF}
END { print field }
5.Print every input line with more than 4 fields:
NF > 4
6.Print every input line in which the last field is more than 4:
$NF > 4
7.Print the total number of fields in all input lines:
{ nf = nf + NF }
END { print nf }
8.Print the total number of lines that contain Beth:
/Beth/ { nlines = nlines + 1 }
END { print nlines }
9.Print the largest first fields and the line that contains it
( assumes some $1 is positive):
$1 > max { max = $1 ; maxlines = $0 }
END { print max, maxline)
10.Print every line that has at least one field:
NF > 0
11.Pritn every line longer than 80 characters:
length($0) > 80
12.Print the numer of fields in every line, followed by the line itself:
{ print NF, $0 }
13.Print the first two fields, in opposite order, of every line:
{ print $2, $1 }
14.Exchange the first two fields of every line and then print the line:
{ temp = $1 ; $1 = $2 ; $2 = temp ; print }
15.Print every line witg rge first field replaced by the line number:
{ $1 = NR ; print }
16.Print every line after erasing the second field:
{ $2 = ""; print }
17.Print in reverse order the fields of every line:
{ for (i=NF ; i>0 ; i=i-1) printf( "%s ", $1)
printf("\n")
}
18.Print the sums of the fields of every line:
{ sum = 0
for ( i=1 ; i<=NF ; i=i+1) sum = sum + $i
print sum
}
19.Ad up all fields in all lines and print the sum:
{ for ( i=1 ; i<=NF ; i=i+1 ) sum = sum + $i}
END { print sum }
20.Print every line after replacing each field by its absolute value:
{ for (i=1 ; i<=NF ; i=i+1) if ($i<0) $i=-$i
}
Source: The AWK Programming Language
A handful of useful "one-liners"
A handful of useful "one-liners"
1. END{print NR}
2. NR==10
3. {print $NF}
4. {field=$NF}
END{print field}
5. NF>4
6. $NF>4
7. {nf=nf+NF}
END {print nf}
8. /Beth/ {nl=nl+1} # �� line�� ���� Beth�� �����
END {print ln}
9. $1>max {max=$1; maxl=$0}
END{print max, maxl}
10. NF>0
11. length($0)>80
12. {print NF, $0}
13. {temp=$1; $1=$2;$2=temp;print}
15. {$1=NR; print}
16. {$2=""; print}
17. {for(i=NF;i>0;i=i-1) printf("%s ",$i)
printf("\n") }
18. {sum=0
for(i=1;i<=NF;i=i+1) sum=sum+$i
print sum }
19. {for(i=1;i<=NF;i=i+1) sum=sum+$i }
END{ print sum}
20. {for(i=1;i<=NF;i=i+1)
if($i<0) $i=-$i
print }
1. END{print NR}
2. NR==10
3. {print $NF}
4. {field=$NF}
END{print field}
5. NF>4
6. $NF>4
7. {nf=nf+NF}
END {print nf}
8. /Beth/ {nl=nl+1} # �� line�� ���� Beth�� �����
END {print ln}
9. $1>max {max=$1; maxl=$0}
END{print max, maxl}
10. NF>0
11. length($0)>80
12. {print NF, $0}
13. {temp=$1; $1=$2;$2=temp;print}
15. {$1=NR; print}
16. {$2=""; print}
17. {for(i=NF;i>0;i=i-1) printf("%s ",$i)
printf("\n") }
18. {sum=0
for(i=1;i<=NF;i=i+1) sum=sum+$i
print sum }
19. {for(i=1;i<=NF;i=i+1) sum=sum+$i }
END{ print sum}
20. {for(i=1;i<=NF;i=i+1)
if($i<0) $i=-$i
print }
one lines intro good one
The TAPL book contains 20 examples of One-liners (A Handful of Useful awk One-Liners) at the end of the book's first chapter.
Here are the very first of them:
1. Print the total number of input lines:
END { print NR }
2. Print the tenth input line:
NR == 10
3. Print the last field of every input line:
{ print $NF }
Many one-liners are practical. For example, the following perl one-liner will reverse all the bytes in a file:
perl -0777e 'print scalar reverse <>' filename
One-liners are also used to show off the differential expressive power of programming languages. Frequently, one-liners are used to demonstrate programming ability. Contests are often held to see who can create the most exceptional one-liner.
The following example is a C program (a winning entry in the "Best one-liner" category of the IOCCC.)
main(int c,char**v){return!m(v[1],v[2]);}m(char*s,char*t){return*t-42?*s?63==*t|*s==*t&&m(s+1,t+1):!*t:m(s,t+1)||*s&&m(s+1,t);}
This one-liner program is a glob pattern matcher. It understands the glob characters `*' meaning `zero or more characters' and `?' meaning exactly one character, just like most Unix shells.
Run it with two args, the string and the glob pattern. The exit status is 0 (shell true) when the pattern matches, 1 otherwise. The glob pattern must match the whole string, so you may want to use * at the beginning and end of the pattern if you are looking for something in the middle. Examples:
$ prog foo 'f??'; echo $?
$ prog 'best short program' '??st*o**p?*'; echo $?
[edit] One-liner in functional programming
The following Haskell program is a one-liner, except for the two import statements. It sorts its input lines asciibetically.
import IO
import List
main = (sequence . map putStrLn . sort . lines) =<< getContents
[edit] One-liner in Java
public class OneLine { public static void main(String[] args) { System.out.println("One-liner."); } }
[edit] External links
* Perl Oneliners, showing off
* Perl One Liners, practical examples
* More Perl One Liners
* International Obfuscated C Code Contest, IOCCC
Here are the very first of them:
1. Print the total number of input lines:
END { print NR }
2. Print the tenth input line:
NR == 10
3. Print the last field of every input line:
{ print $NF }
Many one-liners are practical. For example, the following perl one-liner will reverse all the bytes in a file:
perl -0777e 'print scalar reverse <>' filename
One-liners are also used to show off the differential expressive power of programming languages. Frequently, one-liners are used to demonstrate programming ability. Contests are often held to see who can create the most exceptional one-liner.
The following example is a C program (a winning entry in the "Best one-liner" category of the IOCCC.)
main(int c,char**v){return!m(v[1],v[2]);}m(char*s,char*t){return*t-42?*s?63==*t|*s==*t&&m(s+1,t+1):!*t:m(s,t+1)||*s&&m(s+1,t);}
This one-liner program is a glob pattern matcher. It understands the glob characters `*' meaning `zero or more characters' and `?' meaning exactly one character, just like most Unix shells.
Run it with two args, the string and the glob pattern. The exit status is 0 (shell true) when the pattern matches, 1 otherwise. The glob pattern must match the whole string, so you may want to use * at the beginning and end of the pattern if you are looking for something in the middle. Examples:
$ prog foo 'f??'; echo $?
$ prog 'best short program' '??st*o**p?*'; echo $?
[edit] One-liner in functional programming
The following Haskell program is a one-liner, except for the two import statements. It sorts its input lines asciibetically.
import IO
import List
main = (sequence . map putStrLn . sort . lines) =<< getContents
[edit] One-liner in Java
public class OneLine { public static void main(String[] args) { System.out.println("One-liner."); } }
[edit] External links
* Perl Oneliners, showing off
* Perl One Liners, practical examples
* More Perl One Liners
* International Obfuscated C Code Contest, IOCCC
awk one liners
FILE SPACING:
# double space a file
awk ‘1;{print “”}’
awk ‘BEGIN{ORS=”\n\n”};1′
# double space a file which already has blank lines in it. Output file
# should contain no more than one blank line between lines of text.
# NOTE: On Unix systems, DOS lines which have only CRLF (\r\n) are
# often treated as non-blank, and thus ‘NF’ alone will return TRUE.
awk ‘NF{print $0 “\n”}’
# triple space a file
awk ‘1;{print “\n”}’
NUMBERING AND CALCULATIONS:
# precede each line by its line number FOR THAT FILE (left alignment).
# Using a tab (\t) instead of space will preserve margins.
awk ‘{print FNR “\t” $0}’ files*
# precede each line by its line number FOR ALL FILES TOGETHER, with tab.
awk ‘{print NR “\t” $0}’ files*
# number each line of a file (number on left, right-aligned)
# Double the percent signs if typing from the DOS command prompt.
awk ‘{printf(”%5d : %s\n”, NR,$0)}’
# number each line of file, but only print numbers if line is not blank
# Remember caveats about Unix treatment of \r (mentioned above)
awk ‘NF{$0=++a ” :” $0};{print}’
awk ‘{print (NF? ++a ” :” :”") $0}’
# count lines (emulates “wc -l”)
awk ‘END{print NR}’
# print the sums of the fields of every line
awk ‘{s=0; for (i=1; i max {max=$1; maxline=$0}; END{ print max, maxline}’
# print the number of fields in each line, followed by the line
awk ‘{ print NF “:” $0 } ‘
# print the last field of each line
awk ‘{ print $NF }’
# print the last field of the last line
awk ‘{ field = $NF }; END{ print field }’
# print every line with more than 4 fields
awk ‘NF > 4′
# print every line where the value of the last field is > 4
awk ‘$NF > 4′
TEXT CONVERSION AND SUBSTITUTION:
# IN UNIX ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format
awk ‘{sub(/\r$/,”");print}’ # assumes EACH line ends with Ctrl-M
# IN UNIX ENVIRONMENT: convert Unix newlines (LF) to DOS format
awk ‘{sub(/$/,”\r”);print}
# IN DOS ENVIRONMENT: convert Unix newlines (LF) to DOS format
awk 1
# IN DOS ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format
# Cannot be done with DOS versions of awk, other than gawk:
gawk -v BINMODE=”w” ‘1′ infile >outfile
# Use “tr” instead.
tr -d \r outfile # GNU tr version 1.22 or higher
# delete leading whitespace (spaces, tabs) from front of each line
# aligns all text flush left
awk ‘{sub(/^[ \t]+/, “”); print}’
# delete trailing whitespace (spaces, tabs) from end of each line
awk ‘{sub(/[ \t]+$/, “”);print}’
# delete BOTH leading and trailing whitespace from each line
awk ‘{gsub(/^[ \t]+|[ \t]+$/,”");print}’
awk ‘{$1=$1;print}’ # also removes extra space between fields
# insert 5 blank spaces at beginning of each line (make page offset)
awk ‘{sub(/^/, ” “);print}’
# align all text flush right on a 79-column width
awk ‘{printf “%79s\n”, $0}’ file*
# center all text on a 79-character width
awk ‘{l=length();s=int((79-l)/2); printf “%”(s+l)”s\n”,$0}’ file*
# substitute (find and replace) “foo” with “bar” on each line
awk ‘{sub(/foo/,”bar”);print}’ # replaces only 1st instance
gawk ‘{$0=gensub(/foo/,”bar”,4);print}’ # replaces only 4th instance
awk ‘{gsub(/foo/,”bar”);print}’ # replaces ALL instances in a line
# substitute “foo” with “bar” ONLY for lines which contain “baz”
awk ‘/baz/{gsub(/foo/, “bar”)};{print}’
# substitute “foo” with “bar” EXCEPT for lines which contain “baz”
awk ‘!/baz/{gsub(/foo/, “bar”)};{print}’
# change “scarlet” or “ruby” or “puce” to “red”
awk ‘{gsub(/scarlet|ruby|puce/, “red”); print}’
# reverse order of lines (emulates “tac”)
awk ‘{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j–] }’ file*
# if a line ends with a backslash, append the next line to it
# (fails if there are multiple lines ending with backslash…)
awk ‘/\\$/ {sub(/\\$/,”"); getline t; print $0 t; next}; 1′ file*
# print and sort the login names of all users
awk -F “:” ‘{ print $1 | “sort” }’ /etc/passwd
# print the first 2 fields, in opposite order, of every line
awk ‘{print $2, $1}’ file
# switch the first 2 fields of every line
awk ‘{temp = $1; $1 = $2; $2 = temp}’ file
# print every line, deleting the second field of that line
awk ‘{ $2 = “”; print }’
# print in reverse order the fields of every line
awk ‘{for (i=NF; i>0; i–) printf(”%s “,i);printf (”\n”)}’ file
# remove duplicate, consecutive lines (emulates “uniq”)
awk ‘a !~ $0; {a=$0}’
# remove duplicate, nonconsecutive lines
awk ‘! a[$0]++’ # most concise script
awk ‘!($0 in a) {a[$0];print}’ # most efficient script
# concatenate every 5 lines of input, using a comma separator
# between fields
awk ‘ORS=%NR%5?”,”:”\n”‘ file
SELECTIVE PRINTING OF CERTAIN LINES:
# print first 10 lines of file (emulates behavior of “head”)
awk ‘NR 1{exit};1′
# print the last 2 lines of a file (emulates “tail -2″)
awk ‘{y=x “\n” $0; x=$0};END{print y}’
# print the last line of a file (emulates “tail -1″)
awk ‘END{print}’
# print only lines which match regular expression (emulates “grep”)
awk ‘/regex/’
# print only lines which do NOT match regex (emulates “grep -v”)
awk ‘!/regex/’
# print the line immediately before a regex, but not the line
# containing the regex
awk ‘/regex/{print x};{x=$0}’
awk ‘/regex/{print (x==”" ? “match on line 1″ : x)};{x=$0}’
# print the line immediately after a regex, but not the line
# containing the regex
awk ‘/regex/{getline;print}’
# grep for AAA and BBB and CCC (in any order)
awk ‘/AAA/; /BBB/; /CCC/’
# grep for AAA and BBB and CCC (in that order)
awk ‘/AAA.*BBB.*CCC/’
# print only lines of 65 characters or longer
awk ‘length > 64′
# print only lines of less than 65 characters
awk ‘length
# double space a file
awk ‘1;{print “”}’
awk ‘BEGIN{ORS=”\n\n”};1′
# double space a file which already has blank lines in it. Output file
# should contain no more than one blank line between lines of text.
# NOTE: On Unix systems, DOS lines which have only CRLF (\r\n) are
# often treated as non-blank, and thus ‘NF’ alone will return TRUE.
awk ‘NF{print $0 “\n”}’
# triple space a file
awk ‘1;{print “\n”}’
NUMBERING AND CALCULATIONS:
# precede each line by its line number FOR THAT FILE (left alignment).
# Using a tab (\t) instead of space will preserve margins.
awk ‘{print FNR “\t” $0}’ files*
# precede each line by its line number FOR ALL FILES TOGETHER, with tab.
awk ‘{print NR “\t” $0}’ files*
# number each line of a file (number on left, right-aligned)
# Double the percent signs if typing from the DOS command prompt.
awk ‘{printf(”%5d : %s\n”, NR,$0)}’
# number each line of file, but only print numbers if line is not blank
# Remember caveats about Unix treatment of \r (mentioned above)
awk ‘NF{$0=++a ” :” $0};{print}’
awk ‘{print (NF? ++a ” :” :”") $0}’
# count lines (emulates “wc -l”)
awk ‘END{print NR}’
# print the sums of the fields of every line
awk ‘{s=0; for (i=1; i max {max=$1; maxline=$0}; END{ print max, maxline}’
# print the number of fields in each line, followed by the line
awk ‘{ print NF “:” $0 } ‘
# print the last field of each line
awk ‘{ print $NF }’
# print the last field of the last line
awk ‘{ field = $NF }; END{ print field }’
# print every line with more than 4 fields
awk ‘NF > 4′
# print every line where the value of the last field is > 4
awk ‘$NF > 4′
TEXT CONVERSION AND SUBSTITUTION:
# IN UNIX ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format
awk ‘{sub(/\r$/,”");print}’ # assumes EACH line ends with Ctrl-M
# IN UNIX ENVIRONMENT: convert Unix newlines (LF) to DOS format
awk ‘{sub(/$/,”\r”);print}
# IN DOS ENVIRONMENT: convert Unix newlines (LF) to DOS format
awk 1
# IN DOS ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format
# Cannot be done with DOS versions of awk, other than gawk:
gawk -v BINMODE=”w” ‘1′ infile >outfile
# Use “tr” instead.
tr -d \r outfile # GNU tr version 1.22 or higher
# delete leading whitespace (spaces, tabs) from front of each line
# aligns all text flush left
awk ‘{sub(/^[ \t]+/, “”); print}’
# delete trailing whitespace (spaces, tabs) from end of each line
awk ‘{sub(/[ \t]+$/, “”);print}’
# delete BOTH leading and trailing whitespace from each line
awk ‘{gsub(/^[ \t]+|[ \t]+$/,”");print}’
awk ‘{$1=$1;print}’ # also removes extra space between fields
# insert 5 blank spaces at beginning of each line (make page offset)
awk ‘{sub(/^/, ” “);print}’
# align all text flush right on a 79-column width
awk ‘{printf “%79s\n”, $0}’ file*
# center all text on a 79-character width
awk ‘{l=length();s=int((79-l)/2); printf “%”(s+l)”s\n”,$0}’ file*
# substitute (find and replace) “foo” with “bar” on each line
awk ‘{sub(/foo/,”bar”);print}’ # replaces only 1st instance
gawk ‘{$0=gensub(/foo/,”bar”,4);print}’ # replaces only 4th instance
awk ‘{gsub(/foo/,”bar”);print}’ # replaces ALL instances in a line
# substitute “foo” with “bar” ONLY for lines which contain “baz”
awk ‘/baz/{gsub(/foo/, “bar”)};{print}’
# substitute “foo” with “bar” EXCEPT for lines which contain “baz”
awk ‘!/baz/{gsub(/foo/, “bar”)};{print}’
# change “scarlet” or “ruby” or “puce” to “red”
awk ‘{gsub(/scarlet|ruby|puce/, “red”); print}’
# reverse order of lines (emulates “tac”)
awk ‘{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j–] }’ file*
# if a line ends with a backslash, append the next line to it
# (fails if there are multiple lines ending with backslash…)
awk ‘/\\$/ {sub(/\\$/,”"); getline t; print $0 t; next}; 1′ file*
# print and sort the login names of all users
awk -F “:” ‘{ print $1 | “sort” }’ /etc/passwd
# print the first 2 fields, in opposite order, of every line
awk ‘{print $2, $1}’ file
# switch the first 2 fields of every line
awk ‘{temp = $1; $1 = $2; $2 = temp}’ file
# print every line, deleting the second field of that line
awk ‘{ $2 = “”; print }’
# print in reverse order the fields of every line
awk ‘{for (i=NF; i>0; i–) printf(”%s “,i);printf (”\n”)}’ file
# remove duplicate, consecutive lines (emulates “uniq”)
awk ‘a !~ $0; {a=$0}’
# remove duplicate, nonconsecutive lines
awk ‘! a[$0]++’ # most concise script
awk ‘!($0 in a) {a[$0];print}’ # most efficient script
# concatenate every 5 lines of input, using a comma separator
# between fields
awk ‘ORS=%NR%5?”,”:”\n”‘ file
SELECTIVE PRINTING OF CERTAIN LINES:
# print first 10 lines of file (emulates behavior of “head”)
awk ‘NR 1{exit};1′
# print the last 2 lines of a file (emulates “tail -2″)
awk ‘{y=x “\n” $0; x=$0};END{print y}’
# print the last line of a file (emulates “tail -1″)
awk ‘END{print}’
# print only lines which match regular expression (emulates “grep”)
awk ‘/regex/’
# print only lines which do NOT match regex (emulates “grep -v”)
awk ‘!/regex/’
# print the line immediately before a regex, but not the line
# containing the regex
awk ‘/regex/{print x};{x=$0}’
awk ‘/regex/{print (x==”" ? “match on line 1″ : x)};{x=$0}’
# print the line immediately after a regex, but not the line
# containing the regex
awk ‘/regex/{getline;print}’
# grep for AAA and BBB and CCC (in any order)
awk ‘/AAA/; /BBB/; /CCC/’
# grep for AAA and BBB and CCC (in that order)
awk ‘/AAA.*BBB.*CCC/’
# print only lines of 65 characters or longer
awk ‘length > 64′
# print only lines of less than 65 characters
awk ‘length
Subscribe to:
Posts (Atom)