user2243341 user2243341 - 4 months ago 9
Bash Question

Bash Script to print different fields in each line

My input is as follows:-

Test=variable

Test ID=2122

1 hello till you active

2 This code function 3

And I required output like below:-

variable

2122

till

Answer
awk '/=/ {gsub(/^.*=/,""); print; next} !/=/ {print $3}' file
variable
2122
till
code

Explained:

awk '
/=/ {             # if equals sign in the record
  gsub(/^.*=/,"") # remove everything from the beginning to the =
  print           # print it 
  next}
{print $3}        # print the third field
' file

EDITED per advice by @karakfa and @John1024.

Comments