explaining awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers explaining awk
# 1  
Old 03-20-2009
explaining awk

i am new to awk scripting. i couldn't figure out how this awk script works can anyone explain?

Code:
#!/bin/awk -f
{
    for( x=1; x<=NF; ++x) {
        if( x == 3 ) {
            continue
        }
        print x, $x
   }
}

thank you and regards

Last edited by Neo; 03-21-2009 at 10:40 AM.. Reason: add code tags
# 2  
Old 03-21-2009
Break it down

Quote:
Originally Posted by phone_book
i am new to awk scripting. i couldn't figure out how this awk script works can anyone explain?

#!/bin/awk -f
{
for( x=1; x<=NF; ++x) {
if( x == 3 ) {
continue
}
print x, $x
}
}
Because there's no pattern, the code enclosed in {} will run for every line of the input.
NF is the number of fields on the input line.
For each line, it will print all of the fields on the line EXCEPT field 3, because when x == 3, the continue executes, skipping the "print x, $x".
So for every line, you'll get something like
1 field1
2 field2
4 field4 (assuming there's more than 3 fields on the line).

What its intended purpose is, I can't tell you. But that's what it's doing.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Explaining behaviour of sudo bash "$0" "$@";

I've found this script part on the stackoverflow: if ; then sudo bash "$0" "$@"; exit "$?"; fi I realized that sudo bash "$0" "$@"; is the only needed for me. But the strange thing happens when I move this line outside the IF statement: sudo bash "$0" "$@"; stops the... (9 Replies)
Discussion started by: boqsc
9 Replies

2. OS X (Apple)

Help in explaining this echo conundrum.

OSX 10.12.3, default bash terminal. Consider this code and note it is calling 'sh' inside the code... #!/bin/sh echo '1\n2\n2\n3\n5' > /tmp/text hexdump -C /tmp/text /bin/echo '1\n2\n3\n4\n5' > /tmp/text hexdump -C /tmp/text Now view the interactive mode below, note the underlying shell is... (6 Replies)
Discussion started by: wisecracker
6 Replies

3. Shell Programming and Scripting

awk output yields error: awk:can't open job_name (Autosys)

Good evening, Im newbie at unix specially with awk From an scheduler program called Autosys i want to extract some data reading an inputfile that comprises jobs names, then formating the output to columns for example 1. This is the inputfile: $ more MapaRep.txt ds_extra_nikira_usuarios... (18 Replies)
Discussion started by: alexcol
18 Replies

4. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

5. Shell Programming and Scripting

Help to explaining a command in run dot tcl

Hi, I'm running a rdt (run dot tcl) command, and come accross this line: alias abc 'set ARGS =(\!*); source home123/abc/$ARGS/setup' What does the command exactly do? Please help. (6 Replies)
Discussion started by: mar85
6 Replies

6. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

7. Shell Programming and Scripting

awk command to compare a file with set of files in a directory using 'awk'

Hi, I have a situation to compare one file, say file1.txt with a set of files in directory.The directory contains more than 100 files. To be more precise, the requirement is to compare the first field of file1.txt with the first field in all the files in the directory.The files in the... (10 Replies)
Discussion started by: anandek
10 Replies

8. UNIX for Dummies Questions & Answers

Explaining some lines from files : .login and .cshrc

Hello, can anyone explain me please what do those lines do ? From file .login 1) set history=40 2) setenv MACH `uname -s` 3) source /etc/login 4) source ~/$MACH/.login From file .cshrc 1) if ( ! $?prompt) exit 0 (5 Replies)
Discussion started by: bbqtoss
5 Replies

9. UNIX for Dummies Questions & Answers

Need help explaining how to use a VPN on a UNIX server with a Mac OS

I have gotten a gig to teach someone how to use a VPN client for a UNIX server on a MAC os. The problem is I have never used UNIX, dont mess with VPN's (my dad has a VPN that I have used a couple of times). I'm currently taking a crash course on UNIX but I was wondering if anyone could help me with... (0 Replies)
Discussion started by: psycopuppy
0 Replies

10. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies
Login or Register to Ask a Question