Manipulating column in a file in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Manipulating column in a file in UNIX
# 1  
Old 02-11-2014
Manipulating column in a file in UNIX

Hi

I have a file content.txt where I want to replace the value of second column of the file with half of the value of that column.
I only have to replace if the second column starts with COM_, rest all values have to be same
eg,

cat content.txt
Code:
|COM_A|123|JHV|8475
|ABC|2765|BV|876
|COM_B|2437|HDD|854
|YUT_F|78|JDHF|UE
|COM_GTH|8475|FNV|58

Output file
Code:
|COM_A|61.5|JHV|8475
|ABC|2765|BV|876
|COM_B|1218.5|HDD|854
|YUT_F|78|JDHF|UE
|COM_GTH|4237.5|FNV|58

Can you please let me know how to do it

Last edited by Scrutinizer; 02-11-2014 at 05:33 AM.. Reason: code tags
# 2  
Old 02-11-2014
Try:
Code:
awk '$2~/^COM_/{$3/=2}1' FS=\| OFS=\| file

# 3  
Old 02-11-2014
To get a little practice myself....

Code:
OLD_IFS="$IFS"
IFS="|"
while read ColA ColB ColC ColD;do
    newB=$(echo "$ColB / 2" | bc)
    printf "$IFS$ColA$IFS$newB$IFS$ColC$IFS$ColD"
done
IFS="$OLD_IFS"

Hope this helps
# 4  
Old 02-11-2014
@sea: IFS can be used local to the read command:
Code:
while IFS=\| read ColA ColB ColC ColD
do
  ...
done < file

So IFS does not need to be set and reset..
Note that ColA is empty, ColD contains both columns 4 and 5 and ColC needs to be changed if ColB meets the criteria..

Last edited by Scrutinizer; 02-11-2014 at 06:17 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 02-11-2014
Code:
 awk -F "|" '/COM_/{$3=$3 / 2;}1' OFS="|" filename

# 6  
Old 02-12-2014
Thanks Scrutinizer , the code worked well,

1 more question, is it possible to add one more level of validation,
like currently the code is working if the second column startes with _COM
I want to add one more level as the third column should only be "Scen"

Can you please let me know how can this be done
# 7  
Old 02-12-2014
Like so, you mean?
Code:
awk '$2~/^COM_/ && $4=="Scen" {$3/=2}1' FS=\| OFS=\| file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Manipulating UNIX and sqlite db

Hi there, I have multiple rows of data. For example: S/N | Name| Age I would like to store them into sqlite database after doing some grepping in CSV and output them into console/html format. Will it be possible? (1 Reply)
Discussion started by: alvinoo
1 Replies

2. UNIX for Advanced & Expert Users

Copy a column to another column in UNIX fixedwidth file

Hi All, I have a fixedwidth file of length 3000. Now i want to copy a column of 4 chars i.e( length 1678-1681) to column 1127 – 1171 to the same file. Please let me know how can i achive using a single command in fixed width file. Also source column length is 4 chars and target column length... (4 Replies)
Discussion started by: kiranparsha
4 Replies

3. Shell Programming and Scripting

Help with manipulating environmental variables in UNIX

I am wondering if there is away to increment a date in c shell. What I need to do is basic, but I lack the knowledge. I have they following environmental variable in my job scripts setenv YYYY `date '+%Y'` I then set YYYY to be part of my output dataset name: setenv dd_OUTPUTP... (1 Reply)
Discussion started by: jclanc8
1 Replies

4. Shell Programming and Scripting

need help on manipulating a file

Hi, I need a shell/command to achieve this task. I've a delimited unloaded file from oracle in a scrambled format as shown below with many blank lines in it, I'm just trying to tailor it in a format that would be compatible to view and load it to a IDS db. Here is the problem ... (1 Reply)
Discussion started by: divak
1 Replies

5. Shell Programming and Scripting

Manipulating csv file

We need to convert a field in a csv file which is in cents to dollars.(divide by 100) in our shell script. Can some body help me? (3 Replies)
Discussion started by: Deepthz
3 Replies

6. Shell Programming and Scripting

Manipulating a file

Hi everybody, I need an urgent help with a BASH script. I have file which contains (besides the other data) the lines with the following structure identified by with keyword PCList: <PARAMETER NAME="PCList" TYPE="LIST_STRUCTURE" MODEL="{,}" ... (1 Reply)
Discussion started by: sameucho
1 Replies

7. UNIX for Dummies Questions & Answers

Help!! manipulating file

Hi all, I need help manipulating the file below. Here is what I needed to do. First, I have to replace INSUPD to DELETE. Then I need to change the content of the file around by flipping the contents in the file from the bottom to the top (start from "CMD") How should I attack this? Here... (2 Replies)
Discussion started by: sirrtuan
2 Replies

8. Solaris

Manipulating File

Help...please. I have a log that contains Warning Authentication Failed: User GHDT88998HS doesn't exit: The User GHDT88998HS could not be found Mar 22, 2008 5:22:22AM com.hometel.ttm.auth.userlogin. about maybe a thousand entries failed user acct message How can I grab just the username... (2 Replies)
Discussion started by: rivendell500
2 Replies

9. Shell Programming and Scripting

Manipulating a text file

hey guys, need ur expert help. m a core banker got stuck in someting techie and cant find a solution have manged to extract a file from oracle apps in a format that looks something like this... REC- A b c d x INV- A b... (6 Replies)
Discussion started by: komalkg
6 Replies

10. UNIX for Advanced & Expert Users

Manipulating output file

I have a file containing two fields, Name and Time, with about 57 lines in this file. I am struggling to create a loop that will cut out the first ten lines of this file and echo it to the screen. Can anybody help me please. (1 Reply)
Discussion started by: mariner
1 Replies
Login or Register to Ask a Question