Help understand awk command


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Help understand awk command
# 1  
Old 10-31-2019
Help understand awk command

Help understand awk command
This command converts the column values to rows.

Command:
Code:
awk -s1=" " '{S=S?S OFS s1 $0 s1:s1 $0 s1} END{print S}' OFS=,  Input_file

Example:
Code:
1
2
3

is converted to:
Code:
1, 2, 3

Can anyone please help me understand this command?
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 10-31-2019 at 11:20 AM.. Reason: code tags, please!
# 2  
Old 10-31-2019
Quote:
Originally Posted by mohan44
...
This command converts the column values to rows.
...
I'm afraid it doesn't as it has a syntax error. Did you run it as given?
Upfront variable definition needs the -v option.
Quote:
is converted to:
1, 2, 3
Not quite - there are spaces before the commas.





Once you corrected that, it uses a "conditional assignment" to check the S variable and, if non-null, adds the actual input line ($0) enclosed in spaces to it, else assigns it just the latter.


This does similar thigs making use of awk's default bebaviour:
Code:
awk '$1=$1' RS= OFS=, file
1,2,3

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

I can not understand the command from the systemd?

journalctl --since "tomorrow" By idea to show magazines from tomorrow. As it is illogical. Tell me what is the essence of the team with the key tomorrow? Code tags please (1 Reply)
Discussion started by: alekseev
1 Replies

2. Shell Programming and Scripting

Help to understand the command

Hi Gurus, I am new for Unix scripting. below command in one existing script. I am not able to fully understand. please help in below command, I am not able to understand what's {P=1} do thanks in advance awk 'NF==1{$3=$1;$1=L}P&&NF>=3{print $1,$3;L=$1}/^___/{P=1}' FILE (3 Replies)
Discussion started by: green_k
3 Replies

3. Shell Programming and Scripting

Not able to understand use exec command

Hi i am in learning phase of unix. i was going through exec in a unix book. below is the command exec n>file exec n>>file however when i used the exec command like below , where ex is the file name exec 2>>exand then do ls -lrt then again when i do the ls -lrt to see the size of the file... (3 Replies)
Discussion started by: scriptor
3 Replies

4. UNIX for Dummies Questions & Answers

AWK - Hard to understand for me

Hi All, I have googled and spent few hours to understand about AWK usage... But still not very much clear.... Upto my understanding 'AWK' deals with files... It reads the file takes it as a input process it and gives output or writes the output to an another file... >> Please correct me... (23 Replies)
Discussion started by: ambarginni
23 Replies

5. UNIX for Dummies Questions & Answers

Not able to understand the output of w command

I have taken putty session of a server from two separate machines namely HOST1(3 sessions) and HOST2(1 Session) . However w command says there are 5 users Confused over the output any clue will be appreciated. # w 09:29:36 up 34 days, 15:48, 5 users, load average: 0.62, 4.33, 8.16 USER ... (3 Replies)
Discussion started by: pinga123
3 Replies

6. Shell Programming and Scripting

Need help to understand Awk code.

Hi Guys, Can someone please explain this code to me. I could figure out it's adding and comparing two fields but I am not sure which ones. sort -t"|" -k3.1 /tmp/mpcashqc.xtr| awk -F"|" '{CHECKAMT+=$3;BATCHTOT=$4;\ items++}END{for(i in CHECKAMT) if (CHECKAMT!=BATCHTOT)... (6 Replies)
Discussion started by: nua7
6 Replies

7. Shell Programming and Scripting

Need to understand the output of last command

root@desktop:~# last reboot | head -1 reboot system boot 2.6.31-17-generi Tue Jan 26 12:05 - 13:52 (01:46) What does the last two fields(12:05 - 13:52 (01:46)) of the output mean? (2 Replies)
Discussion started by: proactiveaditya
2 Replies

8. Shell Programming and Scripting

Sed command - help me understand

Hello, can someone please explain me what the following commands do.. i know the output but i would like to understand the break down of what they do step by step. 1) sed -n "/ $(date +\%R -d "-1 min")/,$"p req.txt| wc -l 2) awk '/19:00/,/22:00/' app.log |grep "mytesturl"|grep... (2 Replies)
Discussion started by: niks
2 Replies

9. HP-UX

Need help to understand chatr command

Hi, I am trying to find the max memory utilization of a binary file using chatr command. i gave chatr <file name>. But no where i could see the max memory usage of the file. How do you i check the memory usage of this binary file. Upto my knowledge it should be 1.75GB in HP-UX. But the does... (1 Reply)
Discussion started by: kprasanna_79
1 Replies

10. UNIX for Dummies Questions & Answers

Just a quickie to help me understand this command:

Hi. Im working in AIX. Oracle 9.2. In one of the backup scripts, I see this: sysback -f'/dev/rmt0' '-T chrp' '-k mp' -w '/tmpback' 'oradata_vg oraindx_vg ora misc_vg' The line here, as I know, specifies the device name, the temp file, and the volume groups. My query regards theses two: '-T... (2 Replies)
Discussion started by: Incremental
2 Replies
Login or Register to Ask a Question