Explain awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Explain awk
# 1  
Old 09-26-2006
Explain awk

Hi,

I found this command in this forum, but, couldnt understand much from it.

could any one help me understand that???

the commands are :
Code:
awk '{sub(/ ~/,""); printf $0 ($0~/\|$/?ORS:"")}' file1 > file2
awk '{sub(/~ */,x);printf $0(/\|$/?ORS:x)}'
awk '{sub(/~ */,x);sub(/\|$/, "|\n")}8' ORS="" file1 >file2

the purpose is to remove all "~" characters and fix the broken lines

Code:
existing file data:

24|john|account ~ info |56|
25|kuo|account ~ journal |58|
27|kim|account ~ journal |59|
28|San|account ~ 
journal |60|
29|jay|account ~ journal |26|
29|Nan|account ~ 
journal |66|
30|Kee|account ~ journal |27|

Output: 

24|john|account info |56|
25|kuo|account journal |58|
27|kim|account journal |59|
28|San|account journal |60|
29|jay|account journal |26|
29|Nan|account journal |66|
30|Kee|account journal |27|

I am not able to understand the last part of the command where ORS(output record separator) is used
# 2  
Old 09-26-2006
Code:
awk '{sub(/ ~/,""); printf $0 ($0~/\|$/?ORS:"")}' file1 > file2

For each input line :

sub(/ ~/,"");
Remove'~' and all following spaces (replaced by empty sting "").

($0~/\|$/?ORS:"")
If the input line ends with '|' then the value of the expression ORS, else the value is an empty string.
ORS is a variable that contains the Output Record Separator that is used par awk when printing data to stdout (with print statement); The default value of ORS is '\n'.

printf $0 ($0~/\|$/?ORS:"")
Displays the input line, followed by a new line (ORS) if ending with '|'.


The 3 commands are varitions a the same technique.


jean-Pierre.
# 3  
Old 09-27-2006
Thanks jean-Pierre, are you from grenoble by an chance???
# 4  
Old 09-27-2006
Hammer & Screwdriver

Quote:
Thanks jean-Pierre, are you from grenoble by an chance???
Pas de chance, plus à l'ouest : Bordeaux.


Jean-Pierre.
# 5  
Old 09-27-2006
Quote:
Originally Posted by aigles
Pas de chance, plus à l'ouest : Bordeaux.


Jean-Pierre.
I asked you because one of the developers of my application was called by your name and is from grenoble. anyways, looks like you have a vast knowledge of unix scripting. do you mind, telling me where do you refer????
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Please explain the use of quotes in awk command in below example-

Example: `abc.ksh | grep '^GLIS'| awk -F' ' '{print \$1}'`; (3 Replies)
Discussion started by: Tanu
3 Replies

2. Shell Programming and Scripting

Please explain AWK Fibonnaci for loop

Referring to this: #!/bin/awk -f BEGIN{ for(i=0;i<=10;i++) { if (i <=1 ) { x=0; y=1; print i; } else { z=x+y; print z; x=y; y=z; } } (3 Replies)
Discussion started by: p1ne
3 Replies

3. Shell Programming and Scripting

Explain awk

I have 2 files recevied abc def ghi totallist abc 123 jasdhfaj def 345 fjdgkfsfh ghi 567 dfjdhdhfj jkl 678 djkahfdjshdf xyz 984 jdfdhfhdh myOutputFile jkl 678 djkahfdjshdf xyz 984 jdfdhfhdh I used this command for the output : awk 'FNR==NR {f1;next} !($1 in f1)' recevied... (2 Replies)
Discussion started by: nani1984
2 Replies

4. Shell Programming and Scripting

Explain this awk

found this handy one liner in another thread which is closed, it does what i need but im trying to understand it. it basically matches the field that contains the value v and prints its position awk -F, '{for(i=1;i<=NF;i++)if($i==v)print i}' v=yourfield inputfile my understanding is assign... (3 Replies)
Discussion started by: jack.bauer
3 Replies

5. Shell Programming and Scripting

Can Any people explain this awk command

Dear all , Can any people explain this awk command? What is the purpose of if (v++){b=$i;$i=""}? awk -F, '{for (i=1;i<=NF;i++){if (v++){b=$i;$i=""}};print $0} END { print "dups are" ;for ( i in b) print i}' OFS="," input_file This script is used to replace column duplicate value ... (1 Reply)
Discussion started by: eldonlck
1 Replies

6. Shell Programming and Scripting

Please explain what this Awk code is doing

Hi Guys, Please help me, I am new to programming and I don’t understand what some parts of this code are doing. I have comments on the parts I know, please help if my understanding of the code is not correct and also help with parts with questions. awk ' { gsub( ">",... (1 Reply)
Discussion started by: James_Owen
1 Replies

7. UNIX for Dummies Questions & Answers

Please explain this simple AWK example

awk '!_++' Most importantly, I want to know what the underscore does "!_" But ideally, please breakdown the whole thing. It is supposed to remove duplicate lines when found in a file. (1 Reply)
Discussion started by: glev2005
1 Replies

8. Shell Programming and Scripting

Explain this AWK script plz

Hi frnds, one my frnds has given resolution for my problem as below. it working great , but i couldnt understand somethings in the script. Why ++ operator after the function calling. how these each block working. will each run for each input line sequencially or one block for all the lines... (9 Replies)
Discussion started by: Gopal_Engg
9 Replies

9. Shell Programming and Scripting

AWK - HELP pls explain this ?

echo "23.54" | awk ' function round(A) { return int( A + 0.5 ) } { printf("%d\n",round($1)); }'> > > > > > awk: syntax error near line 2 awk: bailing out near line 2 (2 Replies)
Discussion started by: santosh1234
2 Replies

10. Shell Programming and Scripting

plese explain awk '{print \$NF}'

please explain this awk '{print \$NF}' i have a command grep -i adding /logs/eap | grep -iv equation | awk '{print \$NF}' | sort -u | sed 's/\.\$//' >> /temp/t please explain the above awk and sed as well how it works and also what is \$NF (1 Reply)
Discussion started by: mail2sant
1 Replies
Login or Register to Ask a Question