Sed on first instance only


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed on first instance only
# 1  
Old 02-17-2009
Sed on first instance only

Hi, I've been trying to figure this one out and found a post about this on the forum here but the solution didn't seem to work for me. Basically what I have is a file that looks something like:

stuff
morestuff
0
otherthing
0
etc

I want to substitute for the 0 but what I want to substitute will be different for each one. Any ideas how to substitute 1 at a time with sed? Keep in mind that 0 is just the value of the variable I'll be using. I've been trying something like this with no luck...

sed s/$var/$value/1 file
# 2  
Old 02-17-2009
Can you post a sample input as well as output....
# 3  
Old 02-17-2009
Sure, so here would be my text file before. Let's just say I have a file of serial numbers:

E1111111
E2222222
E3333333
0
E5555555
0
E7777777

So the 0 are mess ups in the file and I want to replace each with different serial numbers E4444444 and E6666666 respectively so the output would be:


E1111111
E2222222
E3333333
E4444444
E5555555
E6666666
E7777777

Unfortunately when I use sed it does the whole file so the second 0 gets replaced by E4444444 before I can even try to substitute the E6666666
# 4  
Old 02-17-2009
To replace the first instance in your file you can use this:

Code:
awk 'c && /^0$/{$0=r;c--}1' c=1 r="E4444444" file > newfile

To replace 2 values:

Code:
awk 'BEGIN{a[2]="E4444444";a[1]="E6666666"}
i && /^0$/{$0=a[i--]}1' i=2 file > newfile

Regards
# 5  
Old 02-17-2009
is that just a hypothetical sample OR is that the real math you want to perform to derive ANY missing rows from the previous/non-missing one?
# 6  
Old 02-17-2009
sed might not be the best tool here. However, GNU sed might work for you. Put all your substitutions, one line each, in "file2". And run sed on file1. Note, the newline in the middle of the sed script is necessary:
Code:
sed '/^0${ R file2
;d;}' file1

# 7  
Old 02-17-2009
Quote:
Originally Posted by otheus
sed might not be the best tool here. However, GNU sed might work for you. Put all your substitutions, one line each, in "file2". And run sed on file1. Note, the newline in the middle of the sed script is necessary:
Code:
sed '/^0${ R file2
;d;}' file1

I have a gut feeling that the OP cannot predict what rows will be 'missing'. So gut is telling me the OP wants to derive the value of the missing row from the previous row value. In which case we'll have to know if the sample file is representative of the 'real life' data and what (if any) assumptions can be made regarding the data itself.

Once again, this is all guesses on my part....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed to grab first instance of a range

Hi all, I'm new to the forum and also relatively new to sed and other such wonderfully epic tools. I'm attempting to grab a section of text between two words, but it seems to match all instances of the range instead of stopping at just the first. This occurs when I use: sed -n... (7 Replies)
Discussion started by: Lazarix
7 Replies

2. Shell Programming and Scripting

sed for first instance of a pattern

Hi Everyone, I have the below information from a log file: LOAD SUMMARY ============ WRT_8036 Target: TGT_1_TAB (Instance Name: ) WRT_8039 Inserted rows - Requested: 3929 Applied: 0 Rejected: 3929 Affected: 0 Mutated from update: 3929 WRT_8041 Updated rows ... (7 Replies)
Discussion started by: galaxy_rocky
7 Replies

3. Shell Programming and Scripting

sed command to print first instance of pattern in range

The following text is in testFile.txt: one 5 two 10 three 15 four 20 five 25 six 10 seven 35 eight 10 nine 45 ten 50 I'd like to use sed to print the first occurance of search pattern /10/ in a given range. This command is to be run against large log files, so to optimize efficiency,... (9 Replies)
Discussion started by: uschaafm
9 Replies

4. Shell Programming and Scripting

Using sed can you specify the last instance of a character on a line?

I was told a way to do this with awk earlier today but is there a way with sed to specify the last instance of a character on a line? You will know what character you're looking for but there could be none or one hundred instances of it on a line say and you ONLY want to specify the last one for... (3 Replies)
Discussion started by: Bashingaway
3 Replies

5. Shell Programming and Scripting

sed: how to limit pattern search to first instance only

I need to reduce a file's size below 50MB by deleting chucks of text. The following sed does this. sed '/^begpattern/,/endpattern/d' myfile However, it's possible that the file size can get below 50MB by just deleting the first instance of the pattern. How do I code that into sed? Or can awk... (8 Replies)
Discussion started by: mariod1049
8 Replies

6. Shell Programming and Scripting

sed appending needed only after first instance

Hi, Here is my piece of code used with sed in shell script: sed -i '/<falsemodule-option>/ a\<LdapLogin>' myxmlfile The problem that i am facing with the above is that in 'myxml' file i have mulitple instances of <falsemodule-option> so when i execute the above sed command, it is appending... (10 Replies)
Discussion started by: sunrexstar
10 Replies

7. Shell Programming and Scripting

sed/awk: Delete matching words leaving only the first instance

I have an input text that looks like this (comes already sorted): on Caturday 22 at 10:15, some event on Caturday 22 at 10:15, some other event on Caturday 22 at 21:30, even more events on Funday 23 at 11:00, yet another event I need to delete all the matching words between the lines, from... (2 Replies)
Discussion started by: GrinningArmor
2 Replies

8. Shell Programming and Scripting

sed command - substitue first instance

hi i have one file where i want to substitute only first instance of swap with swap1 i want to replcae only first instance of swap in my script i know we can do this with awk. but i need to do this with sed only i tried follwoing code sed 's/swap/swap1' filename but here all... (15 Replies)
Discussion started by: d_swapneel14
15 Replies

9. Shell Programming and Scripting

sed replace 2nd instance

Hello, I want to replace 2nd instance of "foo" in a file use sed. Any suggestions? (2 Replies)
Discussion started by: katrvu
2 Replies

10. Shell Programming and Scripting

replace first instance(not first instance in line)

Alright, I think I know what I am doing with sed(which probably means I don't). But I cant figure out how to replace just the first occurance of a string. I have tried sed, ed, and grep but can't seem to figure it out. If you have any suggestions I am open to anything! (3 Replies)
Discussion started by: IronHorse7
3 Replies
Login or Register to Ask a Question