using sed to write horizontally & sequentially in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers using sed to write horizontally & sequentially in a file
# 1  
Old 10-10-2009
using sed to write horizontally & sequentially in a file

Hey there

I have two commands to get exactly the data i want but.. i want to write them into a file side by side and in the same order so that they always match. So what i'm hoping to learn from this thread is some of the different ways one could take the output of grepped data and write them in a file next to each other vs appending it starting from the last entry.

Code:
*1010|15:32%cat testfile | grep serverinfo.pl
                        <A HREF=/server/serverinfo.pl?server_ip=99.99.237.76&lite=1>ser-ver14057.su.net</A>
                        <A HREF=/server/serverinfo.pl?server_ip=99.99.148.23&lite=1>ser-ver.sterling.com</A>
                        <A HREF=/server/serverinfo.pl?server_ip=99.99.64.179&lite=1>ser-ver.center.com</A>
                        <A HREF=/server/serverinfo.pl?server_ip=99.99.228.171&lite=1>ser-ver00001.su.net</A>
                        <A HREF=/server/serverinfo.pl?server_ip=99.99.181.80&lite=1>ser-ver35062.su.net</A>
                        <A HREF=/server/serverinfo.pl?server_ip=99.99.111.48&lite=1>ser-ver35220.su.net</A>
                        <A HREF=/server/serverinfo.pl?server_ip=99.99.111.50&lite=1>ser-ver35222.su.net</A>
                        <A HREF=/server/serverinfo.pl?server_ip=99.99.111.51&lite=1>ser-ver35223.su.net</A>
                        <A HREF=/server/serverinfo.pl?server_ip=99.99.111.49&lite=1>ser-ver35221.su.net</A>
                        <A HREF=/server/serverinfo.pl?server_ip=99.99.179.43&lite=1>ser-ver00010.su.net</A>
                        <A HREF=/server/serverinfo.pl?server_ip=99.99.127.211&lite=1>ser-ver00061.su.net</A>
                        <A HREF=/server/serverinfo.pl?server_ip=99.99.148.83&lite=1>ser-ver.web.com</A>
*1010|15:41%cat testfile | grep serverinfo.pl | sed 's/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}//' | cut -c 52- | sed 's/\<\/A\>//'
ser-ver14057.su.net
ser-ver.sterling.com
ser-ver.center.com
ser-ver00001.su.net
ser-ver35062.su.net
ser-ver35220.su.net
ser-ver35222.su.net
ser-ver35223.su.net
ser-ver35221.su.net
ser-ver00010.su.net
ser-ver00061.su.net
ser-ver.web.com
*1010|15:41%cat testfile | grep serverinfo.pl | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'
99.99.237.76
99.99.148.23
99.99.64.179
99.99.228.171
99.99.181.80
99.99.111.48
99.99.111.50
99.99.111.51
99.99.111.49
99.99.179.43
99.99.127.211
99.99.148.83
*1010|15:41%


Desired output...

Quote:
cat testfileoutput 99.99.237.76
ser-ver14057.su.net 99.99.148.23
ser-ver.sterling.com 99.99.64.179
ser-ver.center.com 99.99.228.171
ser-ver00001.su.net 99.99.181.80
ser-ver35062.su.net 99.99.111.48
ser-ver35220.su.net 99.99.111.50
ser-ver35222.su.net 99.99.111.51
ser-ver35223.su.net 99.99.111.49
ser-ver35221.su.net 99.99.179.43
ser-ver00010.su.net 99.99.127.211
ser-ver00061.su.net 99.99.148.83
ser-ver.web.com
# 2  
Old 10-10-2009
Hi.

I was just about to post an answer in sed then I saw your odd requirement, so I cobbled together some awk to fix it.

Code:
sed "s/.*ip=\(.*\)&.*\(ser.*\)<.*/\2 \1/" file1 | awk '
NR == 1 { X = $1; printf( "%s\n", $2 ); next }
{ printf( "%s %s\n", X, $2); X = $1 }
END { print X }
'
 
99.99.237.76
ser-ver14057.su.net 99.99.148.23
ser-ver.sterling.com 99.99.64.179
ser-ver.center.com 99.99.228.171
ser-ver00001.su.net 99.99.181.80
ser-ver35062.su.net 99.99.111.48
ser-ver35220.su.net 99.99.111.50
ser-ver35222.su.net 99.99.111.51
ser-ver35223.su.net 99.99.111.49
ser-ver35221.su.net 99.99.179.43
ser-ver00010.su.net 99.99.127.211
ser-ver00061.su.net 99.99.148.83
ser-ver.web.com

# 3  
Old 10-10-2009
whoops looks like i made a mistake on my example output.. just drop those ip's down one level.

scottn thanks for the quick response.. only one problem.. i think yours assumes the server prefix will begin with the same and it doesn't in my case. Sorry for not mentioning that earlier.

I think my real question is if i run one command and output that to a file, what could i add to the second command to redirect the output to the file in the manner that i need. Whatever it is, it should not care about what data is already in the file but that when it writes to it, it does so from the first line on down adding a space then the data. So everything is presumed correct based on the order for which the information was gathered to begin with.

So lets say i..


$run command a > file ; cat file
fooa
foob
fooc

$run command b (some command here to take output like so below) ; cat file
fooa foo1
foob foo2
fooc foo3

Last edited by phpfreak; 10-10-2009 at 07:28 PM.. Reason: addition
# 4  
Old 10-10-2009
Hi.

Assuming you have your data in files already:

Code:
paste -d" " infile1 infile2

where infile1 is the sec..... and infile2 is the 99.99.... stuff.

Or
Code:
cat infile2 | paste -d" " infile1 -

where "cat infile2" is anything which generates the 99.99.... stuff

Last edited by Scott; 10-10-2009 at 07:43 PM..
# 5  
Old 10-10-2009
scottn that works nicely, despite the fact that i have to generate 3 files it checks out and gets the job done... now i'm sure there is a way either through sed or awk to do all of the work in memory but eh.. i'll save it for later.. if anyone else see's this feel free to post if you think you know of a better method

THANKS AGAIN SCOTTN Smilie
# 6  
Old 10-10-2009
Hi

Code:
sed "s/.*ip=\(.*\)&.*>\([a-z].*\)<.*/\2 \1/" input_file > output_file
 
ser-ver14057.su.net 99.99.237.76
ser-ver.sterling.com 99.99.148.23
ser-ver.center.com 99.99.64.179
ser-ver00001.su.net 99.99.228.171
ser-ver35062.su.net 99.99.181.80
ser-ver35220.su.net 99.99.111.48
ser-ver35222.su.net 99.99.111.50
ser-ver35223.su.net 99.99.111.51
ser-ver35221.su.net 99.99.111.49
ser-ver00010.su.net 99.99.179.43
ser-ver00061.su.net 99.99.127.211
ser-ver.web.com 99.99.148.83

This is crying out for perl!!

As I'm only on page 6 of the O'Reilly book, that'll have to wait.

This is maybe the most bizarre thing you'll see all day:

Code:
awk -F"server_ip=" '{print $2}' file1 | awk -F"&lite=1>" '{sub("</A>", ""); print $2, $1}'

ser-ver14057.su.net 99.99.237.76
ser-ver.sterling.com 99.99.148.23
ser-ver.center.com 99.99.64.179
ser-ver00001.su.net 99.99.228.171
ser-ver35062.su.net 99.99.181.80
ser-ver35220.su.net 99.99.111.48
ser-ver35222.su.net 99.99.111.50
ser-ver35223.su.net 99.99.111.51
ser-ver35221.su.net 99.99.111.49
ser-ver00010.su.net 99.99.179.43
ser-ver00061.su.net 99.99.127.211
ser-ver.web.com 99.99.148.83


Last edited by Scott; 10-10-2009 at 09:27 PM..
# 7  
Old 10-15-2009
Quote:
This is crying out for perl!!
hehe, i couldn't agree with more. thanks again
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed will not write to same file

I am a scripting noob, and having much trouble getting this script to run correctly. Upon start it should read the home directory and sort the files into a text file. Then have user enter a user name and insert it into line 1 of the same text file. Then have the date insert into the second line of... (8 Replies)
Discussion started by: bbaumg02
8 Replies

2. Shell Programming and Scripting

Perl extract number from file & write to file

I have 1 file that has elements as follows. Also the CVR(10) and the word "SAUCE" only appear once in the file so maybe a grep command would work? file1 CVR( 9) = 0.385E+05, ! VEHICLE CVR(10) = 0.246E+05, ! SAUCE CVR(11) = 0.162E+03, ! VEHICLE I need to extract the... (6 Replies)
Discussion started by: austinj
6 Replies

3. Shell Programming and Scripting

Finding BEGINNING & ENDING positions of sequentially increasing sublists from a perl numeric array

I have got an Perl array like: @array = (1,2,3,4,5,6,1,2,3,4,1,2,1,2,3,4,5,6,7,8,9...............) This numeric sequence will be always sequentially increasing, unless it encounters, The beginning of the new sequentially increasing numeric sequence. SO in this array we get sequentially... (5 Replies)
Discussion started by: teknokid1
5 Replies

4. Shell Programming and Scripting

How to write If statement using && and operator in Unix

Hi What is the syntax for if statement using && and || operator? if && ] || here its giving me an error to this if statement any suggestion?? (2 Replies)
Discussion started by: Avi
2 Replies

5. Shell Programming and Scripting

Extract data from an XML file & write into a CSV file

Hi All, I am having an XML tag like: <detail sim_ser_no_1="898407109001000090" imsi_1="452070001000090"> <security>ADM1=????</security> <security>PIN1=????</security> <security>PIN2=????</security> ... (2 Replies)
Discussion started by: ss_ss
2 Replies

6. Shell Programming and Scripting

PHP read large string & split in multidimensional arrays & assign fieldnames & write into MYSQL

Hi, I hope the title does not scare people to look into this thread but it describes roughly what I'm trying to do. I need a solution in PHP. I'm a programming beginner, so it might be that the approach to solve this, might be easier to solve with an other approach of someone else, so if you... (0 Replies)
Discussion started by: lowmaster
0 Replies

7. Shell Programming and Scripting

Extract string from a file & write to a new file (Perl)

Hi, This is the first time playing around with perl and need some help. Assuming if i have a line of text that looks like this: Date/Time=Nov 18 17:12:11;Device Name=192.168.1.1;Device IP=192.168.1.1;Device Class=IDS;Source IP=155.212.212.111;Source Name=UNKNOWN;Source Port=1679... (3 Replies)
Discussion started by: LuckyGuy
3 Replies

8. Shell Programming and Scripting

Need help in reading a file horizontally and printing vertically

Hi Every body, I have file which has enttries, with each 5 entries as a set of entries, I would like to read the file (line by line) and print five entries of a set vertically, the next entry should come in the next line. Example: cat sample_file I am a Unix Adminsitrator new to shell... (6 Replies)
Discussion started by: aruveiv
6 Replies

9. Shell Programming and Scripting

sed to read and write to very same file

This is likely to be a dumb one. How can I use sed to substitute string occurances having it read from an input file and write to this very same file ? I have a file with lots of occurances of '2006', I want to change it to '2007', but I'd like these changes to be saved on the input file. ... (5 Replies)
Discussion started by: 435 Gavea
5 Replies

10. UNIX for Advanced & Expert Users

Merging Two File Horizontally

I am trying to merge two large file horizontally using paste command. Every thing is working fine except for time. Its taking lot of time. Is there any effiecient way of doing the same thing or is there anyway by which I can improve its perfomance programatically? Thanks, Yeheya (1 Reply)
Discussion started by: yeheyaansari
1 Replies
Login or Register to Ask a Question