Solved

Column value change script

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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.

785 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