-1

Second pattern

gsub(/FHD/,"HD",val) is working 

First pattern

gsub(/"  1"/;"+1",val) is not working

dont know how to put all in one regex.

Input (pattern to be found is " 1" and "FHD"):

Abc Sort 3  1 FHD 
Dcef Raz 1  1 FHD
Hgd  1 1 FHD
Und der trees FHD

Output (" 1" must be changed in "+1" and "FHD" in "HD")

Abc Sort 3 +1 HD
Dcef Raz 1 +1 HD
Hgd +1 1 HD
Und der trees HD
Francesco
  • 11
  • 3
  • 2
    Sorry, but StackOverflow isn't a free coding service (or exam answering service). You're expected to show your code, along with relevant sample inputs, expected outputs, actual error msgs as well as your comments about where you are stuck. Please show your best effort to solve this problem (use the {} tool at the top left of the edit box to format code/data/output/errMsgs correctly), and people may be able to help you. Good luck. – shellter Aug 23 '17 at 13:13
  • done thanks, i'm sorry. – Francesco Aug 23 '17 at 13:31
  • Why just the first `1` on the third record is changed to `+1` (As opposed to the second `1`, or both) why is the logic different for the third record than the second record? – JNevill Aug 23 '17 at 13:49
  • Also, if you split this record by a space, is the logic (assuming that third record example is bad): If the third field is `1` and the fourth field `FHD` then change the third field to `+1` and change the fourth field to `HD`? – JNevill Aug 23 '17 at 13:52
  • Like @JNevill, you need to specify what you want change. So: do you want only the first "1" occurrence on each line to be changed to "+1" or do you want all of them to be changed? – Marc Lambrichs Aug 23 '17 at 14:20
  • thanks, i want all " 1" occurences to be changed in "+1" , i'm trying to use gsub – Francesco Aug 23 '17 at 14:41
  • If your question is "how do I map `1` to `+1` and `FHD` to `HD` in one regexp substitution with gsub()?" then the answer is "you can't". If that's NOT what you're asking then [edit] your question to clarify what your trying to do. – Ed Morton Aug 23 '17 at 16:12

1 Answers1

0

To get the output you posted from the input you posted would be:

$ awk '{gsub(/  1/," +1"); gsub(/FHD/,"HD")}1' file
Abc Sort 3 +1 HD
Dcef Raz 1 +1 HD
Hgd +1 1 HD
Und der trees HD

If that's not what you're looking for then edit your question to clarify what it is.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185