Solved

Column value change script

Posted on 2015-01-05
4
149 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
block mails from a particular country in postfix 7 80
Linux kernel panic ext3-fs error 14 76
Krita v3 Linux Mint/Ubuntu 16.04 9 75
Scripting 4 35
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

751 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