Insert text into specific column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert text into specific column
# 1  
Old 12-24-2012
Insert text into specific column

I have the following data:

Code:
Dec 24 11:31:10 0000008b 9911662486 Answered Price SGD 0.003 PERIOD: 0 m 6 s
Dec 24 11:21:42 00000086 9911662486 Answered Price SGD 0.001 PERIOD: 0 m 2 s
Dec 20 15:34:28 00000004 9911662486 Answered Price SGD 0.007 PERIOD: 0 m 12 s
Dec 20 18:42:30 0000017b 9911662486 Answered Price SGD 0.005 PERIOD: 0 m 9 s
Dec 20 18:36:04 00000172 9911662486 Answered Price SGD 0.004 PERIOD: 0 m 7 s
Dec 20 18:29:21 0000016d 9911662486 Answered Price SGD 0.007 PERIOD: 0 m 11 s

I would like it to be:


Code:
Dec 24 11:31:10 0000008b 9911662486 Answered Price SGD 0.003 PERIOD: 0 m 6 s
Dec 24 11:21:42 00000086 9911662486 Answered Price SGD 0.001 PERIOD: 0 m 2 s
Dec 20 15:34:28 00000004 9911662486 Answered Price SGD 0.007 PERIOD: 0 m 12 s
Dec 20 18:42:30 0000017b 9911662486 Answered Price SGD 0.005 PERIOD: 0 m 9 s
Dec 20 18:36:04 00000172 9911662486 Answered Price SGD 0.004 PERIOD: 0 m 7 s
Dec 20 18:29:21 0000016d 9911662486 Answered Price SGD 0.007 PERIOD: 0 m 11 s
                                                                                   Total SGD 0.027

The "Total SGD 0.027" is insert into column 7,8,9


Last edited by Scott; 12-24-2012 at 08:37 AM.. Reason: Code tags
# 2  
Old 12-24-2012
Code:
# cat file ; awk '{ sum+=$9} END {print "Total SGD " sum}' file
Dec 24 11:31:10 0000008b 9911662486 Answered Price SGD 0.003 PERIOD: 0 m 6 s
Dec 24 11:21:42 00000086 9911662486 Answered Price SGD 0.001 PERIOD: 0 m 2 s
Dec 20 15:34:28 00000004 9911662486 Answered Price SGD 0.007 PERIOD: 0 m 12 s
Dec 20 18:42:30 0000017b 9911662486 Answered Price SGD 0.005 PERIOD: 0 m 9 s
Dec 20 18:36:04 00000172 9911662486 Answered Price SGD 0.004 PERIOD: 0 m 7 s
Dec 20 18:29:21 0000016d 9911662486 Answered Price SGD 0.007 PERIOD: 0 m 11 s
Total SGD 0.027

# 3  
Old 12-24-2012
Hi


Code:
awk '{x+=$9;}END{print "Total SGD  " x;}1'  file

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 4  
Old 12-24-2012
i would like to insert into column 7,8,9 at the last line. Is this possible?
# 5  
Old 12-24-2012
Something like this?

Code:
awk '{ sum+=$9} {print} END {printf "%60s\n", "Total SGD " sum}' file

Code:
Dec 24 11:31:10 0000008b 9911662486 Answered Price SGD 0.003 PERIOD: 0 m 6 s
Dec 24 11:21:42 00000086 9911662486 Answered Price SGD 0.001 PERIOD: 0 m 2 s
Dec 20 15:34:28 00000004 9911662486 Answered Price SGD 0.007 PERIOD: 0 m 12 s
Dec 20 18:42:30 0000017b 9911662486 Answered Price SGD 0.005 PERIOD: 0 m 9 s
Dec 20 18:36:04 00000172 9911662486 Answered Price SGD 0.004 PERIOD: 0 m 7 s
Dec 20 18:29:21 0000016d 9911662486 Answered Price SGD 0.007 PERIOD: 0 m 11 s
                                             Total SGD 0.027

This User Gave Thanks to mjf For This Post:
# 6  
Old 12-24-2012
Awesome! can explain a bit about the command?
# 7  
Old 12-24-2012
Code:
awk '{ sum+=$9} {print} END {printf "%60s\n", "Total SGD " sum}' file

Code:
{sum+=$9}

Sums the value in column 9 for each record in file. Since no delimiter is specified, space is the default delimiter.

Code:
{print}

Prints each record on file as is, i.e. with no changes (can also be written as {print $0} )

Code:
END {printf "%60s\n", "Total SGD " sum}

