0

A text file (file name: 1.txt):

Incoming_Queries_A:     13201096
Incoming_Queries_A6:    946
Incoming_Queries_AAAA:  1288191
Incoming_Queries_ANY:   31280
Incoming_Queries_AXFR:  5
Incoming_Queries_CNAME: 410
Incoming_Queries_DS:    20
Incoming_Queries_MX:    854
Incoming_Queries_NS:    97217
Incoming_Queries_PTR:   1011409
Incoming_Queries_SOA:   5006
Incoming_Queries_SPF:   1
Incoming_Queries_SRV:   3555
Incoming_Queries_TXT:   511
Incoming_Requests_IQUERY:       11
Incoming_Requests_NOTIFY:       1
Incoming_Requests_QUERY:        15640501
Incoming_Requests_STATUS:       1
Incoming_Requests_UPDATE:       5

I want to remove all strings before tab (include tab) in a line of text and print the output(example: 13201096 ) to standard out.

Example:

# egrep -i "Incoming_Queries_A:" ./1.txt | sed  's/.Incoming_Queries_A:\t//'

Output:

Incoming_Queries_A:     13201096

But I only want to output 13201096

How to fix it? thanks

Clinton
  • 33
  • 2
  • 6
  • You should use `awk` for column filtering. – Zulu Sep 22 '14 at 13:45
  • possible duplicate of [awk - how to delete first column with field separator](http://stackoverflow.com/questions/16448153/awk-how-to-delete-first-column-with-field-separator) – fedorqui Sep 22 '14 at 14:24

4 Answers4

2

Since you only need the second column, you can use cut:

cut -f2 file.txt
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
2

sed doesn't automatically handle escaped chars (such as \t and \n) on bash. You can handle it in two different ways:

  • You can replace the \t by an actual tab in your expression. To hit a tab in the terminal, you do Control-V, then hit the TAB key: Ctrl-V and then Tab.

  • (this one seems far more elegant, IMO) you can force sed to interpret your \t, by placing a $ before your substitution string. This way, your command would be like:

    egrep -i "Incoming_Queries_A:" ./1.txt | sed $'s/Incoming_Queries_A:\t//'
    

(I removed that . before Incoming_Queries_A: - probably a typo/desperate tentative)

Hope that helps.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Fernando Aires
  • 532
  • 2
  • 7
0

With awkyou can do this like that:

awk '{print $2}' 1.txt
13201096
946
1288191
31280
5
410
20
854
97217
1011409
5006
1
3555
511
11
1
15640501
1
5

Or awk '/Incoming_Queries_A:/ {print $2}' /tmp/t.txt for get only the line you wanted

Zulu
  • 8,765
  • 9
  • 49
  • 56
0

You could try the below GNU sed command,

sed -r 's/^.*\t//' file
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • 2
    Your current solution is not very clear, because tabs and spaces don't look different. Also, if there were many columns, you would remove all of them but the last one. I would go for something more general like `sed 's/^[^\t]*\t//' file`. Also, adding some explanation would be useful. – fedorqui Sep 22 '14 at 14:28
  • It worked to me on `sed (GNU sed) 4.2.2`. I see your update: nice, then it is good to indicate so in your answer. – fedorqui Sep 22 '14 at 14:31
  • 1
    @fedorqui your comment here is most complete answer for question in title! – josifoski Sep 22 '14 at 14:52