This awk should work, shouldn't it?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers This awk should work, shouldn't it?
# 1  
Old 09-28-2015
This awk should work, shouldn't it?

Heyas

Trying to parse a tempfile, but somehow i mess up.
To my understand, this should work...

Plain:
Code:
tail -n1 out.tmp
  1 81.5M    1 1066k    0     0   359k      0  0:03:52  0:00:02  0:03:50  359k

I want to get the 81.5M, so i'd assume it'll be $2 for awk....
Code:
tail -n1 out.tmp | awk '{print $2}'
0

Hmm it wants not?
Code:
var=$(tail -n1 out.tmp) ;echo "$var"
  1 81.5M    1 1066k    0     0   359k      0  0:03:52  0:00:02  0:03:50  359k

echo "$var" | awk '{print $2}'
0

Most confusing to me:
Code:
echo "$var" | IFS=" " awk '{print $1,$2,$3,$4,$5,$6,$7,$8}'
 0 0 0 0 0 0 0

Any ideas please?

OS: 4.1.6-1-ARCH
GNU Awk 4.1.3, API: 1.1 (GNU MPFR 3.1.3-p4, GNU MP 6.0.0)
GNU bash, Version 4.3.42(1)-release (x86_64-unknown-linux-gnu)

Thank you
# 2  
Old 09-28-2015
Hello sea,

Could you please try following.
Code:
awk 'END{print $2}' Input_file

No need to use tail and echo and then save it to a variable then. awk is capable to read files.Also I don't think there is a need of mentioning IFS there too as we know awk has FS OR -F option to set field separator but if we are NOT setting it, it will set field separator to space only.
Anyways thinking for your problem more as it worked for me by your way too, will let you know if get something.


Thanks,
R. Singh

Last edited by RavinderSingh13; 09-28-2015 at 08:29 AM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 09-28-2015
Hello Ravinder

Yeah, the saving to the variable was mainly for testing purposes.

Code:
awk 'END{print $2}' out.tmp 
0

Sadly, no change.
Thank you
# 4  
Old 09-28-2015
Hello sea,

Could you please try as follows.
Code:
awk '{for(i=1;i<=NF;i++){print i FS $i}}' Input_file

We can check by above command what's happening here, also if possible then show us sample input. Also considering there are no carriage characters in your Input_file too Smilie.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 09-28-2015
Try this:
Code:
awk '{$1=$1}END{print $2}' out.tmp

# 6  
Old 09-28-2015
No change, still just a zero.

Even tried with:
Code:
awk -v IFS=" "  '{$1=$1}END{print $2}' out.tmp

As well, just a 0.

Might it be a curl issue?
Code:
0 tui # curl https://www.kernel.org/pub/linux/kernel/v4.x/linux-4.2.1.tar.xz -o out.tar.xz 2&>out.tmp 
^C
130 tui # cat out.tmp 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  5 81.5M    5 4749k    0     0  1047k      0  0:01:19  0:00:04  0:01:15 1047k

0 tui # awk -v IFS=" "  '{$1=$1}END{print $2}' out.tmp
0

Thank you

---------- Post updated at 14:02 ---------- Previous update was at 13:54 ----------

Overseen yours Ravinder, sorry, looks promising.. somehow Smilie
Code:
awk '{for(i=1;i<=NF;i++){print i FS $i}}' out.tmp 
1 %
2 Total
3 %
4 Received
5 %
6 Xferd
7 Average
8 Speed
9 Time
10 Time
11 Time
12 Current
1 Dload
2 Upload
3 Total
4 Spent
5 Left
6 Speed
1 
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 --:--:--
11 --:--:--
12 --:--:--
13 0
14 0
15 0
16 0
17 0
18 0
19 0
20 0
21 0
22 --:--:--
23 --:--:--
24 --:--:--
25 0
26 0
27 81.5M
28 0
29 20024
30 0
31 0
32 14343
33 0
34 1:39:22
35 0:00:01
36 1:39:21
37 14343
38 0
39 81.5M
40 0
41 146k
42 0
43 0
44 64145
45 0
46 0:22:13
47 0:00:02
48 0:22:11
49 64133
50 0
51 81.5M
52 0
53 442k
54 0
55 0
56 134k
57 0
58 0:10:22
59 0:00:03
60 0:10:19
61 134k

# 7  
Old 09-28-2015
Quote:
Originally Posted by sea
No change, still just a zero.

