Solved

Column value change script

Posted on 2015-01-05
4
148 Views
Last Modified: 2015-01-05
I would like to create a bash script that can replace the last column value with text.

test data
Rob;Wright;20;PC;2015;Type;2
Rob;Wright;20;PC;2015;Type;4
Rob;Wright;20;PC;2015;Type;5

wanted output
Rob;Wright;20;PC;2015;Type;fire
Rob;Wright;20;PC;2015;Type;water
Rob;Wright;20;PC;2015;Type;earth

I believe I could use awk or sed to this but not sure where to begin. Can you someone point me in the right direction.

 awk '/regexp/{gsub(/foo/, "bar")};{print}' file.txt
0
Comment
Question by:DOCDGA
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 40532559
awk -F';' 'BEGIN{OFS=FS;e[2]="fire";e[4]="water";e[5]="earth"}{$7=e[$7];print}'
0
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
ID: 40532590
You could use sed, but not as flexible as awk/perl.

sed -e "s/2$/fire/" -e "s/4$/water/" -e "s/5$/earth/  filename

or put all the sed commands into a file, eg:

s/2$/fire/
s/4$/water/
s/5$/earth/

And invoke with

sed -f myfile.sed filename
0
 

Author Comment

by:DOCDGA
ID: 40532767
Tried this solution
awk -F';' 'BEGIN{OFS=FS;e[2]="fire";e[4]="water";e[5]="earth"}{$7=e[$7];print}' file.txt
Output:
Rob;Wright;20;PC;2015;Type;fire
Rob;Wright;23;PC;2015;Tyep;water
Robi;werwea;43;ibb;3424;tt;earth
;;;;;;
;;;;;;
;;;;;;
Don't know why I'm receiving extra semicolons
0
 
LVL 84

Expert Comment

by:ozo
ID: 40532782
Were there extra blank lines in file.txt ?

If not all lines contain 7 fields, you may prefer something like
awk -F';' 'BEGIN{OFS=FS;e[2]="fire";e[4]="water";e[5]="earth"}{$NF=e[$NF]}1' file.txt
or
awk -F';' 'BEGIN{OFS=FS;e[2]="fire";e[4]="water";e[5]="earth"}e[$NF]{$NF=e[$NF]}1' file.txt
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question