1

I have two files, fisrt "file.txt" have few lines, next file: "output.txt is empty", examples:

    cat file.txt

Line1     x   i   12gb
Line2     x   u   13.7gb
Line3     x   q   4.3gb
Line4     x   t   33gb
Line5     f   t   29.04gb
Line6     f   t   38gb
cat output.txt

I need to grep lines only where size is greater than 30 and separate size, f.e. 29.04gb to 29.04 gb (with white char) but i have problem with my script (in awk):

#!/bin/bash

plik=file.txt
OUT=output.txt

awk '{if (30<=$2) print $1, $4}' $plik >$OUT

My output.txt file looks:

Line3 4.3gb
Line4 33gb
Line6 38gb

Can u tell me where is the problem with my awk line ?

3
  • 1
    Please add sample input (no descriptions, no images, no links) and your desired output for that sample input to your question (no comment). Commented Mar 28, 2021 at 6:42
  • Your question is much harder to understand because you don't show exactly the output you want given that input. I assume by and separate size, f.e. 29.04gb to 29.04 gb (with white char) you mean you want a blank char between the number and gb in the output - if that's not chat you want then please clarify that statement. Commented Mar 28, 2021 at 16:09
  • It's confusing that in your code if (30<=$2) print $1, $4 you know that the number you want to print is stored in $4 but you're testing the value of $2 instead of $4 to decide whether to print it or not. There must be some reason for that but I can't imagine what it'd be unless your real data doesn't actually look like the example you provided. Commented Mar 28, 2021 at 16:13

3 Answers 3

2

With your shown samples, please try following.

awk '$NF+0>30' Input_file

Simply checking condition if last field(where +0 will take till digits only in it) is greater than 30, if this condition is TRUE then print that line.

Sign up to request clarification or add additional context in comments.

2 Comments

This command works. But what if i dont have my data size (f.e. 30.24gb) in last column ? Example: line1.file 30gb host1.new.york line2.file3 29.8gb host2.new.jersey The point is that I need to take only column 1 and 2 ($1 and $2) where size is greater than 30 (so we will take only 1st line) and then separate size from units parts. e.g. output in this case: line1.file 30 gb
@seroslaw, Sorry this is not clear, as per your shown samples, my code is working fine. Please do let me know more clearly what's not working?
0

I would do it following way using GNU AWK, let file.txt content be

Line1     x   i   12gb
Line2     x   u   13.7gb
Line3     x   q   4.3gb
Line4     x   t   33gb
Line5     f   t   29.04gb
Line6     f   t   38gb

then

awk '{$0=gensub(/([[:alpha:]]+$)/, " \\1", 1)}$4>=30{print $1, $4, $5}' file.txt

output

Line4 33 gb
Line6 38 gb

Explanation: I instruct AWK to look for letters ([[:alpha:]]) at end ($) of line and insert space before them, thus 4th column become number itself and 5th column gb, then when value in 4th column is less equal 30 I print 1st, 4th and 5th column.

(tested in GNU Awk 5.0.1)

Comments

0

It sounds like this is what you're asking for:

$ awk '($NF+0)>30{sub(/[[:alpha:]]/," &",$NF); print $NF}' file.txt
33 gb
38 gb

or maybe:

$ awk '($NF+0)>30{sub(/[[:alpha:]]/," &",$NF); print $1, $NF}' file.txt
Line4 33 gb
Line6 38 gb

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.