views:

135

answers:

4

I have a text in an email on a windows box that looks something like this:

100 some random text
101 some more random text
102 lots of random text, all different
103 lots of random text, all the same

I want to extract the numbers, i.e. the first word on each line. I've got a terminal running bash open on my Linux box...

If these were in a text file, I would do this:

awk '{print $1}' mytextfile.txt

I would like to paste these in, and get my numbers out, without creating a temp file.

my naive first attempt looked like this:

$ awk '{print $1}'
100 some random text
100
101 some more random text
101
102 lots of random text, all different
103 lots of random text, all the same

102
103

The buffering of stdin and stdout make a hash of this. I wouldn't mind if stdin all printed first, followed by all of stdout; this is what would happen if I were to paste into 'sort' for example, but awk and sed are a different story.

a little more thought gave me this: open two terminals. Create a fifo file. Read from the fifo on one terminal, write to it on another.

This does in fact work, but I'm lazy. I don't want to open a second terminal. Is there a way in the shell that I can hide the text echoed to the screen when I'm passing it in to a pipe, so that I paste this:

100 some random text
101 some more random text
102 lots of random text, all different
103 lots of random text, all the same

but see this?

$ awk '{print $1}'
100
101
102
103
+1  A: 

You could use a here document. I tried the following and it worked:

$ awk '{print $1}' << END
> 100 some random text
> 101 some more random text
> 102 lots of random text, all different
> 103 lots of random text, all the same
> END
100
101
102
103

I'll try to explain what I typed:

awk '{print $1}' << END (RETURN)
(PASTE) (RETURN)
END (RETURN)

If that makes sense.

The pasted text still shows up on stdout, but the results you care about all end up afterwards, so you can easily grab it out. Make sure you pick something that's not in your text to replace the END in my example!

Carl Norum
+1  A: 

Perhaps the best way is to use your shell here-docs:

awk '{print $1}' <<EOF
100 some random text
101 some more random text
102 lots of random text, all different
103 lots of random text, all the same
EOF
100
101
102
103

Alternatively, you can use the END block:

awk '{a[NR]=$1} END{for (i=1;i<=NR;i++) print a[i]}'
100 some random text
101 some more random text
102 lots of random text, all different
103 lots of random text, all the same
^d
100
101
102
103
marco
A: 

Something like

stty -echo ; awk '{print $1}' ; stty echo

may help you.

Michael Krelin - hacker
Michael,is stty global to the tty, or could it be localized by process?(stty -echo; awk 'print $1')
Barton Chittenden
A: 

If you are set up like I am, running Windows, Xming and PuTTY, you can do this:

$ xclip -i[enter]
[right-click-paste]
[ctrl-d]
$ xclip -o | awk '{print $1}'

It requires that you have X-forwarding set up and you've installed xclip (xsel -i and xsel -o can be used instead).

Dennis Williamson
Very nice! This is, in fact, exactly how I am set up...
Barton Chittenden
This triggered some brainstorming on my part; I came up with the following: smenu() ( IFS=',' ; select x in $*; do echo "$x" | xsel -i; done )Allowing something like this: smenu "First item to paste,Paste me #2,Third menu item" 1) First item to paste 2) Paste me #2 3) Third menu item #?entering '3' at the prompt will put 'Third menu item' into your cut/paste buffer. Very handy for speeding up repetitive data entry in retarded click-happy windows programs.I added this and your previous suggestion at commandlinefu.com... hope you don't mind.
Barton Chittenden
SO seems to be @#$%ing up my code formatting... sorry.
Barton Chittenden