LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-01-2015, 01:18 AM   #1
linux_project
LQ Newbie
 
Registered: Apr 2015
Posts: 11

Rep: Reputation: Disabled
not getting desired output with awk


I am not getting desired output with below awk command. I want to cut column number 4 using awk with below but it is not fetching the right column. As per my knowledge it should pick one or spaces in a same way . But while Pasting my file content over here . I find *(stars) that i cannot see in vi and notepad++. Any idea for the issue ? How can i find in future as well




2 *ip * * * * *0x12 * * * *10.153.98.246 * * 129.144.130.130 * * * 6666 * * * * 514 * * *539583 * 9.72
3 *ip * * * * *0x06 * * * *10.227.108.31 * * 10.198.184.4 * * * * * * 216 * * * 48719 * * *531099 * 9.57
4 *ip * * * * *0x08 * * * *10.217.97.237 * * 10.198.180.84 * * * * * *216 * * * 64942 * * *333256 * 6.00%
5 *ip * * * * *0x06 * * * *10.226.230.218 * *10.198.184.4 * * * * * * 216 * * * 50049 * * *307549 * 5.54%
6 *ip * * * * *0x32 * * * *160.34.15.60 * * *216.131.130.12 * * * * 46972 * * * 34501 * * *232317 * 4.18%
7 *ip * * * * *0x06 * * * *10.227.48.133 * * 10.198.184.4 * * * * * * 216 * * * 62924 * * *187629 * 3.38%
8 *ip * * * * *0x06 * * * *10.227.93.49 * * *10.198.184.4 * * * * * * 216 * * * 42644 * * *180628 * 3.25%
9 *ip * * * * *0x32 * * * *160.34.31.60 * * *216.131.130.12 * * * * 46972 * * * 34503 * * *172353 * 3.10%
10 *ip * * * * *0x32 * * * *160.34.47.60 * * *216.131.130.12 * * * * 46972 * * * 34516 * * * 90544 * 1.63%



[root@test ~]# cat /tmp/ip | awk -F " " '{print $4}'

10.153.98.246
10.227.108.31
10.217.97.237
10.226.230.218
160.34.15.60 *
10.227.48.133
10.227.93.49 *
160.34.31.60 *
160.34.47.60

Last edited by linux_project; 08-01-2015 at 01:22 AM.
 
Old 08-01-2015, 04:33 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Well my first question back would be, are you showing us the same file you are using??

When i use the data shown and your awk, all i get is 9 lines of asterisks:
Code:
$ awk -F " " '{print $4}' file
*
*
*
*
*
*
*
*
*
So I would need to know how you get any of the ip addresses you are showing as I do not.

I am also not following the, "I find *(stars) that i cannot see in vi and notepad++"??
 
Old 08-02-2015, 01:08 AM   #3
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
I am sure there is a preprocess (prior programme) which you have not mentioned, where the sequence of one or more stars is replaced by a space and the output has been put in /tmp/ip Also you have pasted the original file (input to the prior programme).

OK
 
Old 08-02-2015, 05:20 AM   #4
hickmz
LQ Newbie
 
Registered: Aug 2015
Posts: 1

Rep: Reputation: Disabled
Hi I have been trying 4 hours now i have some text as follows HASH####7hAZ1BoLTM#### all i want is the 7hAZ1BoLTM i have tried countless variations but i either get nothing or the whole line just prints again not just the test that i want
awk '/####/{a=1};a;/####/{exit}' test.txt
 
Old 08-02-2015, 06:01 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Firstly, please do not hijack someone else's question and simply ask your own. Try setting the field separator appropriately. See [url=http://www.gnu.org/software/gawk/manual/html_node/index.html]here[\url] for more details.
 
Old 08-02-2015, 07:34 AM   #6
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Your file, as shown in your post, appears to have non printing characters embedded in it (that you don't want embedded).

You can use the od utility to dump the file in octal or hex or ASCII (read the manual page for the utility) which will help you identify what those asterisks are so you can get rid of them -- you probably want to look at whatever is writing the file first as it is probably inserting values in your file (those values will be non printing characters but will be embedded in the file). You can also invoke vi so that it will show you non printing characters but I just can't remember right now how to do that (I'll keep looking).

