help needed .. Unable to write the data to new file after matching the pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help needed .. Unable to write the data to new file after matching the pattern
# 1  
Old 12-31-2008
help needed .. Unable to write the data to new file after matching the pattern

Hi,
i am pretty new to Unix environment .....
Can i get some help from any of you guyz on writing Unix script.
my requirement is like reading a csv file, finding a specific pattern in the lines and repalce the string with new string and write it to another file.

My file is file ABC123.dat
Col1,Col2,Col3
102628,AB10 0,21
102628,AB10 1,21
102628,AB10 2,21
102628,AB23 5,21
102628,AB35 9,21
102628,AB53 8AA,21
102628,AB53 8AB,21
102628,AB53 8AD,21
102628,AB53 8AE,21
102628,AB53 8AG,21
102636,AB54 0,21
102636,AB54 1,21
102636,AB54 2,21
102636,AB54 3,21
102636,AB54 4,21
102636,AB54 5,21
102636,AB54 6,21
102636,AB54 7A,21
102636,AB54 7B,21
102636,AB54 7D,21
102636,AB54 7E,21
888888,A99 9,21

and i have written a small script which finds the the pattern and prints the line numbers too but i am unable to write to another file....

file=/export/home/prashant/ABC123.dat

String1="[,][A-Z][A-Z][0-9][0-9][ ][0-9][,]"
String2="[,][A-Z][0-9][0-9][ ][0-9][,]"
String3="[,][0][,]" # Replacement for the string 1 & 2 if found the pattern in the file.

if [ -f "file" ]; then
echo "File $file does not exist."
exit 1
fi
cmd=$`(grep -c "[$String1|$String2]" $file)`
line=$(grep -n "$String1" $file)
line1=$(grep -n "$String2" $file)

if [ "$cmd" != "0" ]; then

echo "the String exists what you are expecting in the Line Number"
echo "$line"
echo "$line1"
else
echo "Word does not exist."
fi

out put is something like this ....
the String exists what you are expecting in the Line Number
2:102628,AB10 0,85
3:102628,AB10 1,85
4:102628,AB10 2,85
5:102628,AB23 5,85
6:102628,AB35 9,85
12:102636,AB54 0,85
13:102636,AB54 1,85
14:102636,AB54 2,85
15:102636,AB54 3,85
16:102636,AB54 4,85
17:102636,AB54 5,85
18:102636,AB54 6,85
23:888888,A99 9,85

but i am unable to replace the string1 & string2 after finding in the file with the string3 variable to another file .....
thanks
prashant
# 2  
Old 12-31-2008
Hi,

something like this:

command:
Code:
grep -n -v "AB[0-9]\{1,2\} [0-9]\{1,2\}[A-Z]\{1,2\}" file > newfile

output in newfile:
Code:
1:Col1,Col2,Col3
2:102628,AB10 0,21
3:102628,AB10 1,21
4:102628,AB10 2,21
5:102628,AB23 5,21
6:102628,AB35 9,21
12:102636,AB54 0,21
13:102636,AB54 1,21
14:102636,AB54 2,21
15:102636,AB54 3,21
16:102636,AB54 4,21
17:102636,AB54 5,21
18:102636,AB54 6,21
23:888888,A99 9,21

HTH Chris
# 3  
Old 01-02-2009
Thank you Chris .... its working
now i have to compare the original file with the new file and eliminate the matching records and write it to another file ..... need your suggestion on this trying to with diff command ....
but does it work on huge file .csv file which have a couple of million records.

Thank you very much
Prashant
# 4  
Old 01-02-2009
command:
Code:
grep -n "AB[0-9]\{1,2\} [0-9]\{1,2\}[A-Z]\{1,2\}" file

output:
Code:
7:102628,AB53 8AA,21
8:102628,AB53 8AB,21
9:102628,AB53 8AD,21
10:102628,AB53 8AE,21
11:102628,AB53 8AG,21
19:102636,AB54 7A,21
20:102636,AB54 7B,21
21:102636,AB54 7D,21
22:102636,AB54 7E,21

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

IN Between Data after matching the Pattern

