perform echo and awk inside a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perform echo and awk inside a string
# 1  
Old 02-08-2012
perform echo and awk inside a string

hi,

just wanted to make a shortcut of this one
a="a b c"
b=`echo $a | awk '{print $2}'`
echo "the middle is $b"


why can't i do this:
a="a b c"
echo "the middle is ${`echo $a | awk '{print $2}'`}" <- bad substitution Smilie

thanks
# 2  
Old 02-08-2012
Code:
echo "the middle is `echo $a | awk '{print $2}'`"

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 02-08-2012
Quote:
Originally Posted by h0ujun
hi,

just wanted to make a shortcut of this one
a="a b c"
b=`echo $a | awk '{print $2}'`
echo "the middle is $b"


why can't i do this:
a="a b c"
echo "the middle is ${`echo $a | awk '{print $2}'`}" <- bad substitution Smilie

thanks
It should be:

Code:
 
a="a b c"
echo "the middle is $(echo $a | awk '{print $2}')"

[PS: U should keep your Codes/Output in CODE Tag. Mods don't like unformatted codes and output. SO next time please use Code Tags]
# 4  
Old 02-08-2012
Either way is fine. Backticks are used in post #2.. $() will work equally well.
# 5  
Old 02-08-2012
Quote:
Originally Posted by balajesuri
Either way is fine. Backticks are used in post #2.. $() will work equally well.

Oopps... We both posted nearly at the same time so did not see ur reply :P
I like $() more then `` because of aeshetic value and less confusion over `` and '' Smilie
# 6  
Old 02-08-2012
Quote:
Originally Posted by knight_eon
[..]
I like $() more then `` because of aeshetic value and less confusion over `` and '' Smilie
Plus in the backquote style command substitution you sometimes need to escape certain things (for example when nesting) whereas in $() this is much more straightforward..
# 7  
Old 02-08-2012
Plus, backticks is obsolete, $( ) is recommended.

--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed inside the awk script to replace a string in the array

The requirement is i need to find an array value matching with pattern {5:{ , replace that with 5: and reassign that to same array index and print it. I write something like below and the issue is sed command is not working. If i replace " with "`" the script gives syntax error.how can i... (8 Replies)
Discussion started by: bhagya123
8 Replies

2. Shell Programming and Scripting

Gnu tool; sed awk echo etc to prepend or append string to a file

Looking to add text to a file, example File example; nodegroups: check-hosts: L@host.domain.com,host2.domain.com,host3.domain.com I need to take a file with a one line list of hosts separated by commas host.domain.com,host2.domain.com,host3.domain.comand prepend the string " ... (6 Replies)
Discussion started by: bash_in_my_head
6 Replies

3. UNIX for Beginners Questions & Answers

String has * as the field delimiter and I need echo/awk to escape it, how?

Hi, I am trying to read an Oracle listener log file line by line and need to separate the lines into several fields. The field delimiter for the line happens to be an asterisk. I have the script below to start with but when running it, the echo command is globbing it to include other... (13 Replies)
Discussion started by: newbie_01
13 Replies

4. Shell Programming and Scripting

Using echo to print arguments inside a function

I am using echo in bash. Have created a function prargv which takes a number of arguments. Example: prargv "-e" "--examples" Inside prargv, I want to print all the arguments using echo echo "$@" This returns --examples rather than -e --examples" This problem can be fixed... (3 Replies)
Discussion started by: kristinu
3 Replies

5. Shell Programming and Scripting

Unable to echo single quotes inside awk

# echo 'export HISTFILE=/var/log/history/history_$(uname -n)_$(date +%Y:%b:%d:%H:%M)_$(who am i | awk '{print \$1}')' >> new_file # # cat new_file export HISTFILE=/var/log/history/history_$(uname -n)_$(date +%Y:%b:%d:%H:%M)_$(who am i | awk {print $1}) # Now how to echo the quotes around the... (2 Replies)
Discussion started by: proactiveaditya
2 Replies

6. Shell Programming and Scripting

Urgent - Search a string inside awk

I need to search for string which is formed from the awk input file in a file given as input through -v option. I tried using the while (getline filename) and match(). But this approach is consuming lot of time as the input file has thousands of records. Please suggest any alternative. Thanks! (0 Replies)
Discussion started by: Cool
0 Replies

7. Shell Programming and Scripting

tcsh - understanding difference between "echo string" and "echo string > /dev/stdout"

I came across and unexpected behavior with redirections in tcsh. I know, csh is not best for redirections, but I'd like to understand what is happening here. I have following script (called out_to_streams.csh): #!/bin/tcsh -f echo Redirected to STDOUT > /dev/stdout echo Redirected to... (2 Replies)
Discussion started by: marcink
2 Replies

8. UNIX for Dummies Questions & Answers

How to perform string replace in shell script?

I have value like ABCDEF,BBCCDD in a shell variable, now i would like to have ABQWEF,BBQWDD in the same shell variable. How can i replace the char at position 3&4 with QW in shell script? (3 Replies)
Discussion started by: vel4ever
3 Replies

9. Shell Programming and Scripting

Appending string (charachters inside the line) to a fixed width file using awk or sed

Source File: abcdefghijklmnop01qrstuvwxyz abcdefghijklmnop02qrstuvwxyz abcdefghijklmnop03qrstuvwxyz abcdefghijklmnop04qrstuvwxyz abcdefghijklmnop05qrstuvwxyz Whatever characters are in 17-18 on each line of the file, it should be concatenated to the same line at the character number... (6 Replies)
Discussion started by: tamahomekarasu
6 Replies

10. UNIX for Dummies Questions & Answers

How to correctly use an echo inside an echo?

Bit of a weird one i suppose, i want to use an echo inside an echo... For example... i have a script that i want to use to take users input and create another script. Inside this script it creates it also needs to use echos... echo "echo "hello"" >$file echo "echo "goodbye"" >$file ... (3 Replies)
Discussion started by: mokachoka
3 Replies
Login or Register to Ask a Question