Visit Our UNIX and Linux User Community


nawk newbie


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting nawk newbie
# 1  
Old 03-31-2011
Computer nawk newbie

hi,

i've got a number of csv files (e.g. 1.csv, 2.csv ...) in a directory to process them.

I need to read them fast and using nawk, to carry out reg expression matching, i would then output those files into their respective files (e.g. 1.csv.out, 2.csv.out).

in my codes:
Code:
while ((getline< "/workspace/folder/1.csv")>0)
regex="abx"
if ($6 ~ regex)
{
print $1 $3 $4 >> "/workspace/folderout/1.csv.out"
}

it works when the file name is hard coded, for loop doesnt seems to work.
i had:
"for FILE in /workspace/folder*.csv" before the begin

but it gives me syntax err at the for ..

pls help Smilie

Last edited by Franklin52; 03-31-2011 at 08:12 AM.. Reason: Please use code tags
# 2  
Old 03-31-2011
Code:
regex="abx"
cd /workspace/folderout

for file in *.cvs
do
   nawk -v str=$regex '{if ($6 ~ v) print $1,$3,$4 > FILENAME ".out"}' $file
done


Last edited by rdcwayx; 03-31-2011 at 08:06 AM..
This User Gave Thanks to rdcwayx For This Post:
# 3  
Old 03-31-2011
Please post an example of input and expected output then people will be able to provide a more accurrate help

---------- Post updated at 11:59 AM ---------- Previous update was at 11:56 AM ----------

Quote:
Originally Posted by rdcwayx
Code:
... print $1,$3,$4 > "/workspace/folderout/" FILENAME ".out}' $file
done

A double quote is missing : ".out"
This User Gave Thanks to ctsgnb For This Post:
# 4  
Old 03-31-2011
hi thanx,
Code:
cd /workspace/folderout

din work properly as the input file is overwritten, i'd like to keep the original file too ..

Code:
FILENAME ".out"

there's a syntax err for this, i think FILENAME cant be manipulate in the single quotes.
# 5  
Old 03-31-2011
Code:
regex="abx"; for file in *.cvs; do awk -v str=$regex '{if ($6==str) print $1,$3,$4}' $file >$file.out; done

This User Gave Thanks to sgruenwald For This Post:
# 6  
Old 04-01-2011
Quote:
Originally Posted by bing
hi thanx,
Code:
cd /workspace/folderout

din work properly as the input file is overwritten, i'd like to keep the original file too ..

Code:
FILENAME ".out"

there's a syntax err for this, i think FILENAME cant be manipulate in the single quotes.
FILENAME is build-in var in awk, no need quotes around it.
# 7  
Old 04-05-2011
This is how the script looks like

Code:
regex="abx"; for file in *.csv; do nawk -v str=$regex '{if ($6 ~ str) print $1,$3,$4}' $file >$file.out; done

i need to do regular expression using '~'.

as i've many csv files, is there a way to make the above run faster other than running the script multiple times simultaneously? SmilieSmilie

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nawk Problem - nawk out of space in tostring on

Hi.. i am running nawk scripts on solaris system to get records of file1 not in file2 and find duplicate records in a while with the following scripts -compare nawk 'NR==FNR{a++;next;} !a {print"line"FNR $0}' file1 file2duplicate - nawk '{a++}END{for(i in a){if(a-1)print i,a}}' file1in the middle... (12 Replies)
Discussion started by: Abhiraj Singh
12 Replies

2. Shell Programming and Scripting

using nawk

help out with code. two files aaa bbb contains some records..output file xyz should be like this..see below i/p file:aaa 08350|60521|0000|505|0000|1555|000|NYCMT|Pd_1 |-11878 i/p file: bbb 60521|60510 o/p file :xyz 60510|08350|60521|0000|505|0000|1555|000|NYCMT|Pd_1 |-11878 (5 Replies)
Discussion started by: Diddy
5 Replies

3. Shell Programming and Scripting

Nesting - two nawk into one nawk

hi people; this is my two awk code: nawk '/cell+-/{r=(NF==8) ? $4FS$5FS$6 : NF==7 ? $4FS$5 : $4 ;c=split(r,rr);for (i=1;i<=c;i++){if(rr != "111111"){printf($3" %d ""\n",(i+3))}}printf("")}' /home/gc_sw/str.txt > /home/gc_sw/predwn.txt nawk -F'*' '{gsub(/ *$/,"")}$0=$1$($NF-2)'... (2 Replies)
Discussion started by: gc_sw
2 Replies

4. Shell Programming and Scripting

nawk help

Hi Gurus, I am using a script as under : read string nawk -v search="$string" ' /a/,/z/ { block = (block ? block ORS : "") $0; } /z/ { if (block ~ search) print block; } ' <File> nawk -v search="$string" ' /b/,/z/ { ... (1 Reply)
Discussion started by: vanand420
1 Replies

5. Shell Programming and Scripting

perl newbie . &&..programming newbie

Hi, I am new to programming and also to perl..But i know 'perl' can come to my rescue, But I am stuck at many places and need help..any small help is much appreciated... below is the description of what i intend to acheive with my script. I have a files named in this format... (13 Replies)
Discussion started by: xytiz
13 Replies

6. Shell Programming and Scripting

perl newbie . &&..programming newbie (question 2)

Hello everyone, I am having to do a lot of perl scripting these days and I am learning a lot. I have this problem I want to move files from a folder and all its sub folders to one parent folder, they are all .gz files.. there is folder1\folder2\*.gz and there are about 50 folders... (1 Reply)
Discussion started by: xytiz
1 Replies

7. UNIX for Dummies Questions & Answers

UNIX newbie NEWBIE question!

Hello everyone, Just started UNIX today! In our school we use solaris. I just want to know how do I setup Solaris 10 not the GUI one, the one where you have to type the commands like ECHO, ls, pwd, etc... I have windows xp and I also have vmware. I hope I am not missing anything! :p (4 Replies)
Discussion started by: Hanamachi
4 Replies

8. Shell Programming and Scripting

nawk help

What am I doing wrong here? I get syntax error. I am trying to parse a file looking for the lines with "running" as the first field then print the 5th field, then looking at the 9th field for specific values and writing the whole line to a junk file. nawk '; {if ($1 == "running")... (10 Replies)
Discussion started by: beppler
10 Replies

9. Shell Programming and Scripting

how to access values of awk/nawk variables outside the awk/nawk block?

i'm new to shell scripting and have a problem please help me in the script i have a nawk block which has a variable count nawk{ . . . count=count+1 print count } now i want to access the value of the count variable outside the awk block,like.. s=`expr count / m` (m is... (5 Replies)
Discussion started by: saniya
5 Replies

10. Shell Programming and Scripting

nawk

Hi, I had this syntax and no matter what I do, I can't get it run. err message: run6: syntax error at line 121 : `(' unexpected I went to line 121 and it's comment out! All the variables passed to nawk are valid. There are two places I suspect have the problem: 1.... (3 Replies)
Discussion started by: whatisthis
3 Replies

Featured Tech Videos