1

I am using bash scripting in Linux. I have a variable that looks like this:

a="1-1-1 1-1-2 1-1-3 1-1-4"

I want to separate columns with values equal to or greater than 1-1-3 using AWK.

My output should be:

1-1-3 1-1-4

Can someone help me with this?

2
  • How do you define "greater than"? What about a="2-1-1 1-1-2 1-1-3 1-1-4" ? Commented Jan 23, 2016 at 8:29
  • I think at first , column one should be compared then two and three, regarding to your example, output will be 2-1-1 1-1-3 1-1-4. if our data is single column I can use echo "$a | awk '($0 >= 1-1-3) {print $0}' ... but now I have input in one line Commented Jan 23, 2016 at 8:41

3 Answers 3

1

You can do it without a loop, if you use the space as record separator:

echo $a | awk -v RS=' ' -v ORS=' ' '{$1=$1} $1>="1-1-3"'
Sign up to request clarification or add additional context in comments.

Comments

1

You can loop over the line and compare:

echo $a | awk -v ORS=' ' '{for(i=1;i<=NF;i++) if($i >= "1-1-3") print $i;}'

Comments

1

sort's -V option gives you "version" sorting that might be helpful:

$ a="2-0-0 1-1-1 1-1-2 1-1-3 1-1-4"
$ tr ' ' '\n' <<<"$a" | sort -V
1-1-1
1-1-2
1-1-3
1-1-4
2-0-0

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.