Multiple lines combined into one for netstat (or ls)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple lines combined into one for netstat (or ls)
# 1  
Old 05-27-2010
Multiple lines combined into one for netstat (or ls)

Hello

So I understand when I do the following

Code:
used_ports=$(netstat -nat |cut -d : -f 2 |cut -d ' ' -f 1)

that the output will look like this

Code:
Active Proto 5298 22 631 55012 56093 39672 43196 56619 39677 36103 
38453 41413 56137 37902 41410 41414 43195 38426 49253 38420 34273 
49241 56124 38433 34935 56121 49255 38451 45457 58900 49240 34274 
45535 54558 49242 41411 36104 38452 49254 59107

But I actually want to then grep for individual ports -- impossible if all are on the same line. I need to make sure that 10 or 15 consecutive ports I need are not in use. easy to do if I call netstat 10-15 times and grep for each port seperately, but that seems expensive. Is there a way to store the netstat output and then ensure the ports I need are not in use?

I actually have similar problems with ls occasionally, so explaining why the output gets stuck on a single line, and then how to work with it in general would be useful to me.
# 2  
Old 05-27-2010
For one, quotes might prove helpful:

Code:
[house@leonov] used_ports=$( netstat -nat | cut -d: -f2 | cut -d' ' -f1 ); echo $used_ports
Active Proto 60224 2049 42344 40461 815 111 631 [...]

[house@leonov] used_ports=$( netstat -nat | cut -d: -f2 | cut -d' ' -f1 ); echo "$used_ports"
Active
Proto
60224
2049
42344
40461
815
111
631
[...]

Alternatively, "extended grep-ing" would be an option:

Code:
[house@leonov] echo $used_ports | grep -o -E '(111|815) '
815
111


Last edited by dr.house; 05-27-2010 at 01:00 PM.. Reason: 2nd part added
This User Gave Thanks to dr.house For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing carriage returns from multiple lines in multiple files of different number of columns

Hello Gurus, I have a multiple pipe separated files which have records going over multiple Lines. End of line separator is \n and records going over multiple lines have <CR> as separator. below is example from one file. 1|ABC DEF|100|10 2|PQ RS T|200|20 3| UVWXYZ|300|30 4| GHIJKL|400|40... (7 Replies)
Discussion started by: dJHa
7 Replies

2. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies

3. Shell Programming and Scripting

Merging multiple lines to columns with awk, while inserting commas for missing lines

Hello all, I have a large csv file where there are four types of rows I need to merge into one row per person, where there is a column for each possible code / type of row, even if that code/row isn't there for that person. In the csv, a person may be listed from one to four times... (9 Replies)
Discussion started by: RalphNY
9 Replies

4. Shell Programming and Scripting

Reading multiple values from multiple lines and columns and setting them to unique variables.

Hello, I would like to ask for help with csh script. An example of an input in .txt file is below, the number of lines varies from file to file and I have 2 or 3 columns with values. I would like to read all the values (probably one by one) and set them to independent unique variables that... (7 Replies)
Discussion started by: FMMOLA
7 Replies

5. Shell Programming and Scripting

Awk match multiple columns in multiple lines in single file

Hi, Input 7488 7389 chr1.fa chr1.fa 3546 9887 chr5.fa chr9.fa 7387 7898 chrX.fa chr3.fa 7488 7389 chr21.fa chr3.fa 7488 7389 chr1.fa chr1.fa 3546 9887 chr9.fa chr5.fa 7898 7387 chrX.fa chr3.fa Desired Output 7488 7389 chr1.fa chr1.fa 2 3546 9887 chr5.fa chr9.fa 2... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

6. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

7. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

8. Shell Programming and Scripting

Combined Two CSV Lines

I have two CSV lines, I.e.: Line 1 = the,quick,brown,fox, ,jumps, ,the, ,dog Line 2 = the,quick,brown,fox, , ,over, ,lazy,dog Literally, columns missing from line 1 exist in line 2. Any suggestions on quick ways to combined these two lines into one line: New line:... (2 Replies)
Discussion started by: msf004
2 Replies

9. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

10. Shell Programming and Scripting

Find multiple patterns on multiple lines and concatenate output

I'm trying to parse COBOL code to combine variables into one string. I have two variable names that get literals moved into them and I'd like to use sed, awk, or similar to find these lines and combine the variables into the final component. These variable names are always VAR1 and VAR2. For... (8 Replies)
Discussion started by: wilg0005
8 Replies
Login or Register to Ask a Question