How to alias this awk?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to alias this awk?
# 1  
Old 02-09-2016
How to alias this awk?

Hi,

I am running the following df command to display df in GB. I am not able to use df -h on this server as that option is not available.

Code:
 
 df -k /tmp | awk 'NR==1 {CNV=1024*1024;GBYTES="GBytes";FMT="%-35s %-10s %-10s %-10s %10s %-s\n"; printf FMT, $1, GBYTES, $3, $4, $5, $6; next} {printf FMT,$1,$2/CNV,$3/CNV,$4/CNV,$5,$6}'

Sample output below:

Code:
 
 $: df -k /tmp | awk 'NR==1 {CNV=1024*1024;GBYTES="GBytes";FMT="%-35s %-10s %-10s %-10s %10s %-s\n"; printf FMT, $1, GBYTES, $3, $4, $5, $6; next} {printf FMT,$1,$2/CNV,$3/CNV,$4/CNV,$5,$6}'
  
 Filesystem                          GBytes     used       avail        capacity Mounted
swap                                33.2402    0.0285187  33.2117            1% /tmp

I tried to alias this as below:

Code:
alias x='df -k $1 | awk \'NR==1 {CNV=1024*1024;GBYTES="GBytes";FMT="%-35s %-10s %-10s %-10s %10s %-s\n"; printf FMT, $1, GBYTES, $3, $4, $5, $6; next} {printf FMT,$1,$2/CNV,$3/CNV,$4/CNV,$5,$6}\''

But it is 'complaining' that there is a missing single quote. After running the alias above, it presents me a > instead.

Currently, the workaround that I am doing is to create dummy script named /tmp/df_g.ksh

Code:
 
 #!/bin/ksh
 df -k $1 | awk 'NR==1 {CNV=1024*1024;GBYTES="GBytes";FMT="%-35s %-10s %-10s %-10s %10s %-s\n"; printf FMT, $1, GBYTES, $3, $4, $5, $6; next} {printf FMT,$1,$2/CNV,$3/CNV,$4/CNV,$5,$6}'
 exit 0

And then I included the following in my .profile.

Code:
 
 alias df_g='/tmp/df_g.ksh'

Now, I can run it as df_g /tmp.

Is there any way that I can alias the awk without using the dummy script?
# 2  
Old 02-09-2016
Hello newbie_01,

You could add the variable directly into the .profile as follows, I tried in bash and it worked successfully for me.
Off course use a new session for server after adding this to .profile to make alias effective.
Code:
X=`df -k /tmp | awk 'NR==1 {CNV=1024*1024;GBYTES="GBytes";FMT="%-35s %-10s %-10s %-10s %10s %-s\n"; printf FMT, $1, GBYTES, $3, $4, $5, $6; next} {printf FMT,$1,$2/CNV,$3/CNV,$4/CNV,$5,$6}'`
export X;

In a new session for server as follows I got value.
Code:
echo $X
Filesystem GBytes Used Available Use% Mounted tmpfs 0.5 0.000367655 0.493422 1% /tmp

EDIT: You could use $ also for the command to put it into the alias name as follows.
Code:
X=$(df -k /tmp | awk 'NR==1 {CNV=1024*1024;GBYTES="GBytes";FMT="%-35s %-10s %-10s %-10s %10s %-s\n"; printf FMT, $1, GBYTES, $3, $4, $5, $6; next} {printf FMT,$1,$2/CNV,$3/CNV,$4/CNV,$5,$6}')
export X;

Thanks,
R. Singh

Last edited by RavinderSingh13; 02-09-2016 at 05:12 AM.. Reason: Added one more solution where placing the value of command into alias name by using $ too now.
# 3  
Old 02-09-2016
Please be aware that RavinderSingh13's proposal reflects the situation at login. No development during the session could be monitored.

---------- Post updated at 10:27 ---------- Previous update was at 10:20 ----------

Try escaping certain meta chars:
Code:
alias DF="df -k /tmp | awk 'NR==1 {CNV=1024*1024;GBYTES=\"GBytes\";FMT=\"%-35s %-10s %-10s %-10s %10s %-s\n\"; printf FMT, \$1, GBYTES, \$3, \$4, \$5, \$6; next} {printf FMT,\$1,\$2/CNV,\$3/CNV,\$4/CNV,\$5,\$6}'"

# 4  
Old 02-09-2016
Try using a function instead of an alias, which allows you to use parameters:

Code:
x() {
  df -k "$1" | awk 'NR==1 {CNV=1024*1024;GBYTES="GBytes";FMT="%-35s %-10s %-10s %-10s %10s %-s\n"; printf FMT, $1, GBYTES, $3, $4, $5, $6; next} {printf FMT,$1,$2/CNV,$3/CNV,$4/CNV,$5,$6}'
}

