Search for a string and replace the searched string in the same position in samefile


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Search for a string and replace the searched string in the same position in samefile
# 22  
Old 03-13-2009
Quote:
Originally Posted by ganesh_248
sorry ,i still don get what is CLI in
"colsA is an array with values passed via cols from CLI."
the variable 'cols' specified on CLI contains the list of the columns/fields to be processed by the script. In the 'BEGIN' block of the script, we ''split' a list 'cols' into an array 'colsA' for us to iterate through later on.
# 23  
Old 03-13-2009
MySQL

Quote:
Originally Posted by vgersh99
you have SIX fields in your file.
Code:
nawk -v OFS='\t' -v cols='1 3 5'  'BEGIN {n=split(cols, colsA, FS)} {for(i=1; i<=n; i++) sub(/^01/,"20",$colsA[i]); print}' $file1

Thanks Alot Vgresh!!!worked fine!!!Thanks for your clarifications and help!
SmilieSmilieSmilie
# 24  
Old 03-20-2009
Quote:
Originally Posted by Whiteboard
Code:
$ cat tab.txt
00000000 0 +00000000.00 2008/12/29 11:06:04 0108/12/29 10:05:37 00521363
 
$ awk '{sub(/^../,"20",$6); print}' OFS="\t" tab.txt
00000000        0       +00000000.00    2008/12/29      11:06:04        2008/12/29      10:05:37        00521363

$ cat samp.txt
00000000        0       +00000000.00    2008/12/29      11:06:04        0108/12/29

$ awk '{sub(/^../,"20",$6); print}' samp.txt
00000000 0 +00000000.00 2008/12/29 11:06:04 2008/12/29

$ awk '{sub(/^../,"20",$6); print}' OFS="\t" samp.txt
00000000        0       +00000000.00    2008/12/29      11:06:04        2008/12/29


Hope you can understand the above code with sample examples.....
Whiteboard your command is working fine if there is no NULL column inbetween.But if there is a NULL value in any of the columns,AWK omits that column.So I am losing one column .How do i get over this.Pls help me.!!!
# 25  
Old 03-20-2009
Quote:
Originally Posted by vgersh99
you have SIX fields in your file.
Code:
nawk -v OFS='\t' -v cols='1 3 5'  'BEGIN {n=split(cols, colsA, FS)} {for(i=1; i<=n; i++) sub(/^01/,"20",$colsA[i]); print}' $file1

Vgresh your command is working fine if i don have any NULL columns in the file.The Nawk command is removing the NULL values.Even Awk does the same thing.I am losing Null value columns due to this.

My sample data is

004668400000060000 00400 +00000000.00 003 00000000 0 +00000000.00 0109/2/22 14:53:00 0109/2/22 11:53:00 00000000 0000 000000 0000000000000 0109/2/22 14:53:00

It is a tab delimited file.The date columns "2009/2/22 14:53:00" is a single column even though it has a space in between.There are some NULL values like "0000 000000" .I want to retain the NULL values and also the date columns pattern as such .In the date columns i need to replace "01" with"20".Please help.
# 26  
Old 03-20-2009
Quote:
Originally Posted by ganesh_248
Vgresh your command is working fine if i don have any NULL columns in the file.The Nawk command is removing the NULL values.Even Awk does the same thing.I am losing Null value columns due to this.

My sample data is

004668400000060000 00400 +00000000.00 003 00000000 0 +00000000.00 0109/2/22 14:53:00 0109/2/22 11:53:00 00000000 0000 000000 0000000000000 0109/2/22 14:53:00

It is a tab delimited file.The date columns "2009/2/22 14:53:00" is a single column even though it has a space in between.There are some NULL values like "0000 000000" .I want to retain the NULL values and also the date columns pattern as such .In the date columns i need to replace "01" with"20".Please help.
In the above post NULL values is not displayed in the sample data given but there are two columns with NULL values between 0000 and 000000.
# 27  
Old 03-20-2009
try this:
Code:
nawk -v cols='1 3 5'  'BEGIN {FS=OFS="\t"; n=split(cols, colsA, FS)} {for(i=1; i<=n; i++) sub(/^01/,"20",$colsA[i]); print}' $file1

# 28  
Old 03-23-2009
Tools

Thanks Vgresh! I actually found out myself and used FS=OFS="\t" .
Thanks once again Vgresh!SmilieSmilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search partial string in a file and replace the string - UNIX

I have the below string which i need to compare with a file and replace this string in the file which matches closely. Can anyone help me on this. string(Scenario 1)- user::r--,user::ourfrd:r-- String(Scenario 2)- user::r-- File **** # file: /local/Desktop/myfile # owner: me # group:... (6 Replies)
Discussion started by: sarathy_a35
6 Replies

2. UNIX for Dummies Questions & Answers

Search for a string,delete the line and replace with new string in a file

Hi Everyone, I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file. I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
10 Replies

3. Shell Programming and Scripting

Search for a string at a particular position and replace with blank based on position

Hi, I have a file with multiple lines(fixed width dat file). I want to search for '02' in the positions 45-46 and if available, in that lines, I need to replace value in position 359 with blank. As I am new to unix, I am not able to figure out how to do this. Can you please help me to achieve... (9 Replies)
Discussion started by: Pradhikshan
9 Replies

4. Shell Programming and Scripting

Search String and extract few lines under the searched string

Need Assistance in shell programming... I have a huge file which has multiple stations and i wanted to search particular station and extract few lines from it and the rest is not needed Bold letters are the stations . The whole file has multiple stations . Below example i wanted to search... (4 Replies)
Discussion started by: ajayram_arya
4 Replies

5. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

6. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

7. UNIX for Dummies Questions & Answers

Search a string in the file and then replace another string after that position

Hi I am looking for a particular string in a file.If the string exists, then I want to replace another string with some other text.Once replaced, search for the same text after that character position in the file. :wall: E.g: Actual File content: Hello Name: Nitin Raj Welcome to Unix... (4 Replies)
Discussion started by: dashing201
4 Replies

8. Shell Programming and Scripting

Search, replace string in file1 with string from (lookup table) file2?

Hello: I have another question. Please consider the following two sample, tab-delimited files: File_1: Abf1 YKL112w Abf1 YAL054c Abf1 YGL234w Ace2 YKL150w Ace2 YNL328c Cup9 YDR441c Cup9 YDR442w Cup9 YEL040w ... File 2: ... ABF1 YKL112W ACE2 YLR131C (9 Replies)
Discussion started by: gstuart
9 Replies

9. Shell Programming and Scripting

Search for a string and replace the searched string in the same position

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found..The issue is the last... (15 Replies)
Discussion started by: ganesh_248
15 Replies

10. Shell Programming and Scripting

Find the position of a string and replace with another string

Hi, I have a file named "Test_2008_01_21" The file contains a string "manual" that occurs many times in the file How can i find the positions of the string "manual" in the file Ex: if the string " manual " occurs three times in the file. i want to replace the second occurance of string... (6 Replies)
Discussion started by: bab123
6 Replies
Login or Register to Ask a Question