Adding lines and columns to a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Adding lines and columns to a file
# 1  
Old 07-22-2009
Adding lines and columns to a file

Hi everybody,

I've got two simples file1 like:

Code:
aaa aaa aaa
bbb bbb bbb
ccc ccc ccc

and file2 like:
Code:
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 be the new 4th column for example

I know this question has been asked before and I did find some solutions, but none of them are working for me.

This is what I've got so far:
1) add a new line code:
Code:
sed '1 i\new line' file1 > newfile

Problem: it adds "new line" after each line of my file1, when I need it to be one line only (the first one)

2) add a column code:
Code:
awk '{$4= " awk'{print $3}file2' "}1'

Problem: it also adds filed3 of file2 as the first column of every second line

Any ideas would be greately appretiated!

Many thanks in advance!!!

Last edited by vgersh99; 07-22-2009 at 12:13 PM.. Reason: code tags, PLEASE!
# 2  
Old 07-22-2009
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

---------- Post updated at 11:29 AM ---------- Previous update was at 11:14 AM ----------

Code:
nawk '
  FNR==NR {f2[FNR]=$3;next}
  FNR==1 { print "here is my new line"}
  $NF = $NF OFS f2[FNR]' file2 file1

# 3  
Old 07-22-2009
Many thanks and I apologize for not using code correctly.
Unfotunatelly, nawk is not working. It says: nawk: command not found
# 4  
Old 07-22-2009
then use either awk or gawk if you have one.
# 5  
Old 07-22-2009
Thanks once again!
I've try to run it with wak, but nothing happens. All that's printed is >
Sorry, this may be very basis, but I'm new to UNIX

---------- Post updated at 10:50 AM ---------- Previous update was at 10:46 AM ----------

may be we could simplify the task:
How can I add just another column to an existing file1?
Please
# 6  
Old 07-22-2009
Quote:
Originally Posted by zajtat
Thanks once again!
I've try to run it with wak, but nothing happens. All that's printed is >
Sorry, this may be very basis, but I'm new to UNIX

---------- Post updated at 10:50 AM ---------- Previous update was at 10:46 AM ----------

may be we could simplify the task:
How can I add just another column to an existing file1?
Please
just type what's been posted - assuming your file1 and file2 do exist in the current working directory:
Code:
awk '
  FNR==NR {f2[FNR]=$3;next}
  FNR==1 { print "here is my new line"}' file2 file1
  $NF = $NF OFS f2[FNR]' file2 file1

# 7  
Old 07-22-2009
I'm really sorry to be a pain, but it still doesn't work.
The first two lines of your script retun just "here is my new line" and it doesn't really allow to print the last line. In the end I've amanged to type in the last line as well, but then the old problem happens: it expects more writing >
Sorry...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Help needed: Adding columns in csv file in loop

Hi Everyone: My shell script creates multiple csv files (~30) in for loop. I want to compile (or merge) 3rd column from each (all) of these files to another file (in loop). Please help. Thanks. (3 Replies)
Discussion started by: smap007
3 Replies

3. 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

4. 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

5. Shell Programming and Scripting

Adding new lines to a file + adding suffix to a pattern

I need some help with adding lines to file and substitute a pattern. Ok I have a file: #cat names.txt name: John Doe stationed: 1 name: Michael Sweets stationed: 41 . . . And would like to change it to: name: John Doe employed permanently stationed: 1-office (7 Replies)
Discussion started by: hemo21
7 Replies

6. Shell Programming and Scripting

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... (9 Replies)
Discussion started by: LMHmedchem
9 Replies

7. UNIX for Dummies Questions & Answers

Adding EMPTY columns to Tab-delimited txt file

Hi I have a txt file with 4 columns where I need to add 4 empty columns in the middle meaning that I need what is currently column 4 to be column 8 in a new file. The idea is that I have to use the file as input in a program that reads the data in column 1 and 8, so the content of the other... (8 Replies)
Discussion started by: Banni
8 Replies

8. 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

9. 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

10. Shell Programming and Scripting

Perl: adding columns in CSV file with information in each

Hi Wise UNIX Crew, I want to add 3 different columns to the file in which: 1. The first new column pulls in today's date and time 2. Second column one has a '0' 3. Third column has the word 'ANY' going down the column If my file content is as follows: "7","a","abc",123"... (1 Reply)
Discussion started by: dolo21taf
1 Replies
Login or Register to Ask a Question