Visit Our UNIX and Linux User Community


Adding a new column in a file with other existing columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding a new column in a file with other existing columns
# 1  
Old 09-20-2010
Adding a new column in a file with other existing columns

Hi All ,
Kindly help me with this soln
Code:
awk '{printf "%s %7s \n", $1,$c}' infile

where
value of variable c I am externally giving input
But executing the above command shows all the columns of infile where as I want only 1st column of infile and 2nd column should print value c

Last edited by jim mcnamara; 09-20-2010 at 12:43 PM.. Reason: code tags please
# 2  
Old 09-20-2010
Code:
c="something"
awk   -v c="$c"  '{printf "%s %7s \n", $1,$c}' infile

-v $varname lets you pass shell environment variables to awk. One -v per variable usually.
# 3  
Old 09-21-2010
Hi Jim ,

Appreciate your kind support and also thanks for the explanation.Smilie

---------- Post updated 09-21-10 at 06:47 AM ---------- Previous update was 09-20-10 at 11:36 AM ----------

Hi Jim ,

While running the below command

Code:
nawk -v c="230031234567420987654321"  '{printf "%s %30s \n", $1,$c}'  infile

I am getting the following error

Code:
nawk: out of space in morefld

Please help me out

Last edited by Scott; 09-21-2010 at 05:13 PM.. Reason: Code tags, please...
# 4  
Old 09-21-2010
A typo of Jim, the command tries to print field number 230031234567420987654321 instead of the value, try this:
Code:
nawk -v c="230031234567420987654321" '{printf "%s %30s \n", $1, c}' infile

# 5  
Old 09-21-2010
Hi Jim,

Thanks again
but I am getting two more rows with the variable .
What I want is to print the variable except from 1st and last row .
Can we achieve this using NR .

Please let me know

---------- Post updated at 07:28 AM ---------- Previous update was at 07:15 AM ----------

Hi Jim

I come up with something

Code:
nawk -v c="230031234567420987654321" 'NR >1 && NR <8{ printf "%s %30s \n", $1, c}'  infile

I want to print variable c only for row 2 to 7
But above command printing both the columns for 6 times
How i can separate the $1 value so it can print for all 8 rows

Last edited by Scott; 09-21-2010 at 05:13 PM.. Reason: Code tags
# 6  
Old 09-21-2010
Hi,

This is not Jim Smilie, as always, it's better to post the input file and the desired output.
# 7  
Old 09-21-2010
Hi Franklin ,

Here is the 1st column of input file

Code:
12344455
12
13
14
15
15
16
12344455

and the variable value i am externally passing say 14

and desired o/p should be

Code:
12344455
12                     13
13                     13
14                     13
15                     13
15                     13
16                     13
12344455

point is 13 should not be printed for 1st and last row
and sorry for the addressing Smilie

---------- Post updated at 12:45 PM ---------- Previous update was at 12:43 PM ----------

Hi ,

correction
variable value is 13

Last edited by Scott; 09-21-2010 at 05:14 PM.. Reason: Code tags

Previous Thread | Next Thread
Test Your Knowledge in Computers #742
Difficulty: Medium
XL Fortran is the name of an open source Fortran compiler for IBM-supported environments, including Linux for big-endian distributions and AIX.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Matching column then append to existing File as new column

Good evening I have the below requirements, as I am not an experts in Linux/Unix and am looking for your ideas how I can do this. I have file called file1 and file2. I need to get the second column which is text1_random_alphabets and find that in file 2, if it's exists then print the 3rd... (4 Replies)
Discussion started by: mychbears
4 Replies

2. UNIX for Dummies Questions & Answers

Solaris - Filter columns in text file and adding new column

Hello, I am very now to this, hope you can help, I am looking into editing a file in Solaris, with dinamic collums (lenght varies) and I need 2 things to be made, the fist is to filter the first column and third column from the file bellow file.txt, and create a new file with the 2 filtered... (8 Replies)
Discussion started by: jpbastos
8 Replies

3. Shell Programming and Scripting

Adding columns with values dependent on existing columns

Hello I have a file as below chr1 start ref alt code1 code2 chr1 18884 C CAAAA 2 0 chr1 135419 TATACA T 2 0 chr1 332045 T TTG 0 2 chr1 453838 T TAC 2 0 chr1 567652 T TG 1 0 chr1 602541 ... (2 Replies)
Discussion started by: plumb_r
2 Replies

4. Shell Programming and Scripting

Adding a new column to an existing one

I have a data file that looks like this: 0.01 1 3822 4.97379915032e-14 4.96982253992e-09 0 0.01 3822 1 4.97379915032e-14 4.96982253992e-09 0 0.01 2 502 0.00993165137406 993.165137406 0 0.01 502 2 0.00993165137406 993.165137406 0 0.01 4 33 0.00189645523539 189.645523539 0 0.01 33 4... (3 Replies)
Discussion started by: kayak
3 Replies

5. Shell Programming and Scripting

Awk to add columns from a file into an existing file

Hi! I would need some help to add the last two columns of one file into another file using awk (or something similar). For example, I have: file 1: file 2: car book day root lag bar look pay boot tag tar took may moot sag I want to have:... (5 Replies)
Discussion started by: coconaza
5 Replies

6. Shell Programming and Scripting

Adding existing set of records in the same file

I have a file with 50,000 records in it, i have a requirement to use the same 50,000 records and add them 4 times to the same file to make a total of 200,000 records. I was wondering how to do this using ksh. Any help is greatly appreciated. (2 Replies)
Discussion started by: vpv0002
2 Replies

7. Shell Programming and Scripting

Adding file to an existing tar

Hi Friends, I want to know the command to add a new file in a existing tar file. For Ex: I have a tar file file1.tar with the contents one.txt two.txt three.txt Now I need to add file four.txt to this existing tar file, how can I do it? Thanks in advance (4 Replies)
Discussion started by: mr_manii
4 Replies

8. Shell Programming and Scripting

how to add a new column in an existing file

Hi guys, Please help me if u have some solution. I have a file with three columns separated by ':' - INPUT_FILE C416722_2 : calin Dirigent : Dirigent AC4174_6 : Jac : cal_co TC4260_5 : [no : lin kite BC426302_1 : [no : calin Dirigent lin JC426540_3 : lin Pymo_bin : calin TC428_3 : no7... (4 Replies)
Discussion started by: sam_2921
4 Replies

9. Shell Programming and Scripting

Need Help for Adding Three new columns in existing file from fatching data from file

not required this time (36 Replies)
Discussion started by: Sandeep_Malik
36 Replies

10. UNIX for Dummies Questions & Answers

Adding header to an existing file

Dear All, I need to add a header of one line to an already existing file. I know that it can be achieved by the following: echo "Header" > newfile cat file1 >> newfile But my problem is that file is huge and there is no space for creating a new file every time. Is there a way that I can... (5 Replies)
Discussion started by: shash
5 Replies

Featured Tech Videos