Link to home
Start Free TrialLog in
Avatar of AXISHK
AXISHK

asked on

bash script

#1 What does the below line do ?
echo `date`" "`cat $LOG` >> $LOG.history

#2 What does gawk '$7 ~ /&meg=/ {print}' do ? What's the different between awk & gawk ?
For $7, does it extract column 7, delimited by space
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

The first command appends the current date followed by a space plus the content of a file whose name is stored in the variable "$LOG" to a file with the same name as in "$LOG" with ".history" appended.

The awk snippet checks whether the 7th space- or tab-delimited field of the current input line contains "&meg=" and if so it prints the whole line containing that 7th field.

There are several implementations of "awk". "gawk" is "GNU awk". On LInux systems it's in most cases the same as "awk".
Avatar of AXISHK
AXISHK

ASKER

Tks.

For the first command, does it mean a date and a space will be append at the begining of each line in$LOG file ?

For second command, the whole line will be extracted, rather than the 7th column ?
1) Taking the command as you posted it the date and the space appears only once at the beginning of the new content which is appended to $LOG.history. `cat $LOG` is not enclosed in quotes, however, so all the content of the file appears in one single line,  so that at the end each line is prefixed with the date.

2) Yes, the whole line will be printed out if its 7th field matches your criterion.
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AXISHK

ASKER

Great Tks.

echo `date`" ""`cat $LOG`" >> $LOG.history

What make it difference when adding " " between `cat $LOG`.

Tks again.
"echo" turns several consecutive spaces into one, and, important in this case, removes the line feeds between the strings to be echoed.

To avoid this enclose the data to be echoed in double quotes.
The quotes turn consecutive strings (possibly separated by line feeds) into one single string which is left untouched by "echo".

Again, the example:

$LOG contains

1
2
3

i.e. 3 strings separated by line feeds, and

`cat $LOG`

represents these three strings and their line feeds:

1
2
3

But "echo" in echo `cat $LOG` removes the line feeds, so the result of

echo `cat $LOG`

is

1 2 3

"`cat $LOG`"  (note the quotes!) on the other hand represents just one single string with embedded line feeds.

"echo" does not touch content between quotes, so the result of

echo "`cat $LOG`"

is

1
2
3

i. e. the original content (three strings separated by line feeds).

You might want to ask why we don't use single quotes (' ') ?
Now, that's because the shell itself does not touch content between single quotes,
so this $LOG variable will not be expanded to the corresponding value but will be treated literally as '$LOG' which is obviously not desired here.
Avatar of AXISHK

ASKER

Tks