Combine two lines


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Combine two lines
# 1  
Old 10-29-2002
Question Combine two lines

I have a file called test.txt. Inside this file is the following:

tcenh100.telkom.co.za
100.200.300.400

tcenh101.telkom.co.za
500.600.700.800

I want to take out the new lines and spaces, then I want to put the ip address of the host name next to the host name on the same line, as soon as this is done, I want to switch around the ip address and host name with a space between them. The file must look as follows after script is done running:

100.200.300.400 tcenh100.telkom.co.za
500.600.700.800 tcenh101.telkom.co.za

Anyone can help please?

Thanks
WolfSmilie
# 2  
Old 10-29-2002
Code:
#!/usr/bin/perl
while ($line = <>) {
    chomp ($line);

   if ($line ne "") {

     if ($line =~ m/telkom/) {
         $hold = $line;
     } else {
         print $line, " ";
         print $hold, "\n";
     }
   }
}

There you go! Save it in a file called "lineconvert", then run it like this:
Code:
lineconvert test.txt > outputfile

Smilie
# 3  
Old 10-29-2002
Dang! WIntellect beat me to the punch. Oh well, I'm gonna post my sed script anyway...
Code:
#! /usr/bin/sed -f
#
N         ;# suck in a second line
N         ;# suck in a third line
s/\n/ /g  ;# change new-line to space
s/  */ /g ;# change many spaces to one space
s/\([^ ]*\) \([^ ]*\)/\2 \1 / ;# swap the fields

# 4  
Old 10-29-2002
Hey -

I liked this post - I got it on my lunch break at work; it proved to be a bit of fun! Sorta like doing the crossword in the newspaper - only better!

Can't stand crosswords!

Smilie
# 5  
Old 10-29-2002
Re: Combine two lines

wolf wants to turn this:
Code:
tcenh100.telkom.co.za
100.200.300.400

tcenh101.telkom.co.za
500.600.700.800

into this:
Code:
100.200.300.400 tcenh100.telkom.co.za
500.600.700.800 tcenh101.telkom.co.za

Here are two versions, similar to WIntellect's but more concise:

Code:
#!/bin/awk

/telkom/                         { hold = $1 ; next}
/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/ { print $1 " " hold }

The concise perl version is similar:
Code:
#!/usr/bin/perl -lan
/telkom/          && do { $hold = $F[0]; next };
/\d+(?:\.\d+){3}/ && do { print $F[0], " ", $hold };

In both, the "next" is an efficicency hack and only needed if your file is really, really, really big.
# 6  
Old 10-29-2002
Re: Re: Combine two lines

Quote:
Originally posted by criglerj
The concise perl version is similar:
Code:
#!/usr/bin/perl -lan
/telkom/          && do { $hold = $F[0]; next };
/\d+(?:\.\d+){3}/ && do { print $F[0], " ", $hold };

Concise... <gasp> Perl code... <gasp> Hurt brain! Smilie
# 7  
Old 10-30-2002
Re: Re: Re: Combine two lines

Quote:
Originally posted by auswipe


Concise... <gasp> Perl code... <gasp> Hurt brain! Smilie
Smilie But it's about the same thing as the awk version, so an awk user can work it out from there.

How about a Bourne shell version, using expr. I have no idea how portable expr stuff is.
Code:
#!/bin/sh
while read word rest ; do
  if [ `expr "$word" : '.*\.telkom\.'` -gt 0 ] ; then
    hostname=$word
  elif [ `expr "$word" : '^[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*$'` -gt 0 ] ; then
    echo "$word $hostname"
  fi
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to remove lines that do not start with digit and combine line or lines

I have been searching and trying to come up with an awk that will perform the following on a converted text file (original is a pdf). 1. Since the first two lines are (begin with) text they are removed 2. if $1 is a number then all text is merged (combined) into one line until the next... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

awk to combine lines if fields match in lines

In the awk below, what I am attempting to do is check each line in the tab-delimeted input, which has ~20 lines in it, for a keyword SVTYPE=Fusion. If the keyword is found I am splitting $3 using the . (dot) and reading the portion before and after the dot in an array a. If it does have that... (12 Replies)
Discussion started by: cmccabe
12 Replies

3. UNIX for Dummies Questions & Answers

Combine lines in file

Hi All, I am trying to understand if its possible to carry out the following. I have a text file which contains output from multiple commands, within the file a node will be quiered twice if there was 2 commands for example. Is it possible do combine 2 lines into 1 if the first word is the... (1 Reply)
Discussion started by: mutley2202
1 Replies

4. Shell Programming and Scripting

Combine 2 lines

All, i am new to linux script... source Filter: vlan281-BUM-5M BUM-5M 0 0 Filter: vlan282-BUM-5M BUM-5M 0 0 Filter: vlan2828-BUM-5M Filter:... (2 Replies)
Discussion started by: samoptimus
2 Replies

5. UNIX for Dummies Questions & Answers

how can I combine 2 lines into one?

Hi, I have a file like this: A 1 B 2 C 3 ... And I want to have just one line like this: A 1 B 2 C 3 ... How can I do it? Thanks! (4 Replies)
Discussion started by: elsagarcia
4 Replies

6. Shell Programming and Scripting

combine 2 lines

Hello, I want to combine 2 lines in one I have a text file example: bla123 blo31 xx:yy:zz ->bla43 bli532 00:01:02 bla1237 blo351 aa:ss:dd ->bla433 bli34332 55:10:28 I want the result to be: bla123 blo31 xx:yy:zz, ->bla43 bli532 00:01:02 bla1237 blo351 aa:ss:dd, ->bla433 bli34332... (3 Replies)
Discussion started by: Petko Meshov
3 Replies

7. Shell Programming and Scripting

How to combine lines?

Hi, I have a file like this: "sdfc@abc.com","arovls","some addr ", "more stuff" "ssss@email.com","arovls","some addr", "sss" "edx@email.com","arovls","some addr", "sssdfvv" "ssss@a55.com","arovls","some addr", "lsdsdgf" "ssss@0234.com","aro vls","123 Main", "lSdfv" I want to... (4 Replies)
Discussion started by: erniel
4 Replies

8. UNIX for Dummies Questions & Answers

How do I combine the last 2 lines of a file

I tried to put the history line number and the date into the file with one command, and failed. Can't figure out how to get the date variable substituted for the last space captured. history | tail -1 | sed -e 's/.\{7\}/&/g' | head -1 | sed 's/ $/$date/' Result was: 729 $date So, I... (8 Replies)
Discussion started by: jimbob75
8 Replies

9. Shell Programming and Scripting

combine 2 lines

Moderator, kindly delete this thread because I already found what I needed... thanks. (0 Replies)
Discussion started by: Deanne
0 Replies

10. UNIX for Dummies Questions & Answers

Combine two lines

Hi I have a file with the records 1 A B C D 2 E F G H 3 I J K L 4 M N O P In the ouput I want 1 A B C D 2 # F G H 3 I J K L 4 M N O P How to achieve this? (10 Replies)
Discussion started by: superprg
10 Replies
Login or Register to Ask a Question