awk commands on files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk commands on files
# 1  
Old 08-09-2011
awk commands on files

I want to create an awk script that performs different things on different files.

For example, for files with extension .ry I might do something, but for .xt I do something else, and another things on .zc files.

How can I do this in one awk script?
# 2  
Old 08-09-2011
IMO, a shell script with the case statement is the only good solution.
# 3  
Old 08-09-2011
Tools

You can do what you want to do using the awk FILENAME variable. It can be used to determine the filename (and hence extension) that awk is processing.
Code:
awk '{print FILENAME "," $0}' file1 file2 file3

# 4  
Old 08-09-2011
Ok. So now in front of each line I get the filename. Still not so sure how to make the distinction.

Anyway, the thing is that if I have a .xt file I perform the following:

Code:
function abs(val) { return val > 0 ? val : -val }

BEGIN {
  ARGV[ARGC++] = ARGV[ARGC-1]
}

FNR == NR {
  />/ && idx[FNR] = ++i
  $2 || val[i] = $1
  next
}

FNR in idx { v = val[idx[FNR]] }

{ !/>/ && srdist = abs($1 - v) }

/>/ || NF == 2 && srdist < dsrmx { sub(/[ \t]+$/, ""); print }

whereas if I have a .ry file I perform:

Code:
function abs(val) { return val > 0 ? val : -val }

BEGIN {
  print ">"
  RS = ORS = ">"
}

#NR == 1 { print }

NF > 2 { if (abs($1 - $(NF-2)) < srmax) { print } }

---------- Post updated at 08:55 AM ---------- Previous update was at 08:27 AM ----------

For getting the file extension I thought I will do something like this:

Code:
{
  n = split(FILENAME, a, ".")
  ext = a[2]
  print ext
}

My only problem now if how to do different things in the BEGIN stage dependent of the file extension.

---------- Post updated at 09:14 AM ---------- Previous update was at 08:55 AM ----------

I have written
Code:
{
  split(FILENAME, a, ".")
  fext = a[2]

  if (fext == "xt") {
    print "Travel time file detected"
  } else if (fext == "ry") {
    print "Ray paths file detected"
  } else {
    print "Unrecognized file"
  }

  print fext
}

NF > 2 { if (abs($1 - $(NF-2)) < srmax) { print } }

I want to perform the last line only if the extension is .ry
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

AWK commands, in different OS

I have developed a bash shell script which works perfectly fine when any bash shell is in use except SunOS. There are two commands that is not compatible. #!/usr/bin/env bash Problem 1. The awk command below does not work for SunOS Requirement We extract Oracle EBS tables into text... (9 Replies)
Discussion started by: uuuunnnn
9 Replies

2. Shell Programming and Scripting

Combine awk commands into one

my code: gawk 'NR>'"${LASTLINENUM}"' && NR<='"${LINEENDNUM}"'' ${LOGFILE} | gawk '{l=$0;} /'"${STRING1}"'/ && /'"${STRING2}"'/ {for (i=NR-'"${BEFOREGLAF}"'; i<=NR+'"${AFTERGLAF}"'; i++) o=i; t++;} END { for(i=1; i<=NR; i++) if (o) print l; print t+=0;}' i would like to combine this into one... (5 Replies)
Discussion started by: SkySmart
5 Replies

3. UNIX for Dummies Questions & Answers

How to use shell commands within awk?

Hey guys i want to use shell commands like ls, find, cd and more with in awk statements with inputs from the awk variables. Like in the below code how can i change the directory using the value of path. Please suggest awk '{ while (i<NR) { i++; percentage = $5; path = $6; ... (2 Replies)
Discussion started by: rishi90
2 Replies

4. Shell Programming and Scripting

Help me in awk commands

can you please let me know what does the below awk command does. awk 'NR>2{printf "%-30s %-15s\n", $1, !($4)?$2:$4;}' result.txt (1 Reply)
Discussion started by: ramkumar15
1 Replies

5. Shell Programming and Scripting

Find files and execute commands on these files (advanced)

So, I need to find a bunch of files and delete them (this example, but sometimes I need it for something else) and my trusty go-to command has always been: find . -type f -name '*file*' | xargs -I## rm '##' Works wonders... But: touch file\ file\'.txt touch file.txt touch file\ file.txt... (6 Replies)
Discussion started by: Mr.Glaurung
6 Replies

6. Shell Programming and Scripting

perform 3 awk commands to multiple files in multiple directories

Hi, I have a directory /home/datasets/ which contains a bunch (720) of subdirectories called hour_1/ hour_2/ etc..etc.. in each of these there is a single text file called (hour_1.txt in hour_1/ , hour_2.txt for hour_2/ etc..etc..) and i would like to do some text processing in them. Each of... (20 Replies)
Discussion started by: amarn
20 Replies

7. Shell Programming and Scripting

Using shell commands in awk

Hi, I've searched this site and the wider web and have not found anything (that I can understand..) that helps me. I've used shell commands in awk fine in the past, the difference is that I want to pass the shell command a field variable within awk from the current input. A simple example... (3 Replies)
Discussion started by: Ronnie717
3 Replies

8. UNIX for Dummies Questions & Answers

UNIX commands in AWK

Hi, I have 2 lists of words and I need to take every line in first_list.txt and use agrep command to find similar words in whole second_list.txt. I tried to use AWK, but I dont know how to take current line ($0) and use in inside of agrep cat first_list.txt | awk '' Thanks for help (10 Replies)
Discussion started by: m4rty
10 Replies

9. Shell Programming and Scripting

How do we use multiple commands with AWK

Hi, I have a scenario where in, I have a file named abc.txt. I extract the file names from it using. awk '/dbf$/{print $NF}' abc.txt /u01/oradata/omc/systab/omcdef.dbf /u01/oradata/omc/oratemp/temptab1.dbf Now I need to further enhance this command and also extract the mount point... (3 Replies)
Discussion started by: geetap
3 Replies

10. Shell Programming and Scripting

How to use unix commands in awk

Hi All, I try to use the unix commands inside awk and couldnt succeed . Please help me how to achieve it. Regards, Sukumar. (7 Replies)
Discussion started by: jerome Sukumar
7 Replies
Login or Register to Ask a Question