Pipe to awk to variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pipe to awk to variable
# 1  
Old 08-31-2011
Pipe to awk to variable

Hi!

If I'm trying something like:
Code:
echo "hello world" | myvar=`awk -F "world" '{print $1}'`
echo $myvar

myvar is always empty Smilie

I googled for houres now and don't understand why it isn't working...
Trying it in normal bash.

Can someone explain it to me so I can say "Of course! Stupid me..." ? Smilie

Thanks!

Last edited by al0x; 08-31-2011 at 11:18 AM..
# 2  
Old 08-31-2011
I don't understand what you are trying to achieve with that command ...
# 3  
Old 08-31-2011
Quote:
Originally Posted by radoulov
I don't understand what you are trying to achieve with that command ...
Getting "hello" into myvar

Code:
myvar="hello"
echo $myvar

would give me the result I want to achieve

Edit:
Code:
echo "hello world" | awk -F "world" '{print $1}'

Would give me "hello", I just want to put that into my variable
# 4  
Old 08-31-2011
I'm still not sure, but it seems that you want to write something like this:

Code:
myvar=`echo "hello world" | awk -F "world" '{print $1}'`

I'm sure you'll get a better solution if you post more details ...
This User Gave Thanks to radoulov For This Post:
# 5  
Old 08-31-2011
That's exactly what I wanted^^

I knew it would be simple Smilie

Thanks!
# 6  
Old 08-31-2011
In some shells:

Code:
read VAR G <<<"hello world"

puts the variable into VAR without needing to call awk at all, which makes it hundreds of times faster.
# 7  
Old 08-31-2011
Quote:
Originally Posted by Corona688
In some shells:

Code:
read VAR G <<<"hello world"

puts the variable into VAR without needing to call awk at all, which makes it hundreds of times faster.
But I need awk to cut the string (since cut can only handle 1 char)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help on passing an input variable to the voronota program (after third pipe)

Dear UNIX forum members, I am using macbook pro 13 (2015 edition) with MAC OS Mojave and am trying to write the shell script where when it is run through terminal it asks for an input (in the code below an input variable is domains) and then that input becomes capital letter or letters which... (3 Replies)
Discussion started by: Aurimas
3 Replies

2. Shell Programming and Scripting

awk pipe to sort

In the below awk to add a sort by smallest to largest should it be added after the END? Thank you :). BEGIN { FS="*" } # Read search terms from file1 into 's' FNR==NR { s next } { # Check if $5 matches one of the search terms for(i in s) { if($5 ~ i) { ... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

BOTH pipe and file(s) into awk

Hello all, quick question: is it possible to pass input into AWK BOTH with a pipe AND a file at the same time, something like this: command .......|awk '.................' FILEIN > fileout All I read says either one or the other, not both, is it at all possible? And how would the... (2 Replies)
Discussion started by: gio001
2 Replies

4. Shell Programming and Scripting

awk print pipe

Hey fellas, I wrote an script which its output is like this: a 1 T a 1 T a 2 A b 5 G b 5 G b 5 G I wanna print $1 $2 and the total number of $2 value as the third column and after that $3. Sth like this: a 1 2 T a 2 1 A b 5 3 G I know how to do it with a given input... (4 Replies)
Discussion started by: @man
4 Replies

5. Shell Programming and Scripting

help with sed or awk with less pipe

<tr><th align=right valign=top>Faulty_Part</th><td align=left valign=top>readhat version 6.0</td></tr> <tr><th align=right valign=top>Submit_Date</th><td align=left valign=top>2011-04-28 02:08:02</td></tr> .......(a long string) I want to get all the field between "left valign=top>" and "... (2 Replies)
Discussion started by: yanglei_fage
2 Replies

6. Shell Programming and Scripting

using awk for setting variable but change the output of this variable within awk

Hi all, Hope someone can help me out here. I have this BASH script (see below) My problem lies with the variable path. The output of the command find will give me several fields. The 9th field is the path. I want to captured that and the I want to filter this to a specific level. The... (6 Replies)
Discussion started by: Cowardly
6 Replies

7. Shell Programming and Scripting

a pipe within a variable

Hi, I am trying to run this command: ED_CMD="$Access_OPS $ED_Logs_Dir/access | $lgrep -v $Access_Err" $lgrep $ED_CMD However, i am getting an error "Failed to open |: Broken pipe at " My question is how to put a pipe within a variable. Thanks. John. (4 Replies)
Discussion started by: john_prince
4 Replies

8. Shell Programming and Scripting

Trying to embed a pipe with a variable IN a variable

Hey all, I've tried this for quite some time now and haven't figured it out... I was hoping one of you shell scripters could help a newbie out :) I am trying to take the 1st parameter of my script and use it as the search pattern for my for loop #!/usr/bin/sh set -vx REGEX=$1 ... (3 Replies)
Discussion started by: Keepcase
3 Replies

9. Shell Programming and Scripting

Assign command (with pipe) output to a variable

Hi , I would like to assign command (with pipe) output to a variable. The code is as follows. The goal of the code is to get the last folder folder with a particular name pattern. myDate=`ls | grep 2009 | tail -1` echo "myDate=" $myDate However, in the presence of the pipe, the code... (3 Replies)
Discussion started by: jeff_cen
3 Replies

10. Shell Programming and Scripting

Is it better to grep and pipe to awk, or to seach with awk itself

This may just be a lack of experience talking, but I always assumed that when possible it was better to use a commands built in abilities rather than to pipe to a bunch of commands. I wrote a (very simple) script a while back that was meant to pull out a certain error code, and report back what... (4 Replies)
Discussion started by: DeCoTwc
4 Replies
Login or Register to Ask a Question