After all records from file are read (END), print the string "Total SGD " together with total sum of column 9 (sum) right justified with width of 60 spaces (%60s) followed by new line (\n).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert space in specific column among many columns

Hello, I have some problem in inserting the space for the pairs of columns. I have the input file : I used this code below in replacing it using space in specific column (replace space in each two columns) sed -e "s/,/ /2" -e "s/,/ /3" inputfile Output showed : However, I have many... (3 Replies)
Discussion started by: awil
3 Replies

2. Shell Programming and Scripting

Insert text line to specific location CSV

In Perl. ***edited question below*** Hey all, I am teaching myself some simple CSV file manipulation and have become a little stuck. Say I have the following layout in the CSV file: age,name,locationIs it possible to INSERT data into the CSV into the correct age order. For example, if I had... (1 Reply)
Discussion started by: whyte_rhyno
1 Replies

3. Shell Programming and Scripting

sed - insert text if column empty

Hi, I want to insert the text 'Unknown' in 2 specific columns in a csv file (actually | separated) if the column is blank. Its always the same columns. I have tried using sed: sed "s/||/|Unknown|/g" but there are occasion where other fields are blank and they need to be left blank. This... (4 Replies)
Discussion started by: ksexton
4 Replies

4. UNIX for Dummies Questions & Answers

Replacing a specific column of a text file with another column

Hi, I have a text file in the following format: Code: 13412 NA06985 0 0 2 46.6432798439 4 4 4 4 13412 NA06991 NA06993 NA06985 2 48.8478948517 4 4 2 4 13412 NA06993 0 0 1 45.8022601455 4 4 2 4 13401 NA06994 0 0 1 48.780669145 4 4 4 4 13401 NA07000 0 0 2 47.7312017846 2 4 4 4 ... (2 Replies)
Discussion started by: evelibertine
2 Replies

5. UNIX for Dummies Questions & Answers

Replacing a specific column of a text file with another column

I have a text file in the following format: 13412 NA06985 0 0 2 46.6432798439 4 4 4 4 13412 NA06991 NA06993 NA06985 2 48.8478948517 4 4 2 4 13412 NA06993 0 0 1 45.8022601455 4 4 2 4 13401 NA06994 0 0 1 48.780669145 4 4 4 4 13401 NA07000 0 0 2 47.7312017846 2 4 4 4 13402 NA07019... (3 Replies)
Discussion started by: evelibertine
3 Replies

6. UNIX for Dummies Questions & Answers

How to insert alternative columns and sort text from first column to second?

Hi Everybody, I am just new to UNIX as well as to this forum. I have a text file with 10,000 coloumns and each coloumn contains values separated by space. I want to separate them into new coloumns..the file is something like this as ad af 1 A as ad af 1 D ... ... 1 and A are in one... (7 Replies)
Discussion started by: Unilearn
7 Replies

7. Shell Programming and Scripting

Assigning a specific format to a specific column in a text file using awk and printf

Hi, I have the following text file: 8 T1mapping_flip02 ok 128 108 30 1 665000-000008-000001.dcm 9 T1mapping_flip05 ok 128 108 30 1 665000-000009-000001.dcm 10 T1mapping_flip10 ok 128 108 30 1 665000-000010-000001.dcm 11 T1mapping_flip15 ok 128 108 30... (2 Replies)
Discussion started by: goodbenito
2 Replies

8. Shell Programming and Scripting

Can sed be used to insert data at specific column?

I'm trying to use sed to insert data at a specific column, let's say my data looks like this: 0553 1828 0552 1829 0550 1829 0549 1830 0548 1831 what I want is this: timein 0553 timeout 1828 timein 0552 timeout 1829 timein 0550 timeout 1829 timein 0549 timeout 1830 timein 0548... (5 Replies)
Discussion started by: mswartz
5 Replies

9. Shell Programming and Scripting

Insert a text from a specific row into a specific column using SED or AWK

Hi, I am having trouble converting a text file. I have been working for this whole day now, still i couldn't make it. Here is how the text file looks: _______________________________________________________ DEVICE STATUS INFORMATION FOR LOCATION 1: OPER STATES: Disabled E:Enabled ... (5 Replies)
Discussion started by: Issemael
5 Replies

10. Shell Programming and Scripting

insert text into column

Hello! I have a text file containing some text : : : bla other text : : : bla any text : : : bla containing 3 columns separated by the ':' sign. Now i want to insert 'this' in the 2nd line after the 2nd column-deliminiter (i.e. into 3rd column), like some text : : : bla other text :... (1 Reply)
Discussion started by: knoxo
1 Replies
Login or Register to Ask a Question