Search and replace a number


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Search and replace a number
# 1  
Old 04-13-2010
Search and replace a number

Code:
a=`grep  -i a.sh filename.sh|cut -d "|" -f4`
b=`expr $a + 1`
filename=`grep  -i a.sh filename.sh`
while read line
do
echo $line
echo  $filename
if [ $line = $filename ]
then
echo "entered if"
nawk  ' BEGIN { FS="|"; OFS="|" }
{ sub('$a', '$b', $4)
print $0}'  filename.sh
fi
echo "exit if"
done < filename.sh

filename.sh
Code:
User  |id |file|count
--------------------------
oracle|0123|a.sh|0
oracle|0124|b.sh|1
oracle|0125|c.sh|3
oracle|0126|d.sh|0
oracle|0127|e.sh|1
oracle|0128|f.sh|3

expected output
Code:
User |id |file|count
--------------------------
oracle|0123|a.sh|1
oracle|0124|b.sh|1
oracle|0125|c.sh|3
oracle|0126|d.sh|0
oracle|0127|e.sh|1
oracle|0128|f.sh|3


Every time i grep a file name in the file, the counter has to get updated by 1.


I tried this command, if the counter is same for other line, then changes for all the line which are same numbers, i require for single line update, please help me.......

regards,
giri

Last edited by pludi; 04-13-2010 at 03:54 AM.. Reason: code tags, please...
# 2  
Old 04-13-2010
Bumping up posts or double posting is not permitted in these forums.

Please read the rules, which you agreed to when you registered, if you have not already done so.

You may receive an infraction for this. If so, don't worry, just try to follow the rules more carefully. The infraction will expire in the near future

Continue here:

https://www.unix.com/unix-dummies-que...#post302412578

Thank You.

The UNIX and Linux Forums.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nested search in a file and replace the inner search

Hi Team, I am new to unix, please help me in this. I have a file named properties. The content of the file is : ##Mobile props east.url=https://qa.east.corp.com/prop/end west.url=https://qa.west.corp.com/prop/end south.url=https://qa.south.corp.com/prop/end... (2 Replies)
Discussion started by: tolearn
2 Replies

2. Shell Programming and Scripting

Search and replace ---A huge number of files

Hello Friends, I have the below scenario in my current project. Suggest me which tool ( perl,python etc) is best to this scenario. Or should I go for Programming language ( C/Java ).. (1) I will be having a very big file ( information about 200million subscribers will be stored in it ). This... (5 Replies)
Discussion started by: panyam
5 Replies

3. Shell Programming and Scripting

Replace first number of each line in a file with another number

Hi, I have a set of files in a directory that I have to read and replace the first occurrence of a number with another dummy number. This is what I have so far but it does not seem to work. The files have lot of other data in each row and each data element is separated by ,@, for file in... (13 Replies)
Discussion started by: scorpioraghu
13 Replies

4. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

5. Shell Programming and Scripting

highly specific search and replace for a large number of files

hey guys, I have a directory with about 600 files. I need to find a specific word inside a command and replace only that instance of the word in many files. For example, lets say I have a command called 'foo' in many files. One of the input arguments of the 'foo' call is 'bar'. The word 'bar'... (5 Replies)
Discussion started by: ksubrama
5 Replies

6. UNIX for Dummies Questions & Answers

Search and replace a number

a=`grep -i a.sh filename.sh|cut -d "|" -f4` b=`expr $a + 1` filename=`grep -i a.sh filename.sh` while read line do echo $line echo $filename if then echo "entered if" nawk ' BEGIN { FS="|"; OFS="|" } { sub('$a', '$b', $4) print $0}' filename.sh fi echo "exit if" done <... (1 Reply)
Discussion started by: hs.giri
1 Replies

7. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

8. Shell Programming and Scripting

How to search a number in a certain field

Hello, I have a file that looks like this: num11 num12 num13 word1 num21 num22 num23 word2 num31 num32 num33 word3 . . . I would like to search for the lines that contain a given number, but I need to narrow the search only to the first field, that means that the number has to... (4 Replies)
Discussion started by: shira
4 Replies

9. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies
Login or Register to Ask a Question