title of newly added column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting title of newly added column
# 1  
Old 12-13-2010
title of newly added column

Hi,

I have a csv file. I need to add one column at begining of the file, It just contains one value "amr". I am able to add column but unable to put title to it.

Input Data:
Code:
call_id,conn_id,result,ani
1,100,hungup,7601234
2,101,hungup,7601235

Desired Output:
Code:
region,call_id,conn_id,result,ani
amr,1,100,hungup,7601234
amr,2,101,hungup,7601235

Below code gives me output file by adding new column at begining. But it gives "amr" as title, I want title of column as "region". Appreciate your help on this.
Code:
awk '{print "AMR," $0}' <filename>

# 2  
Old 12-13-2010
Code:
awk '{printf (NR>1?"amr,":"region,")} 1'  filename

# 3  
Old 12-13-2010
Thanks for quick reply. It works fine.
Few days back, I raised another question in this forum mainly to add 2 columns after column1. Below is URL of the post.

https://www.unix.com/shell-programmin...#post302478356

Just wondering if i can do both these tasks using single command.

Input Data:
Code:
call_id,conn_id,result,ani
1,100,hungup,7601234
2,101,hungup,7601235

Desired Output:
Code:
region,call_id,key,value,conn_id,result,ani
amr,1,100,dummy,10,hungup,7601234
amr,2,101,dummy,10,hungup,7601235

---------- Post updated at 04:59 PM ---------- Previous update was at 04:57 PM ----------

Correction to desired Output: In summary we want to add three dummy columns. One at beginning of the file, other two after column1.

Code:
region,call_id,key,value,conn_id,result,ani
amr,1,dummy,10,100,hungup,7601234
amr,2,dummy,10,101,hungup,7601235

# 4  
Old 12-13-2010
This should do the trick:

Code:
awk -F, '{$1=NR>1?"amr,"$1",dummy,10":"region,"$1",key,value"} 1'  OFS=, filename

# 5  
Old 12-13-2010
Thanks a lot. it works perfectly as i expected.
Appreciate if you can walk me through the piece of code and give some explanation.
# 6  
Old 12-13-2010
Every awk program consists of a 1 or more blocks of LOGICAL-EXPRESSION { ACTIONS }

If LOGICAL-EXPRESSION is evaluated for each input line and if it equates to true (or it wasn't specified) the ACTIONS block is executed for that line.

(Special LEs of BEGIN (and END) execute their ACTION block once before (or after) the file is processed, this program dosn't use BEGIN or END)

If ACTIONS block is missing the default action is applied, which is print the current line buffer.

In this program we have 1 action block without a LE (so it executes for every line) and after that we have an LE of 1 (true) with no ACTION so it prints every line buffer. We modify the value of field 1 in the 1st action block and this printe in the 2nd.

Breakdown of code as follows:

CodeDescription
-F, Field seperator as comma
$1= Replace value of first field with...
NR>1?X:Y if record # greater than 1 then X else Y
"amr,"$1",dummy,10"X: arm, + 1st field + ,dummy,10
"region,"$1",key,value"Y: region, + 1st field + ,key,value
1 1 is true, default action is print record (with updated 1st field), using OFS specified below
OFS=, Output field sepeartor is comma

Last edited by Chubler_XL; 12-13-2010 at 10:33 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 7  
Old 12-14-2010
Thanks a lot for your time. It is really helpful.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Mount a newly added LUN on a GNU/Linux distro

Hi I am not familiar with the linux, but I was asked to create a file system on a LUN from the NetApp that was mapped to the linux server. The server is runing: uname -a Linux localhost.localdomain 2.6.18-92.el5 #1 SMP Tue Apr 29 13:16:15 EDT 2008 x86_64 x86_64 x86_64 GNU/Linux and now... (6 Replies)
Discussion started by: fretagi
6 Replies

2. Red Hat

"Unable to Detect Newly added HDD - Redhat 5"

Hi Folks, I am unable to detected newly added HDD in my redhat-5 OS. I am expecting to get /dev/sdb. Following are the utilized commands or syntax. root@hostname% cat /etc/redhat-release Red Hat Enterprise Linux Client release 5.4 (Tikanga) echo "- - -" > /sys/class/scsi_host/host0/scan %... (1 Reply)
Discussion started by: Sricharan21
1 Replies

3. UNIX for Advanced & Expert Users

CVS: can not checkout a newly added file

My project is on solaris 10 using cvs 1.12.13. A team member has added a new file to the repo using the add command followed by commit. When another team member executes the status command in the directory containing the new file they do not get notifiction that there is a file that needs to be... (0 Replies)
Discussion started by: twk
0 Replies

4. Hardware

Formatting a newly created lun

Hi , I have created one new lun in my SAN storage and make it visible to my HP servers , but the fdisk -l output is somehow confusing. Do not know what to do next ---------- fdisk -l /dev/sdo1 Disk /dev/sdo1 (Sun disk label): 64 heads, 32 sectors, 10238 cylinders Units =... (7 Replies)
Discussion started by: mishra.sankar
7 Replies

5. Shell Programming and Scripting

Using filename to determine a prefix that needs to be added to string column on file?

Possible filenames: CDD_Whatever.txt DDD_Whatever.txt If the file prefix = CDD, I'd like to prefix every person ID (second column in my examples below) on the file with "c-" If the file prefix = DDD, I'd like to prefix ever person ID with "d-" Input: Desired Output: Any help... (2 Replies)
Discussion started by: lrluis
2 Replies

6. Red Hat

Partitioning newly added disk to Redhat

Hi Everyone, I have added new Virtual disk to OS. The main point is I need to bring this whole Disk into LVM control, is it necessary to partition the disk using fdisk command and assign partition type as '8e', or can I directly add that disk into LVM, by running pvcreate command with out... (2 Replies)
Discussion started by: bobby320
2 Replies

7. UNIX for Dummies Questions & Answers

how to open file and see newly added updates

Hi, I just forgot what command we can use to open a file, and then see its updates, I remember that there is an option -f ,but with what command :wall: Tx Trent (1 Reply)
Discussion started by: trento17
1 Replies

8. Shell Programming and Scripting

List of filenames where column title matches string and value is in limits

I'm somewhat new to BASH scripting but have managed to work my way through most of a problem. I'm trying to get a list of filenames where a column header occurs and any value in that column is within a range. So far I can sort through the list of files in a directory specified by the user, find... (5 Replies)
Discussion started by: hu_r_u2000
5 Replies

9. Red Hat

Redhat 5 can't see my newly added LUN.

hi all, i have added new LUN to Redhat 5. i have already scanned LUN devices and it is confirmed that Kernel sees the newly added LUN's. i have used /proc/partitions and verified that my disks are there. However, i cannot find my disk using fdisk -l command. I am not sure what did i... (2 Replies)
Discussion started by: gisu0602
2 Replies

10. Shell Programming and Scripting

How to find the newly created directory

Hi, I need to create new directory by increasing the number by 1 of extracted lastly created directory. e.g. Log\out_log_1\ Log\out_log_2\ Log\out_log_3\ become Log\out_log_1\ Log\out_log_2\ Log\out_log_3\ Log\out_log_4\ Can anyone help how to do it in c-shell... (3 Replies)
Discussion started by: Andre_2008
3 Replies
Login or Register to Ask a Question