Code:
x /tmp

--
It also allows you to make it more readable:
Code:
x() {
  df -k "$1" | 
  awk '
    NR==1 {
      CNV=1024*1024
      FMT="%-35s %-10s %-10s %-10s %10s %-s\n"
      printf FMT, $1, "GBYTES", $3, $4, $5, $6
      next
    }
    {
      printf FMT,$1,$2/CNV,$3/CNV,$4/CNV,$5,$6
    }
  '
}


Last edited by Scrutinizer; 02-09-2016 at 03:14 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk alias passing a value to a variable

I am trying to turn this into an alias with no luck. I would then like to put the alias into my bashrc file. I know awk is very picky about quotes. I have tried every version of quotes, single quotes, double quotes, and backslashes that I can think of. VAR=$(xrandr | awk '$2=="connected"{s=$1}... (3 Replies)
Discussion started by: cokedude
3 Replies

2. UNIX for Dummies Questions & Answers

Create alias files (not alias commands)

If one: $ find -name 'some expression' -type f > newfile and then subsequently wants to create an alias file from each pathname the find command retrieved and the > placed within 'newfile', how would one do this? Ideally, the newly created alias files would all be in one directory. I am... (3 Replies)
Discussion started by: Alexander4444
3 Replies

3. Shell Programming and Scripting

Alias problem with awk command

Hi to all, I'm facing some problems when adding an alias like: #alias list="ls -al | awk '{ print $1, $2, $3, $4, (($5/1048576))"\t", $6, $7, $8, $9 }'" and when I enter: #list I get: Syntax Error The source line is 1. The error context is { print >>> , <<< awk:... (3 Replies)
Discussion started by: enux
3 Replies

4. UNIX for Dummies Questions & Answers

using alias

I need to login to one server and then switch the user and set a number alias. But i cant modify the .profile file. I have one script avi1.sh $ more avi.sh sudo su - bil sh avi1.sh and in home directory of bil i have avi1.sh that says $ more avi1.sh alias l='ls -ltr' alias b='cd... (7 Replies)
Discussion started by: blackeyed
7 Replies

5. Shell Programming and Scripting

How to alias an awk command with single and double quotes?

Hi, I am trying to write the following command as an alias in my .bashrc file. bjobs -u all | awk '{if (NR > 1) {username++;}}END{{print"\nJOBS BY USER:\n"} for (i in username) {print username,i;}{print"\n Total Jobs=",NR-1,"\n" }}' The command simply puts how many jobs each user is... (2 Replies)
Discussion started by: jacekmaciek
2 Replies

6. Shell Programming and Scripting

Alias to awk BEGIN statement

I'd like to define an alias to awk's begin statement since I use awk with different delimiters all the time and it is tiresome to type awk '{OFS="\t";FS="\t"}{BLAH BLAH}' every time. The problem is that bash won't let me make an alias with an open quote, which is necessary for the BEGIN alias to... (3 Replies)
Discussion started by: baconbasher
3 Replies

7. Shell Programming and Scripting

AWK alias with parameters

Hello, How can I make an alias of an AWK one liner that prints specific lines from a file like below? # from a command like this: awk 'NR == 100, NR == 150' file.cfg The alias I want to make should work like this(I guess): <alias_command> <file.cfg><start_line><end_line> So the... (1 Reply)
Discussion started by: majormark
1 Replies

8. UNIX for Dummies Questions & Answers

How to create alias with awk command

Good morning I would like kindly to ask you to help me with creation of alias with awk command. alias a="awk {print $1 "| " $2 "| " $3 "| " $4 "| " $5 "| " $6 "| " $7 "| " $8 "| " $13 "| " }" there is some error but I don't find it please explain me what is error and how can be avoid... (5 Replies)
Discussion started by: papa1980
5 Replies

9. Shell Programming and Scripting

awk alias

Good morning I would like kindly to ask you to help me with creation of alias with awk command. alias a="awk {print $1 "| " $2 "| " $3 "| " $4 "| " $5 "| " $6 "| " $7 "| " $8 "| " $13 "| " }" there is some error but I don't find it please explain me what is error and how can be avoid... (1 Reply)
Discussion started by: papa1980
1 Replies

10. Shell Programming and Scripting

Define an alias with an embeded awk command ??

Hi all, I'm trying to define an alias with an embeded awk command: alias kpipe='kill `psme| grep "ifw -r" | grep -v "grep ifw -r"| awk '{print $2}'`' The problem is that the awk command (awk '{print $2}') contains two ' ..' quotes. So bash assumes that the first awk quote corresponds to... (5 Replies)
Discussion started by: jfortes
5 Replies
Login or Register to Ask a Question