Even tried with:
Code:
awk -v IFS=" "  '{$1=$1}END{print $2}' out.tmp

As well, just a 0.

Might it be a curl issue?
Code:
0 tui # curl https://www.kernel.org/pub/linux/kernel/v4.x/linux-4.2.1.tar.xz -o out.tar.xz 2&>out.tmp 
^C
130 tui # cat out.tmp 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  5 81.5M    5 4749k    0     0  1047k      0  0:01:19  0:00:04  0:01:15 1047k

0 tui # awk -v IFS=" "  '{$1=$1}END{print $2}' out.tmp
0

Thank you
I think you are confused, if you are using IFS for setting field separator, then there will be no effect use FS instead or -F command line argument
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Why does this if match although it shouldn't?

Hi everyboy, I am a little confused and can't understand why I get a positive match in the following case. Shell Script section echo $SHELL echo "Server type = ${SERV_TYPE}" if ]]; then echo "foor" echo -e $(_date) "${cinfo}INFO: ${crst}Checking SAP memory on ${HOST}"This is the... (1 Reply)
Discussion started by: h1kelds
1 Replies

2. UNIX for Dummies Questions & Answers

Users of own group shouldn't be able to delete

Oracle Linux 6.5 oracle user's primary group is oinstall and its secondary group is dba,asmdba,asmoper. For the below created directory, I want the users belonging to dba,asmdba,asmoper to be able create, read and execute files but not delete them. How can I achieve that. If I use 775 as... (5 Replies)
Discussion started by: kraljic
5 Replies

3. Shell Programming and Scripting

Script outputing out numbers when it shouldn't

i suspect the issue is with the IFS part. I have a script that reads a file. the problem here is that, when i run the script, it outputs a bunch of numbers. i know what these numbers are, but i dont understand why they're being sent to the screen. as you can see below, everything should be... (5 Replies)
Discussion started by: SkySmart
5 Replies

4. Solaris

Particular user account shouldn't be locked after entering wrong passwd specfic no. times

Hi all In my system we have implemented user lockout feature after 3 failure attempt if he tries to login directly or if he run the any command through sudo and enter wrong password thrice. Now I have requirement in which particular user account shouldn't be locked when he run the command... (1 Reply)
Discussion started by: sb200
1 Replies

5. Shell Programming and Scripting

Delimited data contains line feeds where they shouldn't be

I have some data, each record (line) ends with a line feed (\n). Each field is pipe (|) delimited. 1|short desc|long text|2001-01-01 01:01 2|short desc| long text |2002-02-02 02:02 3|short desc| long text | 2003-03-03 03:03 4|short desc | long text | 2004-04-04 04:04 ... (10 Replies)
Discussion started by: ericdp63
10 Replies

6. Shell Programming and Scripting

Parse file using awk and work in awk output

hi guys, i want to parse a file using public function, the file contain raw data in the below format i want to get the output like this to load it to Oracle DB MARWA1,BSS:26,1,3,0,0,0,0,0.00,22,22,22.00 MARWA2,BSS:26,1,3,0,0,0,0,0.00,22,22,22.00 this the file raw format: Number of... (6 Replies)
Discussion started by: dagigg
6 Replies

7. Shell Programming and Scripting

awk: assign variable with -v didn't work in awk filter

I want to filter 2nd column = 2 using awk $ cat t 1 2 2 4 $ VAR=2 #variable worked in print $ cat t | awk -v ID=$VAR ' { print ID}' 2 2 # but variable didn't work in awk filter $ cat t | awk -v ID=$VAR '$2~/ID/ { print $0}' (2 Replies)
Discussion started by: honglus
2 Replies

8. Shell Programming and Scripting

why awk does not work here?

I am trying to find any line with the 9th column's number greater than 200, but why the following awk command does not work? awk '$9 > 200' /tmp/test 2007-09-05 10:13:05.714 640.847 any 1.2.3.4 719 2445 487260 32 6082 199 2007-09-05 10:13:02.686 641.827... (2 Replies)
Discussion started by: fedora
2 Replies

9. UNIX for Dummies Questions & Answers

why is this so hard when it shouldn't be?

All I ask is one thing: 1. Where can i dl unix and the driver for my modem? right now i have win xp HP notebook, and bellsouth is my provider with a westell modem. (2 Replies)
Discussion started by: velious
2 Replies
Login or Register to Ask a Question