Today I have faced a problem that in its root is very basic but i have never ever thought of it. It was kind of hard to find the solution on the Internet on the first page of Google so I hope this article will be useful for some of you. And of course I hope it gets to first page on your search ;)
Ok lets tell you what I wanted to do. Every one one of us(here by 'us' i mean Linux/*nix users) knows knows about the console history. Just cause i do not know how to explain something like that I would rather show you.
So what i needed is to take some commands I had in the console history and reuse them as part of a bash script I was writing. So what I needed is to exclude the first column of the history output, and more precisely the line numbers.
It turned out it was not that simple as i expected to exclude a juts column from the print. I was hoping it will be something like
awk '{print * ^$1}' basically but. I know, I know it makes not sense what i was thinking but, hey, I will give you the solution just in a bit ;).
The above command works just great for me but I'm sure it may have flows. For example If you have in our history commands some special characters as
^,@,$ and so on. Feel free to modify the command and you may post your comments in our forum
Onlinehowto Forum
A side note is the NF parameter in the command. Here is explanation from Linux man pages.
FILENAME
A pathname of the current input file. Inside a BEGIN action the value is undefined. Inside an END action the value shall be the name of the
last input file processed.
FNR The ordinal number of the current record in the current file. Inside a BEGIN action the value shall be zero. Inside an END action the value
shall be the number of the last record processed in the last file processed.
FS Input field separator regular expression; a <space> by default.
NF The number of fields in the current record. Inside a BEGIN action, the use of NF is undefined unless a getline function without a var argu‐
ment is executed previously. Inside an END action, NF shall retain the value it had for the last record read, unless a subsequent, redi‐
rected, getline function without a var argument is performed prior to entering the END action.
NR The ordinal number of the current record from the start of input. Inside a BEGIN action the value shall be zero. Inside an END action the
value shall be the number of the last record processed.
OFMT The printf format for converting numbers to strings in output statements (see Output Statements ); "%.6g" by default. The result of the
conversion is unspecified if the value of OFMT is not a floating-point format specification.
OFS The print statement output field separation; <space> by default.
ORS The print statement output record separator; a <newline> by default.
RLENGTH
The length of the string matched by the match function.
RS The first character of the string value of RS shall be the input record separator; a <newline> by default. If RS contains more than one
character, the results are unspecified. If RS is null, then records are separated by sequences consisting of a <newline> plus one or more
blank lines, leading or trailing blank lines shall not result in empty records at the beginning or end of the input, and a <newline> shall
always be a field separator, no matter what the value of FS is.
posted on 2010-Mar-02 | 12:35:44 AM
posted on 2010-Mar-05 | 12:31:46 AM