how to keep tab from being converted to space


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to keep tab from being converted to space
# 8  
Old 08-23-2012
Its depend on what you want to achieve.
Please provide some more lines of your input.
# 9  
Old 08-23-2012
the file is like this
Code:
# Nmap 5.51 scan initiated Wed Aug 22 18:42:27 2012 as: nmap -sS -P0 -O -sV -oN log -oX log.xml -oG log.grep -append-output 74.82.42.42
Host: 74.82.42.42 (ordns.he.net)        Status: Up
Host: 74.82.42.42 (ordns.he.net)        Ports: 22/open/tcp//tcpwrapped///, 53/open/tcp//domain//PowerDNS 3.3/, 179/open/tcp//tcpwrapped///      Ignored State: closed (997)       Seq Index: 196
# Nmap done at Wed Aug 22 18:42:40 2012 -- 1 IP address (1 host up) scanned in 13.12 seconds
# Nmap 5.51 scan initiated Wed Aug 22 18:42:40 2012 as: nmap -sS -P0 -O -sV -oN log -oX log.xml -oG log.grep -append-output 72.52.104.74
Host: 72.52.104.74 (tserv1.fmt2.he.net) Status: Up
Host: 72.52.104.74 (tserv1.fmt2.he.net) Ports: 22/open/tcp//tcpwrapped///, 53/open/tcp//domain//PowerDNS 3.3/, 179/open/tcp//tcpwrapped///      Ignored State: closed (997)     Seq Index: 207  IP ID Seq: All zeros
# Nmap done at Wed Aug 22 18:43:00 2012 -- 1 IP address (1 host up) scanned in 19.48 seconds
.................
.................


so there are many lines like the 3rd and the 7th lines
there are many fields in these lines, separated by '\t'
and I want to extract the fields and put them into different log files.

for example, in hosts.log, I will have
Code:
22/open/tcp//tcpwrapped///
53/open/tcp//domain//PowerDNS 3.3/
179/open/tcp//tcpwrapped/// 
 22/open/tcp//tcpwrapped///
53/open/tcp//domain//PowerDNS 3.3/
179/open/tcp//tcpwrapped///
....
....

hosts.log is special coz there are sub-fields separated by ','

in Seq_Index.log, I will have
Code:
196
207
...
...

in ip_id.log,I will have
Code:
All Zeros
...
...


Quote:
Originally Posted by pamu
Its depend on what you want to achieve.
Please provide some more lines of your input.
# 10  
Old 08-23-2012
Try this...
Let me know if you want any other output...

Code:
awk -F "\t" '{ for(i=1;i<=NF;i++) { if ( $i ~ /Ports:/ ) { gsub(/Ports:/,"",$i); print $i >> "hosts.log" } else { if ( $i ~ /Seq Index:/ ) { gsub(/Seq Index:/,"",$i); { print $i >> "sq_index.log"  } } else { if ( $i ~ /IP ID Seq:/ ) { gsub(/IP ID Seq:/,"",$i); { print $i >> "ip_id.log" }} }   }}}' file2

# 11  
Old 08-23-2012
Quote:
Originally Posted by esolvepolito
the file is like this
Code:
# Nmap 5.51 scan initiated Wed Aug 22 18:42:27 2012 as: nmap -sS -P0 -O -sV -oN log -oX log.xml -oG log.grep -append-output 74.82.42.42
Host: 74.82.42.42 (ordns.he.net)        Status: Up
Host: 74.82.42.42 (ordns.he.net)        Ports: 22/open/tcp//tcpwrapped///, 53/open/tcp//domain//PowerDNS 3.3/, 179/open/tcp//tcpwrapped///      Ignored State: closed (997)       Seq Index: 196
# Nmap done at Wed Aug 22 18:42:40 2012 -- 1 IP address (1 host up) scanned in 13.12 seconds
# Nmap 5.51 scan initiated Wed Aug 22 18:42:40 2012 as: nmap -sS -P0 -O -sV -oN log -oX log.xml -oG log.grep -append-output 72.52.104.74
Host: 72.52.104.74 (tserv1.fmt2.he.net) Status: Up
Host: 72.52.104.74 (tserv1.fmt2.he.net) Ports: 22/open/tcp//tcpwrapped///, 53/open/tcp//domain//PowerDNS 3.3/, 179/open/tcp//tcpwrapped///      Ignored State: closed (997)     Seq Index: 207  IP ID Seq: All zeros
# Nmap done at Wed Aug 22 18:43:00 2012 -- 1 IP address (1 host up) scanned in 19.48 seconds
.................
.................


so there are many lines like the 3rd and the 7th lines
there are many fields in these lines, separated by '\t'
and I want to extract the fields and put them into different log files.

for example, in hosts.log, I will have
Code:
22/open/tcp//tcpwrapped///
53/open/tcp//domain//PowerDNS 3.3/
179/open/tcp//tcpwrapped/// 
 22/open/tcp//tcpwrapped///
53/open/tcp//domain//PowerDNS 3.3/
179/open/tcp//tcpwrapped///
....
....

hosts.log is special coz there are sub-fields separated by ','

in Seq_Index.log, I will have
Code:
196
207
...
...

in ip_id.log,I will have
Code:
All Zeros
...
...

Your input sample above has spaces instead of tabs, but I think I changed appropriate spaces to tab characters to produce a test file.

