Visit Our UNIX and Linux User Community


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
# 8  
Old 07-22-2009
Quote:
Originally Posted by zajtat
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...
once again - check that you have copy/pasted the code - make sure you have an opening and a closing single quotes.
# 9  
Old 07-22-2009
Many thanks for your suggestions, but I've tried it now several times typing and also cut/pasted it as well. I do use single quotes and still it doesn't work... IS that OK that awk command has one opening single quote and two closing ones?
# 10  
Old 07-22-2009
Quote:
Originally Posted by zajtat
Many thanks for your suggestions, but I've tried it now several times typing and also cut/pasted it as well. I do use single quotes and still it doesn't work... IS that OK that awk command has one opening single quote and two closing ones?
no, it's is not ok and I don't see it in the original post either.
# 11  
Old 07-22-2009
Here's your original script:

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

If I'm not mistaken the first single quote is before the first "file2 file1" (third line);
and the second quote is before the last "file2 file1" (forth line)

May be I misunderstood something?

Many thanks for your help!

I need to go now, but will check the forum tomorrow...
# 12  
Old 07-22-2009
Quote:
Originally Posted by zajtat
Here's your original script:

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

If I'm not mistaken the first single quote is before the first "file2 file1" (third line);
and the second quote is before the last "file2 file1" (forth line)

May be I misunderstood something?

Many thanks for your help!

I need to go now, but will check the forum tomorrow...
no, this is not 'my' original script - reread the original post.
This is what in the original post:
Code:
nawk '
  FNR==NR {f2[FNR]=$3;next}
  FNR==1 { print "here is my new line"}
  $NF = $NF OFS f2[FNR]' file2 file1

# 13  
Old 07-23-2009
Yes this is true, but then I wrote that nawk doesn't work for me and you wrote another code with awk (the one that I've cut and pasted).
# 14  
Old 07-23-2009
Quote:
Originally Posted by zajtat
Yes this is true, but then I wrote that nawk doesn't work for me and you wrote another code with awk (the one that I've cut and pasted).
heh - could you try:
Code:
awk '
  FNR==NR {f2[FNR]=$3;next}
  FNR==1 { print "here is my new line"}
  $NF = $NF OFS f2[FNR]' file2 file1

 

Previous Thread | Next Thread
Test Your Knowledge in Computers #855
Difficulty: Medium
JSX (JavaScript XML) is an extension to the JavaScript language syntax.
True or False?

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

Featured Tech Videos