Suggestions for adding columns to text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Suggestions for adding columns to text file
# 1  
Old 03-27-2010
Suggestions for adding columns to text file

Good afternoon to everyone,

I have some input and output from various widgets that I am trying to get to play nicely together. Basically I would like to stay out of excel and be able to automate the entire process. I have read some posts here about how to use awk, nawk, etc, to do similar operations, but nothing quite the same.

One thing I need to do is to add a leading column with "index" as the value for the first row, and then sequentially number the remaining rows. I know how to number all rows in sed, but not how to skip the first row. I also need to remove column three at some later point, etc.

I think if I can get these two operations working, I can adapt for the rest.

Thanks a bunch for any suggestions, I think these operations are simple, but many things are simple once you know how.

...not so much otherwise Image

LMHmedchem
# 2  
Old 03-27-2010
Based on your description where file1 has the original columns
Code:
sed -n '2,$p' file1 | awk '{ print NR" "$0 }' > file2

# 3  
Old 03-27-2010
Code:
awk '
  NR == 1 { printf "index", $0; next }
          { print NR - 1, $0 }
' "$file"

# 4  
Old 03-27-2010
Hello, LMHmedchem:

Welcome to the forum. In the future, please provide some sample of the data being worked with (feel free to obfuscate sensitive info, so long as the format isn't affected). With nothing to go on, here's my shot in the dark (i've assumed the data is a comma-delimited file):
Code:
awk 'NR==1{print "index,"$0} NR>1{print NR-1","$0}' data

Regards,
Alister

Last edited by alister; 03-27-2010 at 07:26 PM.. Reason: fix off by one bug
# 5  
Old 03-27-2010
Code:
awk '$0=((NR-1)?NR-1:"index")","$0'  infile


Last edited by danmero; 03-27-2010 at 06:07 PM.. Reason: Shorter ;)
# 6  
Old 03-27-2010
Thanks for the responses so far,

This works for adding the index column,
Code:
   awk '$0=((NR-1)?NR-1:"index")"\t"$0'  $INFILE > $OUTFILE

This is a tab delimited file, so I made that change.

The input looks like,
Code:
name   input1   input2   input3   input4   input5
1972257   16.1762   3.38945   56.1511   58.7836   6.71878
2803235   46.5833   8.39445   28.0059   47.72   6.21068
17957224   32.5622   6.57671   40.5624   47.5545   7.44461
17957228   32.5622   6.57671   40.6967   47.5268   7.44241

and for the first transformation, I need
Code:
index    name   ID   input1   input2   input3   input4   input5
1     1972257   1   16.1762   3.38945   56.1511   58.7836   6.71878
2      2803235   2   46.5833   8.39445   28.0059   47.72   6.21068
3      17957224   3   32.5622   6.57671   40.5624   47.5545   7.44461
4      17957228   4   32.5622   6.57671   40.6967   47.5268   7.44241

The code above gets me the index column, for the third column I tried,
Code:
   awk '$2=((NR-1)?NR-1:"ID")"\t"$2'  $INFILE > $OUTFILE

but that definitely didn't work.

LMHmedchem
# 7  
Old 03-27-2010
AWK:
Code:
awk 'BEGIN{OFS="\t"} {$2=(NR>1?NR-1:"ID")"\t"$2; print (NR>1?NR-1:"index"),$0}' data

SED (each red, underscored area is one literal tab, which can be inserted at the command line by typing control-v control-i or control-v tabkey):
Code:
sed -n 'p;=' data | sed '$d; 1{s/____/____ID____/; s/^/index____/;n;}; N; y/\n/____/; s/\(\([^____]*\)____[^____]*____\)/\1\2____/;'

Regards,
Alister

Last edited by alister; 03-27-2010 at 08:41 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help in adding text before columns in shell script

Hello, Can someone please help in below requirement. My requirement is to add date before to first column,some text before 1st,2nd coulmns and insert a new column in between 2 and 3 columns. input file. aa 123 dddd aa 667 kdkdk ddj 738 kkkk aa 123 dddd aa 667 ... (5 Replies)
Discussion started by: Cva2568
5 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. UNIX for Dummies Questions & Answers

Perl - adding columns to file

I have a file in which I need to add more columns to based on a key in the first file: File1 key1,abc,123, key2,def,456, key3,ghi,789, File2 key2,zyx,111,qqq, key3,yuu,222,www, key1,pui,333,eee, key4,xxx,999,rrr, I would like to create the following output: Output (1 Reply)
Discussion started by: WongSifu
1 Replies

4. UNIX for Dummies Questions & Answers

Removing columns from a text file that do not have any values in second and third columns

I have a text file that has three columns. But at the end of the text file, there are trailing lines that have missing second and third columns: 4 0.04972604 KLHL28 4 0.0497332 CSTB 4 0.04979822 AIF1 4 0.04983331 DECR2 4 0.04990344 KATNB1 4 4 4 4 How can I remove the trailing... (3 Replies)
Discussion started by: evelibertine
3 Replies

5. Shell Programming and Scripting

Unix File - Adding columns in the middle

Hello, I have a comma separated flat file. It contains some 20 columns. I want to add two new columns at position 2,3. So that file will have 22 columns. I am providing here sample data with file having 4 columns. Appreciate your help in finding solution for this. data in input file:... (11 Replies)
Discussion started by: ravi.videla
11 Replies

6. Shell Programming and Scripting

Adding a new column in a file with other existing columns

Hi All , Kindly help me with this soln 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 (8 Replies)
Discussion started by: Pratik4891
8 Replies

7. Shell Programming and Scripting

[bash help]Adding multiple lines of text into a specific spot into a text file

I am attempting to insert multiple lines of text into a specific place in a text file based on the lines above or below it. For example, Here is a portion of a zone file. IN NS ns1.domain.tld. IN NS ns2.domain.tld. IN ... (2 Replies)
Discussion started by: cdn_humbucker
2 Replies

8. UNIX for Dummies Questions & Answers

Adding lines and columns to a file

Hi everybody, I've got two simples file1 like: aaa aaa aaa bbb bbb bbb ccc ccc ccc and file2 like: 111 111 111 222 222 222 333 333 333 I need to: 1) add a line say "new line" as the first line of the file 2)add a column from file2 (say column3) to file1; the new column should... (14 Replies)
Discussion started by: zajtat
14 Replies

9. Shell Programming and Scripting

Adding specific text and spaces to each line in a text file

Hi, I wanted to add specific text to each row in a text file containing three rows. Example: 0 8 7 6 5 5 7 8 9 0 7 9 7 8 9 0 1 2 And I want to add a 21 at the beginning of the first row, and blank spaces at the beginning of the second two rows. To get this: 21 0 8 7 6 5 5 7 8... (4 Replies)
Discussion started by: hertingm
4 Replies

10. UNIX for Dummies Questions & Answers

Adding columns to a file

I want to select the first column from a daily file called foo.csv. The result is written to file foo.txt. Currently the following script is used for that: cut -d, -f 1 foo.csv > foo.txt A typical result would yield : A12 A45 B11 B67 What needs to happen in addition is that two columns... (5 Replies)
Discussion started by: figaro
5 Replies
Login or Register to Ask a Question