0

Can somebody explain the meaning of the below script please?

awk -F "," 's != $1 || NR ==1{s=$1;if(p){print p};p=$0;next}
            {sub($1,"",$0);p=p""$0;}
            END{print p}' file

The file has the following data:

2,"URL","website","aaa.com"
2,"abc","text","some text"
2,"Password","password","12345678"
3,"URL","website","10.0.10.75"
3,"uname","text","some text"
3,"Password","password","password"
3,"ulang","text","some text"
4,"URL","website","192.168.2.110"
4,"Login","login","admin"
4,"Password","password","blah-blah"

and the output is:

2,"URL","website","aaa.com","abc","text","some text",Password","password","12345678"
3,"URL","website","10.0.10.75","uname","text","some text""Password","password","password","ulang","text","some text"
2
  • 3
    this is a bit broad. What about indicating what is the part you don't understand? Also, running the script in my machine gives 3 lines of output, you are missing the last one including the block of "4"s Commented Jun 9, 2015 at 12:59
  • Hi, i am new to scripting. but i need this script . s=$1;if(p){print p};p=$0;next. This line i didnot understand. Commented Jun 9, 2015 at 14:59

1 Answer 1

1

awk has this structure

pattern {action}

for your script, let's analyze the elements, first pattern

 s != $1 || NR == 1   # if the variable s is not equal to first field
                      # or we're operating on first row

first action

 s = $1   # assign variable s to first field
 if (p) {  # if p is not empty, print
     print p
 }
 p = $0    # assign the current line to p variable
 next      # move to next, skip the rest

next pattern is missing, so the action will apply to all rows

 sub($1, "", $0)   # same as $1="", will clear the first field
 p = ((p "") $0)   # concat new line to p

last pattern is special reserved word END, only applied when all rows are consumed (there is counterpart BEGIN that's applied before the file is opened)

 END {
      print p      # print the value of p
 }
Sign up to request clarification or add additional context in comments.

1 Comment

if p is not empty should be if p is not the null string and does not evaluate numerically to zero. sub($1, "", $0) is not the same as $1="" as the former will replace the regexp contained in $1 in $0 (try both if $1 is .*) and it will not cause recompilation of $0 (try both with tabs between fields). p=p""$0 is an obfuscated form of p=p $0. I know a lot of awk documentation talks about pattern { action } but that's misleading at best - it's really condition { action }.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.