Print various rows in one row


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print various rows in one row
# 1  
Old 06-19-2013
Print various rows in one row

I have this in a file

Code:
[server.domain.com]
11.22.33.44
yyyyyyuser

With awk/sed, I need this to be output as follows

Code:
alias server.domain.com='ssh yyyyyyuser@11.22.33.44'

# 2  
Old 06-19-2013
anil510,
Check this out:

Code:
$ cat file |sed 's/\[//g;s/\]//g'|xargs|while read a b c;do printf "alias $a=\'ssh $c@$b\'\n";done
alias server.domain.com='ssh yyyyyyuser@11.22.33.44'

Enjoy!,
# 3  
Old 06-19-2013
Try also
Code:
awk '{gsub (/[][]/,""); print "alias "$1"=\047ssh "$3"@"$2"\047"}' RS="" file
alias server.domain.com='ssh yyyyyyuser@11.22.33.44'

This User Gave Thanks to RudiC For This Post:
# 4  
Old 06-19-2013
Rudic, it works.

rveri, Your solution works if there only one server.
# 5  
Old 06-19-2013
Using Perl, file022 :
Code:
[server.domain.com]
11.22.33.44
yyyyyyuser
[server2.domain.com]
55.66.77.88
zzzzzzuser

program file022.pl :
Code:
#!/usr/bin/perl -w
use strict;

my $cur_dir = $ENV{PWD};
my $filename = "$cur_dir/$ARGV[0]";
my ($record,$srv,$ip);
my $cpt=1;

open(FILEIN,"<$filename") or die"open: $!";
while( defined( $record = <FILEIN> ) ) {
  chomp $record;

  if($cpt == 1) {
    ($srv) = $record =~ m/(\w+\.\w+\.\w+)/ ;
  }
  elsif($cpt == 2) {
    $ip=$record;
  }
  else {
    print "alias $srv='ssh $record\@$ip'\n";
  }

  $cpt++;
  $cpt=1 if($cpt == 4);
}
close(FILEIN);

execution :
Code:
%./file022.pl file022
alias server.domain.com='ssh yyyyyyuser@11.22.33.44'
alias server2.domain.com='ssh zzzzzzuser@55.66.77.88'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reseting row count every given number of rows

I have a file with 48 rows. I am counting 6 rows and adding 6 to that number and repeating the operation, and then output the value in column 1. For the second column, I would like to get sort of a binary output (1s and 2s) every 3rd row. This is what I have: awk '{print ++src +... (1 Reply)
Discussion started by: Xterra
1 Replies

2. Shell Programming and Scripting

Splitting single row into multiple rows based on for every 10 digits of last field of the row

Hi ALL, We have requirement in a file, i have multiple rows. Example below: Input file rows 01,1,102319,0,0,70,26,U,1,331,000000113200000011920000001212 01,1,102319,0,1,80,20,U,1,241,00000059420000006021 I need my output file should be as mentioned below. Last field should split for... (4 Replies)
Discussion started by: kotra
4 Replies

3. Shell Programming and Scripting

Print row on 4th column to all row

Dear All, I have input : SEG901 5173 9005 5740 SEG902 5227 5284 SEG903 5284 5346 SEG904 5346 9010 SEG905 5400 5456 SEG906 5456 5511 SEG907 5511 9011 SEG908 5572 9015 SEG909 5622 9020 SEG910 5678 5739 SEG911 5739 5796 SEG912 5796 9025 ... (3 Replies)
Discussion started by: attila
3 Replies

4. Shell Programming and Scripting

Get row number from file1 and print that row of file2

Hi. How can we print those rows of file2 which are mentioned in file1. first character of file1 is a row number.. for eg file1 1:abc 3:ghi 6:pqr file2 a abc b def c ghi d jkl e mno f pqr ... (6 Replies)
Discussion started by: Abhiraj Singh
6 Replies

5. Shell Programming and Scripting

complicated row to rows conversion

I have a file containing rows with the following format. Field1|Field2|Field3|data1:data data2:data data3:"dataA:data dataB:data" data4:data:data (and so on) I need to format the above row into multiple rows that look like this: Field1|Field2|Field3|data1|data ... (2 Replies)
Discussion started by: newreverie
2 Replies

6. UNIX for Dummies Questions & Answers

search one row and print next 9 rows

Hi, I'd like your support on this, my file looks like this: 25 50 200 10 0 1 10 6 12 17 26 24 30 319 10 -0.83 3.31 270.646 12.48 -163.04 30.38 0 0 0 -2.88 0.64 16.06 34.4 33.08 34.4 33.08 34.06 20.48 6.22 -33.39 33.96 -33.5840381 -71.6177759 -163.26 25.59 34.1 -28.8 -5.6 -9.9 0 0 0 0... (3 Replies)
Discussion started by: Gery
3 Replies

7. UNIX for Advanced & Expert Users

Converting rows to a single row

Hi all I have a file as below : Development System User Production i want to convert the file to below format: "Development","System","User","Production" Is it possible with UNIX ? if so can you please give me some direction on it ? Thanks, Satya Use code tags please, ty. (10 Replies)
Discussion started by: satyaranjon
10 Replies

8. UNIX for Advanced & Expert Users

convert rows to single row

Hi I want to convert multiple rows ro single row ,I have tried with below one but I am not getting what I am expecting.Please any idea a.txt conn1=stg conn2=dev path=\xxx\a1.txt fre=a conn1=stg conn2=dev path=\xxx\a2.txt freq=a awk '/a/{ORS=" "}{print}END{print "\n"}'... (5 Replies)
Discussion started by: akil
5 Replies

9. Shell Programming and Scripting

Insert rows with computations of next row

Hello folks, I have data collected in every 3 hours. But, I would like to expand this to 1 hour interval by equally dividing with next row. For example, I want to keep the first value 1987-01-01-00z 2.0, but following all record should be re-written as follow. 1987-01-01-03z 5.0 becomes... (11 Replies)
Discussion started by: Jae
11 Replies

10. Shell Programming and Scripting

Concatenate 2 rows into 1 row

I need to search a file for two values (valueA & valueB). ValueA will be on a different row than valueB, and concatenate the two together on the same row of my output. Example: search input file for strings "node" and "OS", combine the two results into one row input node A text text OS... (4 Replies)
Discussion started by: indianadoug
4 Replies
Login or Register to Ask a Question