Above you say that hosts.log has subfields separated by <comma>, but the only field name that contains comma separated subfields is the field that starts with "Ports:". Assuming you meant Ports instead of Hosts, I think the following script will do what you said you want:
Code:
awk -F "\t" '/^#/       {next}
        {
        for(i=1; i<=NF; i++) {
                sfn=split($i, sf, /[:,] /);
                for(j=1; j<=sfn; j++) {
                        if(j == 1) {
                                gsub(/ /, "_", sf[1])
                                continue
                        }
                        print sf[j] >> sf[1]
                }
        }
}' in.fields2logfiles

Note that this code doesn't care what the field names are in your input, it will determine what they are on the fly. For each field name it finds (either at the start of a line or after a tab up to the first <colon><space> after that) it creates a file with the name found (after changing any spaces to underscores). It will produce separate lines in the log files for each <comma><space> separated subfield it finds no matter what the field name is. With the input shown above, this script produces the log files: Host, IP_ID_Seq, Ignored_State, Ports, Seq_Index, and Status.

It isn't obvious to me that these log files are going to be useful since there is no time stamp on any of the entries and there is no way to connect entries in any of these files (except Host) to the name of host associated with the entry. Maybe you want to capture some data from some of the comment lines to include at the beginning of each line printed to the log files, or maybe you don't want a Host file, but instead want to start each line in the remaining log files with the name from the Host: field at the start of each non-comment line.

Note also that if there is a large number of different field names (each of which produces a different log file), awk may run out of file descriptors. If that is an issue, there are several ways it can be alleviated, but the script will run slower since it may need to close and reopen output files frequently.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace space by tAB

My file looks like 3 33 210.01.10.0 2.1 1211 560 26 45 1298 98763451112 15412323499 INPUT OK 3 233 40.01.10.0 2.1 1451 780 54 99 1876 78787878784 15423210199 CANCEL OK Aim is to replace the spaces in each line by tab Used: sed -e 's/ */\t/g' But I get output like this... (3 Replies)
Discussion started by: sa@@
3 Replies

2. Shell Programming and Scripting

How to remove tab space if any in a variable?

I have a variable sumOfJEOutputFile which is the output file of an SQL command which contains the output of that SQL. The output looks like below: ----------- 58 I am using following code to manipulate the output: (sed 1,2d $sumOfJEOutputFile > $newTemp1 | sed '$d' $newTemp1)... (4 Replies)
Discussion started by: Sharma331
4 Replies

3. UNIX for Dummies Questions & Answers

Changing only the first space to a tab in a space delimited text file

Hi, I have a space delimited text file but I only want to change the first space to a tab and keep the rest of the spaces intact. How do I go about doing that? Thanks! (3 Replies)
Discussion started by: evelibertine
3 Replies

4. UNIX for Dummies Questions & Answers

Delimiter: Tab or Space?

Hello, Is there a direct command to check if the delimiter in your file is a tab or a space? And how can they be converted from one to another. Thanks, G (4 Replies)
Discussion started by: Gussifinknottle
4 Replies

5. Shell Programming and Scripting

So I converted columns to rows but I want it to be tab delimited and also I want.....

Hi, So my file looks like this: title number JR 2 JR 2 JR 4 JR 5 NM 5 NM 8 NM 2 NM 8 I used this line that I wrote to convert it to rows so it will look like this: awk -F"\t" '!/^$/{a=a" "$3} END {for ( i in a) {print i,a}}' occ_output.tab > test.txt JR 2 2 4 5 NM 5 8... (4 Replies)
Discussion started by: kylle345
4 Replies

6. Shell Programming and Scripting

Awk to find space and tab.

Wants to print line when there exist leading or trailing space or tab in fields 2,3 and 5 The below code prints all lines in file even if they dont have leading and trailing space or tab. nawk -F"|" '{for(i=1;i<=NF;i++) {if ($i ~ "^*" || $i ~ "*$")}}1' file file Ouput required: ... (5 Replies)
Discussion started by: pinnacle
5 Replies

7. Shell Programming and Scripting

need help in tab space !

i have a commad that display the total each directory size in KB.Below the commad and o/p: ls -ltr | grep ^d | awk '{print $9}' | xargs du -sk output: what i want is the proper tab space b/w value and dir.? how to get that. thanks in advance (10 Replies)
Discussion started by: ali560045
10 Replies

8. Shell Programming and Scripting

Consecutive spaces within input being converted to single space

I'm reading from a file that is semi-colon delimited. One of the fields contains 2 spaces separating the first and last name (4th field in - "JOHN<space><space> DOE"): e.g. TORONTO;ONTARIO;1 YONGE STREET;JOHN DOE;CANADA When I read this record and either echo/print to screen or write to... (4 Replies)
Discussion started by: NinersFan
4 Replies

9. Shell Programming and Scripting

How to echo a <tab> space?

I've tried this: echo "${bold}User${norm} : u"\t"${bold}All Users ${norm} : a\t" and i got this output: Specific User : u\tAll User: a\t (14 Replies)
Discussion started by: laila63
14 Replies

10. UNIX Desktop Questions & Answers

replace tab with space

How do I replace a tab with a space in scripts using sed/awk ? (1 Reply)
Discussion started by: avnerht
1 Replies
Login or Register to Ask a Question