Appending lines from an existing list to each line in another existing list


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Appending lines from an existing list to each line in another existing list
# 1  
Old 09-10-2011
Appending lines from an existing list to each line in another existing list

Evening all !

I would like to ask your expertise on how to accomplish the following ;

I have 2 lists, and would like each line from list2 to be appended to each line in list1, resulting in list3 ;

List1;
Code:
alpha
beta
charlie

List2;
Code:
one
two
three

List3;
Code:
alphaone
alphatwo
alphathree
betaone
betatwo
betathree
charlieone
charlietwo
charliethree


Could some of you kind folks give me an idea on how to approach this in
bash with sed or awk ?

Any assistance greatly appreciated !

Thanks - TAPE
# 2  
Old 09-10-2011
Code:
awk > list3 'NR == FNR {
  l2[FNR] = $0
  fnr = FNR; next
  }
{
  for (i = 0; ++i <= fnr;)
    print $0 l2[i]
  }' list2 list1

This User Gave Thanks to radoulov For This Post:
# 3  
Old 09-10-2011
Heya radoulov, many thanks for your prompt response.

Tried it out and works great, thanks for the great answer !

Have to admit a lot of what is written there is waaaay out of my comfort zone,
is this the best / fastest way to do this with large lists ?
Any other ways possible that are easily shown ? (just out of curiosity Smilie )

In any case thanks for the answer, I am a happy man Smilie

Cheers - TAPE
# 4  
Old 09-10-2011
Well,
I don't see a better approach right now.
The code builds and associative array in memory with the content of list2, then it simply prints that list for every line in list1: it runs nested loops.

Last edited by radoulov; 09-11-2011 at 04:21 AM..
# 5  
Old 09-10-2011
Well it is great and works well, thanks again for your time and expertise Smilie

Much appreciated !
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding non-existing words in a list of files in a directory and its sub-directories

Hi All, I have a list of words (these are actually a list of database table names separated by comma). Now, I want to find only the non-existing list of words in the *.java files of current directory and/or its sub-directories. Sample list of words:... (8 Replies)
Discussion started by: Bhanu Dhulipudi
8 Replies

2. Shell Programming and Scripting

Build a table from a list by comparing existing table entries

I am new to this shell scripting.... I have a file which contains list of users. This files get updated when new user comes into the system. I want to create script which will give a table containing unique list of users. When I say unique, it means script should match table while parsing... (3 Replies)
Discussion started by: dchavan1901
3 Replies

3. Shell Programming and Scripting

Appending new column to existing files

Hi, i want to add another column to existing files containing strings and need to have the final output as a csv file. i have quite a number of files, each with varying number of rows and i need to append the string "test" for all the valid rows for each file. my sample raw files looks like this... (8 Replies)
Discussion started by: ida1215
8 Replies

4. Shell Programming and Scripting

Append to existing line

I have a file which has lines that end with a plus (+) sign. I would like to get the next line appended to the one with the plus. For example bla bla bla bla bla + blip blip blip would become bla bla bla bla bla blip blip blip However not all lines end with a plus sign . I would... (2 Replies)
Discussion started by: bombcan
2 Replies

5. Shell Programming and Scripting

Appending a CPIO to an existing archive

I created a CPIO archive I wanted to add addition data to it but am having issues: -rw-r--r-- 1 test test 629295104 2011-10-28 12:41 /home/test/Downloads/test.cpio I tried: sudo find /tmp -depth | cpio -oAO /home/test/Downloads/test.cpio cpio: premature end of file and (1 Reply)
Discussion started by: metallica1973
1 Replies

6. Shell Programming and Scripting

insert pipes for existing and non-existing records

I have a source file like this, L4058S462 34329094 F51010141TK1070000483L4058S462 34329094 0232384840 381892 182 5690 L4058S462 34329094 F51020141FIRST CLEARING, LLC A/C 3432-9094 L4058S462 34329094 F51030141JOHAN HOLMQVIST ... (1 Reply)
Discussion started by: saravanamr
1 Replies

7. Solaris

Add existing user into an existing group

Pre: no gpasswd/adduser there is just usermod can be used, also there is no -a option for usermod. How should I add a user into a group? (4 Replies)
Discussion started by: a2156z
4 Replies

8. Shell Programming and Scripting

how to insert text between lines of an existing file using perl

Hi , I need some inputs on how to open a file (file.txt) and parse the text example aaa of the file and bbb of the file and add the text zzzz once i parse (aaa and bbb) and followed by the remaining of the text as it is in the file using perl programming. Thanks in advance (3 Replies)
Discussion started by: madhul2002
3 Replies

9. Shell Programming and Scripting

folder existing and file existing

I want to look into a folder to see if there are any folders within it. If there are, I need to check inside each folder to see if it contains a .pdf file So If /myserver/myfolder/ contains a folder AND that folder conatins a .pdf file do X Else do Z I may have multiple folders and... (4 Replies)
Discussion started by: crowman
4 Replies

10. Shell Programming and Scripting

Add Multiple Lines in an existing file

Hi Unix Experts, I like to add the following multiple lines in an existing file: CHANNELS: MaxChannels=600 MaxActiveChannels=600 MaxInitiators=22 I would be highly appreciated if somebody can shed some light on it. Thank you in advance!!! Khan (5 Replies)
Discussion started by: hkhan12
5 Replies
Login or Register to Ask a Question