2

I like to find and replace on the specific column using sed, because I don't know why awk gsub() didn't work in my Unix (Solaris) that's why I want to use sed.

Sample Data:

122|abc|ds,we||wrqg
145|dw|ett,335||nxd
166|rotl|qqqp,eoepepe||ge
776|gge|022||pp
039|pot|011a||lot

On the 3rd column, I want to replace comma(,) in pipe(|)

When I'm try to use gsub(), there's an error:

awk '{gsub(",","|",$3)}1' in.file > out.file

awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1

Another, I will import it on my database(Oracle). If the data with , will be replace with |, the data records that changed will not be on the same field. How can it be done?

The output should be like this.

122|abc|ds|we|wrqg
145|dw|ett|335|nxd
166|rotl|qqqp|eoepepe|ge
776|gge|022||pp
039|pot|011a||lot

Anyone, I really need this.

  • Need your help guys. – Ace Aug 8 at 7:31
  • I don't get that error. It just doesn't work because you don't set the FS and OFS to |. – pLumo Aug 8 at 7:40
  • You're running on Solaris? – Kusalananda Aug 8 at 7:41
  • I'm using putty.@Kusalananda – Ace Aug 8 at 7:45
  • putty is an ssh client. What is the OS on the server side? – pLumo Aug 8 at 7:45
2

The /usr/bin/awk on Solaris is severely limited in its support for various functions. In particular, the gsub() function is not implemented. This is explained in the manual for awk on Solaris.

For that, you should use /usr/xpg4/bin/awk (or nawk, "new awk").

Your code also don't specify a field separator for awk, so $3 would never contain anything unless you happen to have enough whitespace in your data to accidentally create a 3rd whitespace separated field. Likewise, you don't specify the output field separator, so any modified lines would have its fields delimited by spaces.

Instead:

/usr/xpg4/bin/awk -F '|' 'BEGIN { OFS=FS } { gsub(",", "|", $3); print }' file

To always get /usr/xpg4/bin/awk as your default awk on Solaris, use PATH="$(/usr/xpg4/bin/getconf PATH):$PATH" in your shell's startup file.

  • The code works! What if I set as my default awk, does my awk that I'm using on my server will affect the other function of awk? Or its the same and upgraded? – Ace Aug 8 at 8:02
  • 2
    @Ace It is conceivable that you actually always want to use nawk or /usr/xpg4/bin/awk and never /usr/bin/awk, as the older implementation of awk on Solaris is severely lacking. If you ever used awk on another Unix, the XPG4 awk will be the one that behaves most like most other awk implementations. – Kusalananda Aug 8 at 8:08
  • I still another problem. There are data that don't have a ,. The problem is I will import it on my database, so the data will not match on the field. How it will be done? – Ace Aug 8 at 13:37
  • @Ace I would suggest that you ask a new question about this. I'm a bit busy at the moment and there are many others that knows this stuff as well as (and better than!) me. – Kusalananda Aug 8 at 13:39
  • Thanks a lot J! – Ace Aug 8 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.