split a line of a file and cat a file with another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting split a line of a file and cat a file with another
# 1  
Old 06-21-2010
split a line of a file and cat a file with another

Hi,

I have two files

one.txt
laptop
boy
apple

two.txt
unix
linux
OS
openS

I want to split one.txt into one line each and concatenate it with the two.txt

output files
onea.txt
laptop
unix
linux
OS
openS

oneb.txt
boy
unix
linux
OS
openS

onec.txt
apple
unix
linux
OS
openS

Can this be done with awk?
Any help would be appreciated.
# 2  
Old 06-21-2010
Without Awk ..
Code:
$for line in `cat one.txt`
do
echo "$line" 1> one${i}
cat two.txt >> one${i}
let i+=1
done

# 3  
Old 06-21-2010
Yes:

Code:
awk 'BEGIN {
  n = split("a b c d e f g h i j k l m n o p q r s t u v w x y z", alpha)
  }    
NR == FNR {
  two = two ? two RS $0 : $0; next
  }
FNR == 1 {
  fn = FILENAME; split(fn, FN, ".")
  }
{
  fn = FN[1] alpha[++c] "." FN[2]
  print $0 RS two > fn; close(fn)
  }' two.txt one.txt

You should adjust the code if the number of records of one.txt exceeds the letters in the alphabet Smilie
# 4  
Old 06-21-2010
Hi

Code:
awk '{print > f="file"i++ ; y=system("cat two.txt >>" f);}' one.txt

Guru.
# 5  
Old 06-21-2010
Quote:
Originally Posted by guruprasadpr
Hi

Code:
awk '{print > f="file"i++ ; y=system("cat two.txt >>" f);}' one.txt

Guru.
system command in awk is the key. But I don't know why I can't run it in my env.

Code:
$ awk '{print > f="file"i++ ; y=system("cat two.txt >>" f);}' one.txt
awk: {print > f="file"i++ ; y=system("cat two.txt >>" f);}
awk:           ^ syntax error

I did some adjusts.

Code:
awk '{f="file_" $1 ".txt" ;print >f ; system("cat two.txt >>" f)}' one.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Split a txt file on the basis of line number

I have to split a file containing 100 lines to 5 files say from lines ,1-20 ,21-30 ,31-40 ,51-60 ,61-100 Here is i can do it for 2 file but how to handle it for more than 2 files awk 'NR < 21{ print >> "a"; next } {print >> "b" }' $input_file Please advidse. Thanks (4 Replies)
Discussion started by: abhaydas
4 Replies

2. Shell Programming and Scripting

Search for a pattern in a file and split the line into two lines

Hi All, Greetings everyone !!! I have a file which has many lines, out of which one line is as below. I need to search for pattern "varchar(30) Select" and if exists, then split the line as below. I am trying to achieve this in ksh. Can anyone help me on this. (8 Replies)
Discussion started by: Pradhikshan
8 Replies

3. Shell Programming and Scripting

How to split a file based on pattern line number?

Hi i have requirement like below M <form_name> sdasadasdMklkM D ...... D ..... M form_name> sdasadasdMklkM D ...... D ..... D ...... D ..... M form_name> sdasadasdMklkM D ...... M form_name> sdasadasdMklkM i want split file based on line number by finding... (10 Replies)
Discussion started by: bhaskar v
10 Replies

4. Shell Programming and Scripting

Split line of file from delimeter.

I have a below file. INPUT FILE select * from customer MERGE INTO Archive; delete from Employee; using select * from customer; delete from employee; select * from Employee; insert into employee(1,1); OUTPUT FILE select * from customer MERGE INTO Archive delete from Employee using... (5 Replies)
Discussion started by: Mohin Jain
5 Replies

5. Shell Programming and Scripting

Split csv file by line count.

I have a very large csv file that I sort by the data that is in the second column. But what I need to do next is split the file in groups of say around 30,000 lines but don't split the data while there is still like data in the in the second column. Here is some of the data. ... (2 Replies)
Discussion started by: GroveTuckey
2 Replies

6. Shell Programming and Scripting

Split each column in TSV file to be new line?

My TSV looks like: Hello my name is John \t Hello world \t Have a good day! \t See you later! Is there a simple bash script that splits the tsv on tab to: Hello my name is John Hello world Have a good day! See you later! I'm really stuck, would appreciate any help! (5 Replies)
Discussion started by: pxalpine
5 Replies

7. AIX

Script to cat and dd last line!!! of each file

hi Guys, Am new to this awesome forum, and yea i need some help here asap thnx :) i have a directory with over 34000 text files, i need a script that will delete the last line of each of this file without me necessary opening the files. illustration:- file1 200 records file2 130 records... (5 Replies)
Discussion started by: eetang
5 Replies

8. Shell Programming and Scripting

'for LINE in $(cat file)' breaking at spaces, not just newlines

Hello. I'm making a (hopefully) simple shell script xml parser that outputs a file I can grep for information. I am writing it because I have yet to find a command line utility that can do this. If you know of one, please just stop now and tell me about it. Even better would be one I can input... (10 Replies)
Discussion started by: natedawg1013
10 Replies

9. Shell Programming and Scripting

Split File Based on Line Number Pattern

Hello all. Sorry, I know this question is similar to many others, but I just can seem to put together exactly what I need. My file is tab delimitted and contains approximately 1 million rows. I would like to send lines 1,4,& 7 to a file. Lines 2, 5, & 8 to a second file. Lines 3, 6, & 9 to... (11 Replies)
Discussion started by: shankster
11 Replies

10. Shell Programming and Scripting

Cat'ing a multiple line file to one line

I am writing a script that is running a loop on one file to obtain records from another file. Using egrep, I am finding matching records in file b, then outputing feilds of both into another file. **************************** filea=this.txt fileb=that.txt cat $filea | while read line do... (1 Reply)
Discussion started by: djsal
1 Replies
Login or Register to Ask a Question