Space with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Space with awk
# 1  
Old 11-21-2011
Space with awk

Hi,

Need some help with awk and blank fieds..
Code:
[root@host1 ~]# last -n 2
gml4410  pts/0 host3 Fri Nov 18 07:46 - 07:53  (00:06)
nmc5060 host2                  Thu Nov 17 15:29 - 15:30  (00:00)

I need to extract field1(userid) , field5(month) and field 6(day).

Notice the second line has blank space in 3rd field. so my awk gets the wrong fields.
Code:
[root@host1 ~]#last -n 2 | awk '{print $1,$5,$6}' 
gml4410 Nov 18 == This is what I need.
nmc5060 17 15:29


Last edited by Scott; 11-21-2011 at 06:47 AM.. Reason: Added code tags
# 2  
Old 11-21-2011
Use [code] tags.

You might be able to use cut, but it's hard to tell without seeing the real input.
# 3  
Old 11-21-2011
Code:
 
last -n 2 | nawk '{print $1,$(NF-5),$(NF-4)}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Create the space only in the Third column by awk

Hi All , I am having an input file like this Input file r s e y Pin Numbers s n eppppppppppppppppppc ... (3 Replies)
Discussion started by: kshitij
3 Replies

2. Shell Programming and Scripting

awk to escape space in name

I am searching a file and not able to escape a space if $i has a space in the name. I tried escaping it with a \ and also trying to add it to allow for spaces in the search. Neither are correct as the first awk only outputs path and the second awk doesn't run. Thank you :). first awk awk... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

Need to use delimiter as : and space in awk

Hi , Please suggest me how do I use : (colon and one space) as a delimiter in awk Best regards, Vishal (2 Replies)
Discussion started by: Vishal_dba
2 Replies

4. UNIX for Advanced & Expert Users

Need to remove leading space from awk statement space from calculation

I created a awk state to calculate the number of success however when the query runs it has a leading zero. Any ideas on how to remove the leading zero from the calculation? Here is my query: cat myfile.log | grep | awk '{print $2,$3,$7,$11,$15,$19,$23,$27,$31,$35($19/$15*100)}' 02:00:00... (1 Reply)
Discussion started by: bizomb
1 Replies

5. Shell Programming and Scripting

awk print used space

Hi Team, Can you please tell me how to get a used space in KB LINUX and free space in KB separate commands. For example: $df -k /rer (apdfp01.xxx.com:/var/adm/rash/MT) : 2066900 total allocated Kb 4579 free allocated Kb 16121 used allocated Kb 89 % allocation used /dev/deviceFS1... (5 Replies)
Discussion started by: indira_s
5 Replies

6. Shell Programming and Scripting

AWK - Ignoring White Space with FS

I have an AWK script that uses multiple delimiters in the FS variable. FS="+" My awk script takes a file name such as this: 12345_smith_bubba_12345_20120215_4_0.pdf and parses it out based on the under score. Each parsed field then has some code for data validation etc. This script has... (12 Replies)
Discussion started by: reno4me
12 Replies

7. UNIX for Dummies Questions & Answers

awk for variable with a space

Hi experts, why does $ echo "one two three" | awk '{$3="my tree"; print $0}' one two my tree work, but $ var="my tree" $ echo "one two three" | awk '{$3="'$var'"; print $0}' awk: {$3="my awk: ^ unterminated string does not work? How can the variable tha contains a space be... (2 Replies)
Discussion started by: GermanicGalore
2 Replies

8. Shell Programming and Scripting

Replacing / with a space using awk

I have a string and want to replace the / with a space. For example having "SP/FS/RP" I want to get "SP FS RP" However I am having problems using gsub set phases = `echo $Aphases | awk '{gsub(///," ")}; {print}'` (5 Replies)
Discussion started by: kristinu
5 Replies

9. Shell Programming and Scripting

using awk with space

when printing using awk how can i print all the fields on 1 line? #!/bin/sh awk '{print $1 $2 $3}' givesMeOutputLikeThis whereas awk '{print $1 " " $2 " " $3}' gives Me Output Like This I would like to line like this -> "gives Me Output Like This" (11 Replies)
Discussion started by: strike
11 Replies

10. Shell Programming and Scripting

Search with awk, but having space within

If in my search string with awk, if I have spaces, I am getting an awk error. e.g. awk /A B/ filename How can I search the pattern which has space within? (1 Reply)
Discussion started by: videsh77
1 Replies
Login or Register to Ask a Question