Find the answer to your Linux question:
Results 1 to 2 of 2
Hi Everybody I have the following file with 5 fields which are seperated by '|' Eno |Ename |Designation |dept |sal 5000 |Sharma |Director |Production |5000 6000 |guptha |Director |personnel |20000 ...
  1. #1
    Linux Newbie
    Join Date
    Jul 2004
    Posts
    143

    reg awk

    Hi Everybody

    I have the following file with 5 fields which are seperated by '|'

    Eno |Ename |Designation |dept |sal
    5000 |Sharma |Director |Production |5000
    6000 |guptha |Director |personnel |20000
    7000 |varma |Director |finance |5000


    My requirement is I want to change the value of 5th field(sal) to 10000 if it is 5000.

    My output will be like this
    Eno |Ename |Designation |dept |sal
    5000 |Sharma |Director |Production |10000
    6000 |guptha |Director |personnel |20000
    7000 |varma |Director |finance |10000

    I am using the following one.

    awk -F"|" '{if ($5==5000) $5=10000;print}' <filename>

    But the field seperator in the output will be space instead of a | like below where the sal field is 5000.

    Eno |Ename |Designation |dept |sal
    5000 Sharma Director Production 10000
    6000 |guptha |Director |personnel |20000
    7000 varma Director finance 10000

    Please help me to get the field seperator as |

    Thanks InAdvance,
    Mummaneni.

  2. #2
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    hi,



    -F (which equals FS) is just the input field separator. here you need to set the output field separator.
    so you go like this
    Code:
    awk  '{BEGIN { FS = "|"; OFS = "|"} {if ($5==5000) $5=10000;print}' <filename>
    Linux and me it's a love story

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...