It might be useful to attach your file to a post so that we can take a look at it and perhaps identify the problem (you could also give a link to your system if that's possible).

Hope this helps some.
 
Old 08-04-2015, 02:50 AM   #7
linux_project
LQ Newbie
 
Registered: Apr 2015
Posts: 11

Original Poster
Rep: Reputation: Disabled
Thanks tronayne. I am attaching the file. PFA
Attached Files
File Type: txt file_spcl_char.txt (1.2 KB, 12 views)
 
Old 08-04-2015, 03:38 AM   #8
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
The columns in your file seem to be separated by sequences "20c2a0", where 20 is a space and "c2a0" is a utf-8-encoded non-breaking space. Try:

Code:
awk -F"[ \xc2\xa0]+" '{print $4}' filename
 
Old 08-04-2015, 04:51 AM   #9
linux_project
LQ Newbie
 
Registered: Apr 2015
Posts: 11

Original Poster
Rep: Reputation: Disabled
Thanks millgates. This worked... But how could i find this in vi . what command can be used in future to find it in the vi editor itself .
 
Old 08-04-2015, 05:36 AM   #10
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
You may find this article interesting. Displaying the non-breaking spaces only seems to work in vim, though. That said, if I suspected rogue characters in my files, I'd inspect them with a hex editor.
 
Old 08-04-2015, 07:07 AM   #11
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Here's a slightly easier way to see what's in a file (any file, by the way): a little utility called cdump (as in, character dump).

Save the attached file, cdump.txt, as cdmup.c somewhere then
Code:
make cdump
move that to somewhere on your PATH (such as /usr/local/bin).

Use it
Code:
cdump file_spcl_char.txt
000000:  32 20 c2 a0 69 70 20 c2  a0 20 c2 a0 20 c2 a0 20   2...ip..........
000010:  c2 a0 20 c2 a0 30 78 31  32 20 c2 a0 20 c2 a0 20   .....0x12.......
000020:  c2 a0 20 c2 a0 31 30 2e  31 35 33 2e 39 38 2e 32   .....10.153.98.2
000030:  34 36 20 c2 a0 20 c2 a0  20 31 32 39 2e 31 34 34   46.......129.144
000040:  2e 31 33 30 2e 31 33 30  20 c2 a0 20 c2 a0 20 c2   .130.130........
000050:  a0 20 20 20 36 36 36 36  20 c2 a0 20 c2 a0 20 c2   ....6666........
000060:  a0 20 35 31 34 20 c2 a0  20 c2 a0 20 c2 a0 35 33   ..514.........53
000070:  39 35 38 33 20 c2 a0 20  39 2e 37 32 25 0d 0a 33   9583....9.72%..3

000080:  20 c2 a0 69 70 20 c2 a0  20 c2 a0 20 c2 a0 20 c2   ...ip...........
000090:  a0 20 c2 a0 30 78 30 36  20 c2 a0 20 c2 a0 20 c2   ....0x06........
0000a0:  a0 20 c2 a0 31 30 2e 32  32 37 2e 31 30 38 2e 33   ....10.227.108.3
0000b0:  31 20 c2 a0 20 c2 a0 20  31 30 2e 31 39 38 2e 31   1.......10.198.1
0000c0:  38 34 2e 34 20 c2 a0 20  c2 a0 20 c2 a0 20 c2 a0   84.4............
0000d0:  20 c2 a0 20 c2 a0 20 32  31 36 20 c2 a0 20 c2 a0   .......216......
0000e0:  20 c2 a0 20 34 38 37 31  39 20 c2 a0 20 c2 a0 20   ....48719.......
0000f0:  c2 a0 35 33 31 30 39 39  20 c2 a0 20 39 2e 35 37   ..531099....9.57
(the above is abbreviated output).

The offending characters (c2) show up.

Then, use @millgates method for doing what you want with AWK (or an editor to replace them with a space character).

Hope this helps some.

Last edited by tronayne; 08-04-2015 at 08:28 AM. Reason: Missing last line of attachment, see below.
 
Old 08-04-2015, 08:27 AM   #12
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Oops!

Just noticed that the last line of the file cdup.txt is missing (why I do not know) and I can't figure out how to delete the file in the post above so here's the corrected file file.

Sorry about that.
Attached Files
File Type: txt cdump.txt (1.8 KB, 11 views)
 
Old 08-05-2015, 10:53 AM   #13
linux_project
LQ Newbie
 
Registered: Apr 2015
Posts: 11

Original Poster
Rep: Reputation: Disabled
Thanks millgates. I have gone through the link. I am able to identify the special characters but still not able to remove special characters in vim editor.
I ran `:set nolist` but it just remove the special character but doesn't remove special character permanently.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Matching Pattern & needed desired Output sumit.inform Linux - Software 5 04-18-2013 02:48 PM
Limiting awk to desired response ZimMonkey Linux - Newbie 5 06-28-2009 09:27 PM
Program : Not getting the desired output thelink123 Linux - Newbie 4 11-17-2008 01:59 AM
how to get the desired output from the following code: pdklinux79 Linux - Newbie 1 06-10-2008 01:51 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration