21

How can i do this using awk?

Example -

awk '{split($1,A,"."); print A[-1], $1, $2, $3, $4}'

Sample input and output.

Input

123 456 abc.def.ghi 789 
321 654 qaz.wsx.edc.rfv 987

Output

ghi 123 456 abc.def.ghi 789  
rfv 321 654 qaz.wsx.edc.rfv 987
1
  • can you clarify if you want to split first field or the third field? Commented Sep 26, 2016 at 13:45

5 Answers 5

40

If your problem is exactly as the example in your question, take the answer from @muzido, $NF will give you the last field.

If you just want to know the last element of an array by split():

split() function will return you how many elements it has just "splitted", test with your code: awk '{print split($1,A,".")}' file you will see the number. Then you can just use it by:

awk '{n=split($1,A,"."); print A[n]}' file 
# n is the length of array A
Sign up to request clarification or add additional context in comments.

3 Comments

@anshulgupta you can even: print A[split($1,A,".")] ;-)
Let me know if you can optimize it further before i finalize my code ;)
@anshulgupta it is not a kind of "optimization", I think the line in my answer (with n variable), is more readable.
11

If you have GNU awk, you can try the function length on a array:

awk '{split($1,A,"."); print A[length(A)]}'

4 Comments

length() function here is redundant
@Kent I agree that split() and length() are kind of redundant together. However I find nice to have length() working with an array.
@Oliv, it's sad that i can only mark one as correct answer!
Why would you run length() on an array when split() just returned the length of the array?
3

Why not:

$ awk '{print A[split($3,A,".")],$0}' input.txt

Hope it helps!

Comments

1

Kent already gave you the split() answer but you don't need split creating/using an array for this, e.g. with GNU awk for gensub():

$ awk '{print gensub(/.*\./,"",1,$3), $0}' file
ghi 123 456 abc.def.ghi 789
rfv 321 654 qaz.wsx.edc.rfv 987

Comments

0

any solution that involves split() or gensub() is already overkill :

echo '
123 456 abc.def.ghi 789
321 654 qaz.wsx.edc.rfv 987' | 

awk 'sub(_, substr($3" ", match($3, /[^.]+$/), ++RLENGTH))'

ghi 123 456 abc.def.ghi 789
rfv 321 654 qaz.wsx.edc.rfv 987

If you want to make it fool-proof to guard against the unlikely scenario the input row has not dots at all in $3, then make it

 awk '    ! match($3, /[^.]+$/) ||
      $0 = substr($3, RSTART, RLENGTH)" "$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.