prepend columns to a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers prepend columns to a file
# 1  
Old 10-22-2008
prepend columns to a file

I have two files. File 'a' has contents:

1|1
2|2
3|3
4|4

and file 'b' has contents:

abc|def
hij|klm
nop|qrs
tuv|wxy

I would like to prepend file 'a' to file 'b' (with pipe) such that the contents of 'b' will be:

1|1|abc|def
2|2|hij|klm
3|3|nop|qrs
4|4|tuv|wxy

Thanks,

- CB
# 2  
Old 10-22-2008
Code:
awk ' FILENAME=="file1" {arr[FNR]=$0}
       FILENAME=="file2"  {print arr[FNR] "|" $0" }
      ' file1 file2 > newfile
      mv newfile file2

# 3  
Old 10-22-2008
Hammer & Screwdriver What about paste?

Code:
> cat file271
file 271 line 1
file 271 line 2
> cat file272
file 272 line 1
file 272 line 2
> paste -d"|" file271 file272
file 271 line 1|file 272 line 1
file 271 line 2|file 272 line 2

# 4  
Old 10-22-2008
Thanks for your help guys....

- CB
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find header in a text file and prepend it to all lines until another header is found

I've been struggling with this one for quite a while and cannot seem to find a solution for this find/replace scenario. Perhaps I'm getting rusty. I have a file that contains a number of metrics (exactly 3 fields per line) from a few appliances that are collected in parallel. To identify the... (3 Replies)
Discussion started by: verdepollo
3 Replies

2. Shell Programming and Scripting

Gnu tool; sed awk echo etc to prepend or append string to a file

Looking to add text to a file, example File example; nodegroups: check-hosts: L@host.domain.com,host2.domain.com,host3.domain.com I need to take a file with a one line list of hosts separated by commas host.domain.com,host2.domain.com,host3.domain.comand prepend the string " ... (6 Replies)
Discussion started by: bash_in_my_head
6 Replies

3. Shell Programming and Scripting

How to prepend filename to its content without a third file?

Hello, Is possibly there a way to prepend the filename to its content without a third file? The reason is to add a header to each file contents to distinguish each other when they are pasted side-by-side. sample.txt: XLOC_001 0 XLOC_002 23 XLOC_003 4 XLOC_012 6output (with the same... (4 Replies)
Discussion started by: yifangt
4 Replies

4. Shell Programming and Scripting

Deleting all the fields(columns) from a .csv file if all rows in that columns are blanks

Hi Friends, I have come across some files where some of the columns don not have data. Key, Data1,Data2,Data3,Data4,Data5 A,5,6,,10,, A,3,4,,3,, B,1,,4,5,, B,2,,3,4,, If we see the above data on Data5 column do not have any row got filled. So remove only that column(Here Data5) and... (4 Replies)
Discussion started by: ks_reddy
4 Replies

5. Shell Programming and Scripting

prepend timestamp to continiously updating log file

Hi, I have a process which outputs to a log. Below is the code snippet: process &> $LOGFILE& The log file keeps on updating whenever a transaction is processed. The log file has a time stamp added so every time I kill the process and start the process a new log file is... (4 Replies)
Discussion started by: rajkumarme_1
4 Replies

6. Shell Programming and Scripting

Prepend TimeStamp to STDERR & STDOUT to a file

Currently I am redirecting STDERR and STDOUT to a log file by doing the following { My KSH script contents } 2>&1 | $DEBUGLOG Problem is the STDERR & STDOUT do not have any date/time associated. I want this to be something that i can embed into a script opposed to an argument I use... (4 Replies)
Discussion started by: nitrobass24
4 Replies

7. UNIX for Advanced & Expert Users

Help in Deleting columns and Renaming Mutliple columns in a .Csv File

Hi All, i have a .Csv file in the below format startTime, endTime, delta, gName, rName, rNumber, m2239max, m2239min, m2239avg, m100016509avg, m100019240max, metric3min, m100019240avg, propValues 11-Mar-2012 00:00:00, 11-Mar-2012 00:05:00, 300.0, vma3550a, a-1_CPU Index<1>, 200237463, 0.0,... (9 Replies)
Discussion started by: mahi_mayu069
9 Replies

8. Shell Programming and Scripting

Append and Prepend Text to a file list

I want to print out a directory listing, then append ] to the end of each line. I'm trying to create a list of Wiki links based on folder listings that i can just copy and paste without having to edit 100's of file listings. Using sed i've figured out to do something like this: sed... (4 Replies)
Discussion started by: CapnDoody
4 Replies

9. Shell Programming and Scripting

Prepend content of a file to another file

How can i prepend the content of a file to another file? I tried: sed '1r textfile' myfile but it inserted the text AFTER the first line? What would be a good method? I can easily append lines to a file using the ">>" operator, is the something similar in bash to prepend lines? Kind regards... (2 Replies)
Discussion started by: Christoph Spohr
2 Replies
Login or Register to Ask a Question