Solved

Column value change script

Posted on 2015-01-05
4
144 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

705 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now