How to delete last occurence of word using Linux command?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to delete last occurence of word using Linux command?
# 8  
Old 02-24-2013
Here is an awk program to remove last occurrence of pattern and from a file:
Code:
awk ' {
        a[++i]=$0
        l = i;
} END {
        for(j=l; j>=1; j--)
        {
                if(a[j] ~ /and/ && !f)
                {
                        sub("and",x,a[j]); f=1;
                }
        }
        for(i=1; i<=l; i++)
                print a[i];
}' filename


Last edited by Yoda; 02-24-2013 at 03:46 PM.. Reason: removed printing index
This User Gave Thanks to Yoda For This Post:
# 9  
Old 02-24-2013
Sorry, then I'm out of ideas.
Code:
$ t="on a.id1 = b.id1 and
on a.id2 = b.id2 and
on a.id3 = b.id3 and"

$ echo "$t"
on a.id1 = b.id1 and
on a.id2 = b.id2 and
on a.id3 = b.id3 and

$ echo "$t"|sed '$ s:and::'
on a.id1 = b.id1 and
on a.id2 = b.id2 and
on a.id3 = b.id3

This User Gave Thanks to RudiC For This Post:
# 10  
Old 02-24-2013
somehow I am not getting expected from this code also

Here is an awk program to remove last occurrence of pattern and from a file:

Code:
awk ' { a[++i]=$0 l = i; } END { for(j=l; j>=1; j--) { if(a[j] ~ /and/ && !f) { sub("and",x,a[j]); f=1; } } for(i=1; i<=l; i++) print a[i]; }' filename
# 11  
Old 02-24-2013
Works for me! Let us know what results you are getting or errors if any.

I have a file with trailing blank line:
Code:
$ cat filename
on a.id1 = b.id1 and
on a.id2 = b.id2 and
on a.id3 = b.id3 and

Here is what I get when I run the code:
Code:
$ awk ' {
>         a[++i]=$0
>         l = i;
> } END {
>         for(j=l; j>=1; j--)
>         {
>                 if(a[j] ~ /and/ && !f)
>                 {
>                         sub("and",x,a[j]); f=1;
>                 }
>         }
>         for (i=1;i<=l;i++)
>                 print a[i];
> }' filename
on a.id1 = b.id1 and
on a.id2 = b.id2 and
on a.id3 = b.id3

# 12  
Old 02-24-2013
Why do you need it one line?

Here is what you have done wrong.
Code:
awk ' {a[++i]=$0; l=i } END { for(j=l; j>=1; j--) { if(a[j] ~ /and/ && !f) { sub("and",x,a[j]); f=1; } } for(i=1; i<=l; i++) print a[i]; }'

You miss the ; in red
These 2 Users Gave Thanks to Jotne For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command to delete a word in all files

can anyone tell me what is the commands to delete the below particular word in the all files located in one particular file path files/ll>grep "/ftp/" test.kell ftp -m uskmc -d /ftp/ -i filename.zip output should be : ftp -m uskmc -d -i filename.zip (4 Replies)
Discussion started by: ramkumar15
4 Replies

2. Shell Programming and Scripting

How to find the number of occurence of particular word from a text file?

example: i have the following text file... i am very tired. i am busy i am hungry i have to find the number of occurence of a particular word 'am' from the text file.. can any one give the shell script for it (34 Replies)
Discussion started by: sheela
34 Replies

3. Shell Programming and Scripting

Delete all files if another files in the same directory has a matching occurence of a specific word

Hello, I have several files in a specific directory. A specific string in one file can occur in another files. If this string is in other files. Then all the files in which this string occured should be deleted and only 1 file should remain with the string. Example. file1 ShortName "Blue... (2 Replies)
Discussion started by: premier_de
2 Replies

4. Shell Programming and Scripting

Perl : Search for next occurence of a word in an array

I have an array as follows: Space: ABC Name: def Age: 22 Type: new Name: fgh Age: 34 Type: old Space: XYZ Name: pqr Age: 44 Type: new : : How can I separate the array with elements starting from Space:ABC until Space: XYZ & put them in a different array & so on... (4 Replies)
Discussion started by: deo_kaustubh
4 Replies

5. UNIX for Dummies Questions & Answers

counting the occurence of a word

In a file I have to count a particular word. like i need apache how many times. I tried this $ tr "\011" "\012\012"<foo1 | tr -cd "" |sort\uniq -c but I got result like this 32 apache 18 dns 12 doctor Please sugest me (4 Replies)
Discussion started by: pranabrana
4 Replies

6. Shell Programming and Scripting

finding the number of occurence of a word in a line

suppose i have this line abs|der|gt|dftnrk|dtre i want to count the number of "|" in this line.. how can i do that. plz help:confused: (9 Replies)
Discussion started by: priyanka3006
9 Replies

7. UNIX for Dummies Questions & Answers

search& count for the occurence of a word

Greetings, I need to search and count all the occurences of a word in all the files in a directory. Any suggestions greatly appreciated. Thanks (1 Reply)
Discussion started by: skoppana
1 Replies

8. Shell Programming and Scripting

Delete all occurence of a word in one shot

i have a file called file1 cat file1 i am namish namish lives in India India and namish both are good. I want to delete all the occurences of namish in one shot,if i do it with sed i guess all the lines will be deleted containing the pattern.Suggest me any idea without AWK. Thanks... (6 Replies)
Discussion started by: namishtiwari
6 Replies

9. Shell Programming and Scripting

Count the number of occurence of perticular word from file

I want to count the number of occurence of perticular word from one text file. Please tell me "less" command is work in ksh or not. If it is not working then instead of that which command will work. :confused: (40 Replies)
Discussion started by: rinku
40 Replies
Login or Register to Ask a Question