The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Pattern matching in Duplicated file and print once Danish Shakil Shell Programming and Scripting 1 08-01-2008 04:55 AM
Pattern matching for file doitnow Shell Programming and Scripting 0 06-20-2008 08:02 AM
Script to find file name for non matching pattern sujoy101 Shell Programming and Scripting 5 03-31-2008 10:10 AM
Reading lines in a file matching a pattern torenji Shell Programming and Scripting 4 10-25-2007 05:15 AM
getting file words as pattern matching arunkumar_mca High Level Programming 5 05-31-2005 04:28 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-31-2008
prashant_jsw prashant_jsw is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 3
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 (permalink)  
Old 12-31-2008
Christoph Spohr Christoph Spohr is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 205
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 (permalink)  
Old 01-02-2009
prashant_jsw prashant_jsw is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 3
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 (permalink)  
Old 01-02-2009
Christoph Spohr Christoph Spohr is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 205
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

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:02 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0