What is the meaning of this awk expression using sub?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers What is the meaning of this awk expression using sub?
# 1  
Old 01-19-2012
What is the meaning of this awk expression using sub?

Cannot understand what the "&" in the sub expression is supposed to do.

Code:
{sub(/^/,"&" s)}

# 2  
Old 01-19-2012
It does a search/replace in the current line( $0 ), stopping after the first replace.

The regular expression /^/ is what's being searched for. ^ has a special meaning in expressions, "the beginning of the line".

It replaces it with the literal string "&" S. So what it actually puts in there depends on what value S holds.

So if S was "asdf", this command would add "&asdf" to the beginning of the line.
# 3  
Old 01-19-2012
What would be the difference between these two expressions? They seem to do the same.

Code:
{sub(/^/, s "&")}

Code:
{sub(/^/, "&" s)}

---------- Post updated at 04:11 PM ---------- Previous update was at 04:04 PM ----------

Quote:
Originally Posted by Corona688
It does a search/replace in the current line( $0 ), stopping after the first replace.

The regular expression /^/ is what's being searched for. ^ has a special meaning in expressions, "the beginning of the line".

It replaces it with the literal string "&" S. So what it actually puts in there depends on what value S holds.

So if S was "asdf", this command would add "&asdf" to the beginning of the line.

It seems that & prints the entire text when used in a replacement string.

---------- Post updated at 04:12 PM ---------- Previous update was at 04:11 PM ----------

I have done like this and the results are shown. Not exactly what you are saying.
Code:
echo "Hello" | awk '{s="test";sub ( /^/, "&" s );print}'
testHello

echo "Hello" | awk '{s="test";sub ( /^/, "&" s );print}'
testHello

Also, the one below gives also same result

Code:
echo "Hello" | awk '{s="test";sub ( /^/, s );print}'
testHello

# 4  
Old 01-19-2012
That is very mysterious. I have no idea why the ampersand is being dropped, though it does it for me too.

If you use anything but ampersand it behaves as I described.

---------- Post updated at 03:21 PM ---------- Previous update was at 03:18 PM ----------

Ahah:

Code:
       gsub(r, s [, t])        For each substring matching the regular expres-
                               sion r in the string t, substitute  the  string
                               s,  and return the number of substitutions.  If
                               t is  not  supplied,  use  $0.   An  &  in  the
                               replacement text is replaced with the text that
                               was actually matched.  Use \& to get a  literal
                               &.   (This  must  be  typed as "\\&"; see GAWK:
                               Effective AWK Programming for a fuller  discus-
                               sion  of  the  rules for &'s and backslashes in
                               the replacement text of sub(), gsub(), and gen-
                               sub().)

gsub works the same as sub except it does multiple matches.

Since /^/ doesn't actually match any text, "&" becomes nothing here.
This User Gave Thanks to Corona688 For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

How to evaluate expression under awk?

I have to display only those subscribers which are in "unconnected state" and the date is 90 days older than today's date. Below command is used for this purpose: cat vfsubscriber_20170817.csv | sed -e 's/^"//' -e '1d' | \ nawk -F '",' '{if ( (substr($11,2,4) == 2017) && ( substr($11,2,8)... (1 Reply)
Discussion started by: dia
1 Replies

2. Shell Programming and Scripting

Evaluate Expression within awk

I want to create a conditional expression string and pass in an awk script. My script is as below... comm="\$3 == "hello"" awk -F "^T" -v command="${comm}" ' { if ( command ) { print "hye" } }' testBut the statement "if ( command )" always evaluates to true which is not... (5 Replies)
Discussion started by: Saikat123
5 Replies

3. UNIX for Dummies Questions & Answers

Meaning of awk script

what does this mean? awk '!a||a>$1 {a=$1} END {for (i in a) print a,i}' file (6 Replies)
Discussion started by: osama ahmed
6 Replies

4. Shell Programming and Scripting

awk print expression

Hi All, I have a doubt in awk print exp. Where in some awk commands have seen a digit 1 appended at the end of the awk ,didnt remember the command . like .. cat file |awk '{print }1' Could some one help in understanding these cases where we use them. Regards, Ganesh, (2 Replies)
Discussion started by: rmkganesh
2 Replies

5. Shell Programming and Scripting

What is the meaning of this regular expression?

I am an amateur in bash scripting and RE. Could someone please tel me in precise English, what is the meaning of the following: regex='^(||1|2|25)(\.(||1|2|25)){3}$' Thanks (1 Reply)
Discussion started by: bashily
1 Replies

6. UNIX for Dummies Questions & Answers

Awk inside Awk expression

Hi, It can be used awk inside other Awk?. I need to get another text processing while other text process. Thank you. (2 Replies)
Discussion started by: pepeli30
2 Replies

7. Shell Programming and Scripting

Regular expression in AWK

Hello world, I was wondering if there is a nicer way to write the following code (in AWK): awk ' FNR==NR&&$1~/^m$/{tok1=1} FNR==NR&&$1~/^m10$/{tok1=1} ' my_file In fact, it looks for m2, m4, m6, m8 and m10 and then return a positive flag. The problem is how to define 10 thanks... (3 Replies)
Discussion started by: jolecanard
3 Replies

8. Shell Programming and Scripting

Check for more than one expression using AWK

I have a txt file like below: testin.txt AB BC CD DE I have the following awk script BEGIN {flag1="N"} /(AB)|(BC)|(CD)|(DE)/ {flag1="Y"} END {print flag1} >awk -f testin.awk testin.txt Returns Y (8 Replies)
Discussion started by: visakhcr
8 Replies

9. UNIX for Dummies Questions & Answers

regular expression and awk

I can print a line with an expression using this: awk '/regex/' I can print the line immediately before an expression using this: awk '/regex/{print x};{x=$0}' How do I print the line immediately before and then the line with the expression? (2 Replies)
Discussion started by: nickg
2 Replies

10. Shell Programming and Scripting

awk and regular expression

Ive got a file with words and also numbers. Bla BLA 10 10 11 29 12 89 13 35 And i need to change "10,29,89,25" and also remove anything that contains actually words... (4 Replies)
Discussion started by: maskot
4 Replies
Login or Register to Ask a Question