Sponsored Content
Top Forums Shell Programming and Scripting help needed .. Unable to write the data to new file after matching the pattern Post 302272684 by prashant_jsw on Wednesday 31st of December 2008 10:32:52 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
escape(1)							Mail Avenger 0.8.3							 escape(1)

NAME
escape - escape shell special characters in a string SYNOPSIS
escape string DESCRIPTION
escape prepends a "" character to all shell special characters in string, making it safe to compose a shell command with the result. EXAMPLES
The following is a contrived example showing how one can unintentionally end up executing the contents of a string: $ var='; echo gotcha!' $ eval echo hi $var hi gotcha! $ Using escape, one can avoid executing the contents of $var: $ eval echo hi `escape "$var"` hi ; echo gotcha! $ A less contrived example is passing arguments to Mail Avenger bodytest commands containing possibly unsafe environment variables. For example, you might write a hypothetical reject_bcc script to reject mail not explicitly addressed to the recipient: #!/bin/sh formail -x to -x cc -x resent-to -x resent-cc | fgrep "$1" > /dev/null && exit 0 echo "<$1>.. address does not accept blind carbon copies" exit 100 To invoke this script, passing it the recipient address as an argument, you would need to put the following in your Mail Avenger rcpt script: bodytest reject_bcc `escape "$RECIPIENT"` SEE ALSO
avenger(1), The Mail Avenger home page: <http://www.mailavenger.org/>. BUGS
escape is designed for the Bourne shell, which is what Mail Avenger scripts use. escape might or might not work with other shells. AUTHOR
David Mazieres Mail Avenger 0.8.3 2012-04-05 escape(1)
All times are GMT -4. The time now is 05:46 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy