Visit Our UNIX and Linux User Community


Perl Text manipulation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Text manipulation
# 1  
Old 07-23-2009
Perl Text manipulation

Hello All,

I have been working on a great script to remotely gather server info and store it in a .txt that can be imported to .xls

I have been reading the hostnames that are in the /.shh/known_hosts file so I don't have to mess with passing a password - via ssh (not easy to do , by the way, but posibble) anyway...

I have been stripping the known_hosts file of the following , trying to get a clean hosts.txt file to use as my CONFIRMED list of servers with exchanged keys.

I have removed the follow:
- ssh key
- extra charaters and extended hostnames
(e.g. myserver vs myserver_backup) So I would remove the "_backup"
- duplicates

So now I have something like the following

Quote:
hostname, 192.168.1.1
hostname2, 192.168.1.2
192.168.1.3, hostname3, hostname3.yahoo.com
hostname4
hostname5, 192.168.1.5
192.168.1.6
So I am looking to manage the text. move all hostnames to the first column and ip's to the 2nd comma separated spot. And move all single column enties to the bottom hostname first then ip's, but that doesn't really matter, just need to know how to manipulate the text, I know your going to say... reg expressions, but I want the easiest way or at least a way I can repeat and reuse for separate tasks.(something I understand)

Quote:
hostname, 192.168.1.1
hostname2, 192.168.1.2
hostname3, 192.168.1.3
hostname5, 192.168.1.5
hostname4
192.168.1.6


I will then create an array and print them out via the array.

Code:
 
"@lines = map({[split /,/]} @lines);"

Thanks
# 2  
Old 08-30-2009
If having a third column in the spreadsheet is acceptable, then you could keep the string "hostname3.yahoo.com" in the file and the solution would be easy to do if you start with an IP checker written in the shell language, some of which is written here: https://www.unix.com/shell-programmin...idation-2.html

Previous Thread | Next Thread
Test Your Knowledge in Computers #158
Difficulty: Easy
The migration of the ARPANET to TCP/IP was officially completed on January 1, 1983, when the new protocols were permanently activated.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Text manipulation

i want to generate a list line-by-line of normal characters using letters . for example : dnds gnos mgod pets jnfp etc... i want to use all letters with all the posibilities is there a script that can do this ? (3 Replies)
Discussion started by: suppliernr1
3 Replies

2. UNIX for Dummies Questions & Answers

Text manipulation help

Hello unix.com I'm having trouble with a text file. It looks like this: Alvaro Costa Daldit Kaur Sings Brian G Heward Desmond Ogilvie John Der William Gherasim Lance Mackey Donald Kopplin Robert Mckinlay Jahir Hussain Mohamed Jack Benaim Abraham Weiss I want... (7 Replies)
Discussion started by: galford
7 Replies

3. UNIX for Dummies Questions & Answers

Text manipulation help

Hello unix.com users, I have a ip file (line-by-line). How can I delete the ips that keep repeating by mark XXX.XXX.XXX.* ... I want to erase only the lines that keep repeating more than 2 times. Example: 1.2.3.1 1.2.3.2 1.2.3.3 I want to erase all ips blocks that are repeating by C... (1 Reply)
Discussion started by: galford
1 Replies

4. UNIX for Dummies Questions & Answers

Text Manipulation Help

Hello Unix.com, I have a text in format: john sara lee How can I make it: john:john john:john1 john:john12 john:john123 sara:sara sara:sara12 sara:sara123 and so on (2 Replies)
Discussion started by: galford
2 Replies

5. UNIX for Dummies Questions & Answers

text manipulation help

Hello again unix.com How can I extract from a large file in format: steve@aol.com steve hawkins Location of this member is bla bla bla sun@hotmail.com Sun Ying This member is using browser bla bla bla to another text in format: steve@aol.com steve hawkins sun@hotmail.com sun ying ... (5 Replies)
Discussion started by: galford
5 Replies

6. Shell Programming and Scripting

Text Manipulation Help

Hello unix.com people! How can I modify a text in format: A:B:C A:B:C A:B:C into C/A/B C/A/B C/A/B Note: Text is line by line and "C", "B", "A" fields are different each row. Thanks in advance. (7 Replies)
Discussion started by: galford
7 Replies

7. Shell Programming and Scripting

text manipulation

Hi All; i need to do text processing : I have a file: file1.txt >>>>>>>>>>>> 30 2 23 some 30 2 22 text 30 2 21 xyz 30 2 20 ttttt 30 2 19 ttttt-1 30 2 18 xryz 30 2 17 xyzr 30 2 16 xy111z 30 2 15 xanyyz 30 2 14 xzz 30 2 13 xyy 30 2 0 zzz-w 50 3 25 zzz-w 50 3 12 productw 50 3 10... (4 Replies)
Discussion started by: unlx
4 Replies

8. Shell Programming and Scripting

Perl Text Manipulation

I'm in need of help for a project that I'm working on. I believe Perl would be the best way of handling the string manipulation, however, I've barely used perl, and I'm used to BASH scripting. Another note is, this project is in a Windows environment, so I can use Perl, but I do not have shell... (1 Reply)
Discussion started by: drewrockshard
1 Replies

9. UNIX for Dummies Questions & Answers

Help with text manipulation

Hi there, I have some text files in unix format that processed by a program in windows, and when I open them with less or vi in linux, a warn for opening binary file is prompted, and as shown in vi, between every two characters there was inserted a "^@". How can I fix this. Plus, there are over... (2 Replies)
Discussion started by: dustinwang2003
2 Replies

10. Shell Programming and Scripting

Text Manipulation.

Hi I have only ever used awk and sed for basic requirements up until now. I have had to break a log down for multiple purposes. Using awk, sed and a date script. I am left with this: (message id, time of msg attempt, message id, domain name, time of msg completion) ... (4 Replies)
Discussion started by: Icepick
4 Replies

Featured Tech Videos