HI , I WANT TO RETRIVE IN BETWEEN DATA FROM PARENTHESIS AND I AM GETTING ERRORS WHILE RUN THE AWK.I HAVE 2 FILES AND WANT TO PROCESS 1ST FILE PATTERN TO 2ND FILE AND WRITES INTO OUTPUT FILE.THIS TIME I AM PUTTING WHERE EXACTLY I AM GETTING ERRORS.SO PLEASE HELP. PATTERN_FILE.TXT --------------... (1 Reply)
Discussion started by: andrew_11
1 Replies

2. Shell Programming and Scripting

Big pattern file matching within another pattern file in awk or shell

Hi I need to do a patten match between files . I am new to shell scripting and have come up with this so far. It take 50 seconds to process files of 2mb size . I need to tune this code as file size will be around 50mb and need to save time. Main issue is that I need to search the pattern from... (2 Replies)
Discussion started by: nitin_daharwal
2 Replies

3. Shell Programming and Scripting

Word matching and write other data

Hi all, I have 7 words CAD CD HT RA T1D T2D BD Now I have 1 file which contain data in large number of rows and columns from 2 nd column onwards it contain any of these 7 words or may be more than one words among above 7 words: these 7 names are present in starting from 2nd ... (7 Replies)
Discussion started by: manigrover
7 Replies

4. Shell Programming and Scripting

Script to read file and extract data by matching pattern

Hello, I have a file ( say file1) which has lines like below. xxxx:xxxx,yyyy,1234,efgh zzzz:zzzz,kkkk,pppp,1234,xxxx,uuuu,oooo dddd:dddd here the word before ":" ( ie: xxxx) is the file name and the string after : are also file names, but each file name separated by "," In case of... (20 Replies)
Discussion started by: pradeepmacha
20 Replies

5. Shell Programming and Scripting

Help with matching pattern inside a file

I have a huge file that has roughly 30304 lines. I need to extract specific info from that file. For example, Box 1 > *aaaaaaaajjjj* > hbbvjvj > jdnnfddllll > *dgdfhfekwjh* Box 2 > *aaaaaaa'aj'jjj* > dse hkjuejef bfdw > dyeee > dsewq > *dgdfhfekwjh* >feweiuei Box 3 > *aaaa"aaaaj"jjj* >... (25 Replies)
Discussion started by: Ernst
25 Replies

6. Shell Programming and Scripting

Assistance needed for pattern matching

Hi, Below are input files:- File1:- 1: ### CREATE TABLE ## 2: SOMETHING ELSE 1 3: ### CREATE TABLE ## 4: ### CREATE <spaces> 5: TABLE ### 6:SOMETHING ELSE 2 7: ## CREATE 8: <spaces> TABLE ### 9: SOMETHING ELSE 3 File2:- Similar Format... Desired 1 Output File:- (3 Replies)
Discussion started by: kishal
3 Replies

7. UNIX for Dummies Questions & Answers

Unable to write to a file within a loop

Hi All, Following is the program that i have written in cygwin. The redirection of the unfound $param1 to error.txt file doesnt work.Can any one help? #!/usr/bin/sh fname=$1 sed 's/ //g' "$fname" > fname1 while read i do echo $i > file1 #param1 is script name ... (1 Reply)
Discussion started by: janardhanamk
1 Replies

8. Shell Programming and Scripting

Removing data with pattern matching

I have the following: HH:MM:SS I want to use either % or # sign to remove :SS can somebody please provide me an example. I know how to do this in awk, but awk is too much overhead for something this simple since I will be doing this in a loop a lot of times. Thanks in advance to all... (2 Replies)
Discussion started by: BeefStu
2 Replies

9. Programming

File Pattern Matching C++

Hi, I have large files with fixed length fields or fields seperated by delimeter. I would like to do validation on some or all fields to check for numeric or date or characters etc.. I would like to write this in C++. Please let me know if any one have any ideas on this. Thanks for all... (2 Replies)
Discussion started by: rameshmelam
2 Replies

10. Shell Programming and Scripting

Pattern matching for file

Hi All, I'm new to perl, My requirement is to check if particular file exists. e.g. filename.txt, filename1.txt, filename2.txt etc I tried the below code:- my $var1 = "filename.txt" if ( -e ($var1 = ~ /file\w/)) { print "File found \n"; } else { print "File not found \n"; } ... (0 Replies)
Discussion started by: doitnow
0 Replies
Login or Register to Ask a Question