awk split lines without knowing the number of fields a-priori


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk split lines without knowing the number of fields a-priori
# 1  
Old 08-22-2012
awk split lines without knowing the number of fields a-priori

I want to use awk to split fields and put them into a file
but I don't know the number of fields
for example, in the following line

Code:
Ports: 22/filtered/tcp//ssh///, 53/open/tcp//tcpwrapped///, 111/filtered/tcp//rpcbind///, 543/filtered/tcp//klogin///, 544/filtered/tcp//kshell///, 1002/filtered/tcp//windows-icfw///, 1556/open/tcp//veritas_pbx?///, 2105/filtered/tcp//eklogin///, 2200/filtered/tcp//ici///, 8081/filtered/tcp//blackice-icecap///, 12000/open/tcp//patrol//BMC Patrol Agent/, 13722/filtered/tcp//netbackup///, 13782/filtered/tcp//netbackup///, 13783/filtered/tcp//netbackup///, 50000/filtered/tcp//ibm-db2///, 50001/filtered/tcp//unknown///, 50002/filtered/tcp//iiimsf///, 50003/filtered/tcp//unknown///

I want to put each port information into a file
I want to get a file like

Code:
22/filtered/tcp//ssh///
53/open/tcp//tcpwrapped///
111/filtered/tcp//rpcbind///
543/filtered/tcp//klogin///
....
....

how to achieve this?
thanks
# 2  
Old 08-22-2012
Code:
awk '{sub(/^Ports: */,"");n=split($0,a,/ *, */);for(i=1;i<=n;i++) print a[i] > "Portsdata.txt"}' file

# 3  
Old 08-22-2012
Try this one :

Code:
$ awk '{sub("Ports: ",""); gsub(", ","\n");print > "outfile"}' infile

# 4  
Old 08-22-2012
With sed:
Code:
sed 's/^Ports: *//;s/ *, */\
/g' file > Portsdata

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 add plus or minus to fields and split another field

In the tab-delimited input below I am trying to use awk to -10 from $2 and +10 to $3. Something like awk -F'\t' -v OFS='\t' -v s=10 '{split($4,a,":"); print $1,$2-s,$3+s,a,$5,$6} | awk {split(a,b,"-"); print $1,$2-s,$3+s,b-s,b+s,$5,$6}' input should do that. I also need to -10 from $4... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Awk: Combine multiple lines based on number of fields

If a file has following kind of data, comma delimited 1,2,3,4 1 1 1,2,3,4 1,2 2 2,3,4 My required output must have only 4 columns with comma delimited 1,2,3,4 111,2,3,4 1,222,3,4 I have tried many awk command using ORS="" but couldnt progress (10 Replies)
Discussion started by: mdkm
10 Replies

3. UNIX for Dummies Questions & Answers

Split file based on number of blank lines

Hello All , I have a file which needs to split based on the blank lines Name ABC Address London Age 32 (4 blank new line) Name DEF Address London Age 30 (4 blank new line) Name DEF Address London (8 Replies)
Discussion started by: Pratik4891
8 Replies

4. Shell Programming and Scripting

awk to split one field and print the last two fields within the split part.

Hello; I have a file consists of 4 columns separated by tab. The problem is the third fields. Some of the them are very long but can be split by the vertical bar "|". Also some of them do not contain the string "UniProt", but I could ignore it at this moment, and sort the file afterwards. Here is... (5 Replies)
Discussion started by: yifangt
5 Replies

5. UNIX for Dummies Questions & Answers

Command to split the files based on the number of lines in it

Hello Friends, Can anyone help me for the below requirement. I am having a file called Input.txt. My requirement is first check the count that is wc -l input.txt If the result of the wc -l Input.txt is less than 10 then don't split the Input.txt file. Where as if Input.txt >= 10 the split... (12 Replies)
Discussion started by: malaya kumar
12 Replies

6. Shell Programming and Scripting

AWK:Split fields separated by semicolon

Hi all, I have a .vcf file which contains 8 coulmns and the data under each column as shown below, CHROM POS ID REF ALT QUAL FILTER INFO 1 3000012 . A G 126 ... (6 Replies)
Discussion started by: mehar
6 Replies

7. Shell Programming and Scripting

How to (n)awk lines of CSV with certain number of fields?

I have a CSV file with a variable number of fields per record. How do I print lines of a certain number of fields only? Several permutations of the following (including the use of escape characters) have failed to retrieve the line I'm after (1,2,3,4)... $ cat myfile 1,2,3,4 1,2,3 $ # Print... (1 Reply)
Discussion started by: cs03dmj
1 Replies

8. Shell Programming and Scripting

Script to split files based on number of lines

I am getting a few gzip files into a folder by doing ftp to another server. Once I get them I move them to another location .But before that I need to make sure each gzip is not more than 5000 lines and split it up . The files I get are anywhere from 500 lines to 10000 lines in them and is in gzip... (4 Replies)
Discussion started by: gubbu
4 Replies

9. Shell Programming and Scripting

Scripting help: Split a file into equal number of lines.

Experts, I have a file datafile.txt that consists of 1732 Line, I want to split the file into equal number of lines with 10 file. (The last file can have 2 line extra to match 1732) Please advise how to do that, Thanks in advance.. (2 Replies)
Discussion started by: rveri
2 Replies

10. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 Replies
Login or Register to Ask a Question