Delete a word and complete line


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Delete a word and complete line
# 1  
Old 05-02-2008
Delete a word and complete line

Hi Canone please provide me solution how can achieve the result below:

File1.txt

$
sweet apple£1
scotish
green
$
This is a test1
$
sweet mango£2
asia
yellow
$
This is a test 2
$
sweet apple red (there is no pound symbol here)
germany
green
$
This is a test 3
$


Result required: delete the word "sweet" from lines which has a "£" symbol and everything after £ in that line. And delete exactly two lines below £. Expected result as:

$
apple£
$
This is a test
$
mango£
$
This is test2
$
sweet apple red (there is no pound symbol here)
germany
green
$
This is a test 3
$
# 2  
Old 05-02-2008
Put this code in a file.

File: my-awk
Code:
BEGIN {
    RS="\n\$\n"
    FS="\n"
    ORS="\n$\n"
}


/£/ {
    sub(/sweet /, "", $1)
    sub(/£.+/, "£", $1)
    print $1
}

!/£/ {
    print $0
}

Code:
$ awk -f my-awk your-file-to-process

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find word in a line and output in which line the word occurs / no. of times it occurred

I have a file: file.txt, which contains the following data in it. This is a file, my name is Karl, what is this process, karl is karl junior, file is a test file, file's name is file.txt My name is not Karl, my name is Karl Joey What is your name? Do you know your name and... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

2. UNIX for Dummies Questions & Answers

How to delete a particular word on particular line with sed?

I have a file from which I am trying to delete a particular word on a particular line. NEW NEW /v/engine NEW /ifs/list NEW /ifs/vdrome NEW I am trying to delete the first line only if it contains the word NEW. I am also trying to delete the last line only if it contains the word NEW. I... (11 Replies)
Discussion started by: newbie2010
11 Replies

3. Programming

How can I read complete word from text file?

how can I know that the word in test file end at this point .... I have an Idea to search on ' ' , '\n' or '\0' but don't know what function can store the string before those characters .. help please ! (3 Replies)
Discussion started by: fwrlfo
3 Replies

4. UNIX for Dummies Questions & Answers

delete last word of each line a directory

I want to delete the last word of each line in all the files in one directory but dont know what I am doing wrong FILES="data/*" for X in $FILES do name=$(basename $X) sed s/'\w*$'// $X > no-last/${name} done Can you please help me :wall: (8 Replies)
Discussion started by: A-V
8 Replies

5. UNIX for Dummies Questions & Answers

How to get complete path in variable excluding last word?

Hello Experts, Can you please help me by providing a code which can give me the complete path except last word in a variable, the variable i can use anywhere else for my operation eg: if this below one is my path: ... (3 Replies)
Discussion started by: aks_1902
3 Replies

6. UNIX for Dummies Questions & Answers

Can grep command return word instead of complete line

Hi Is there any way GREP command can return word and not complete line. My file has following data: Hello Everyone I am NitinrajSrivastava Hi Friends Welcome VrajSrivastava I am using grep 'raj' which is returning me complete line.However I want only the word having keyword 'raj'. Required... (11 Replies)
Discussion started by: dashing201
11 Replies

7. Shell Programming and Scripting

SED to delete last word on line...what's wrong?

I have a line that gets pulled from a database that has a variable number of fields, fields can also be of a variable size. Each field has a variable number of spaces between them so there is no 'defined' delimiter. The LastData block is always a single word. What I want to do is delete the... (2 Replies)
Discussion started by: Bashingaway
2 Replies

8. Shell Programming and Scripting

delete line containin specified word

write a shell script that deletes all lines containing a specified word in one or more files supplied as arguments to it.help is appreciated .thank you. (2 Replies)
Discussion started by: shawz
2 Replies

9. Shell Programming and Scripting

cat/delete per line any word "192.168.1.12"

Hi All Can u help me.. My problem is delete word per line sample: cat /tmp/file.txt monitor 192.168.1.11 Copying files in current directory 1 monitor 192.168.1.1 Copying files in current directory 2 monitor 192.168.1.12 Copying files in current directory 3 monitor 192.168.1.14... (1 Reply)
Discussion started by: carnegiex
1 Replies

10. UNIX for Dummies Questions & Answers

Find a word and delete the line

Hi I have a text file like this name today.txt the request has been accepted the scan is successful at following time there are no invalid packages 5169378 : map : Permission Denied the request has been accepted Now what i want do is I want to search the today.txt file and if i... (1 Reply)
Discussion started by: gsusarla
1 Replies
Login or Register to Ask a Question