Script that appends to a single line?

Kasyx

Expert Member
Joined
Jun 6, 2006
Messages
2,565
I have a file (call it id.txt) with multiple IDs (each on a new line):

e.g:

Code:
127262
172628
918272
817267
916287

What I need to do is format these for horrible kludgy use in a query, thus I need the following output:

Code:
127262 or 172628 or 918272 or 817267 or 916287

How would I go about doing this?
I assume something like:

Code:
for i in $( cat id.txt); do
(something with sed) > output.txt
done

Could anyone give me a hand with this? My sed-fu is weak indeed.
 

sn3rd

Expert Member
Joined
Jan 18, 2008
Messages
4,305
Hideous mockup code:
Code:
#!/usr/bin/env perl
use strict;
use warnings;

if ($#ARGV + 1 != 1)
{
        print "Usage: longlines <filename>\n";
        exit;
}
open(my $input, "<", $ARGV[0]) or die($!);

while(<$input>)
{
        chomp;
        print $_ . " ";
}

modify as desired.
 

dabean

Expert Member
Joined
Feb 24, 2004
Messages
1,663
You could just use echo.

Code:
for i in $( cat id.txt); do
 echo -n "$i or " >> output.txt
done

Edit: That'll leave a ' or ' after the last item though, it can be done without the loop in sed.
Code:
sed -e :a -e '$!N;s/\n/ or /;ta' id.txt > output.txt
 
Last edited:

Kasyx

Expert Member
Joined
Jun 6, 2006
Messages
2,565
You could just use echo.

Code:
for i in $( cat id.txt); do
 echo -n "$i or " >> output.txt
done

Edit: That'll leave a ' or ' after the last item though, it can be done without the loop in sed.
Code:
sed -e :a -e '$!N;s/\n/ or /;ta' id.txt > output.txt

Yeah, but I need it all on a single line. Echo >> appends a new line for each iteration.
 
Top