how do I paste text to a line by line text filter like awk, without having stdin echo to the screen?
I have a text in an email on a wi开发者_运维问答ndows 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
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!
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
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).
Something like
stty -echo ; awk '{print $1}' ; stty echo
may help you.
精彩评论