LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-05-2006, 10:01 AM   #1
mr_scary
Member
 
Registered: Aug 2006
Posts: 39

Rep: Reputation: 15
sed: appending words residing in separate file


I have a file (file1) containing words one per line. I have a second small file (file2) acting as a template containing a single keyword, say, BLAH. I would like to append all the words (lines) in the first file where the keyword is in the second.

So

$cat file1

one;
two;
three;

$cat file2

line1
line2 BLAH
line3

Desired output:

line1
line2 one;two;three;
line3

I figured sed would work but I'm having trouble.
 
Old 10-05-2006, 10:25 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Alternative in Python
Code:
#!/usr/bin/python
data = open("file1.txt").readlines()
file1 = ''.join(data).replace("\n","") #one;two;three;
for lines in open("file2.txt"):
 	lines = lines.strip()
 	if "BLAH" in lines:
 		print lines.replace("BLAH", file1 )
 	else:
 		print lines
Output:
Code:
 
line1
line2 one;two;three;
line3
 
Old 10-05-2006, 01:03 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Or awk
Code:
BEGIN{
  while (getline < "file1.txt" >0){
    cat = cat $0
  }
}

{
   if( $0 ~ /BLAH/ ){gsub( /BLAH/, cat )}; print
}


Cheers,
Tink
 
Old 10-05-2006, 01:25 PM   #4
pete1234
Member
 
Registered: May 2005
Distribution: Slack, FreeBSD,NetBSD, OpenBSD, Open Solaris, Minix
Posts: 172

Rep: Reputation: 30
This seems to work with sed
Code:
#!/bin/bash - 
#

FILE=`cat file1 | tr -d '\n'`
PATTERN=BLAH

sed -i -e "s/$PATTERN/$FILE/" file2

Last edited by pete1234; 10-05-2006 at 01:33 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete text between key words : sed shalomajay Programming 7 05-11-2011 06:22 PM
appending a file circuit_girl Programming 8 10-01-2006 11:00 AM
Need to strip words from front of line. sed/awk/grep? joadoor Linux - Software 6 08-28-2006 04:39 AM
Sed(?); Appending a comma-delineated file ice_hockey Linux - General 2 05-27-2005 08:42 AM
Appending a string to a file JStew Linux - General 13 03-04-2003 03:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration