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


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to delete a particular word on particular line with sed?
# 8  
Old 09-25-2013
Hi RudiC...

I think we are at cross purposes here.

I know all that. I was pointing out that when "sed" was used to place the file contents
into the variable "text"...
Code:
AMIGA:barrywalker~> text=$(sed '{1d;$d;}' < /tmp/txt)

...that the LAST newline was removed and to be aware of it. I always use "echo -n..."
as a double check to see if the trailing newline exists or not after such an event.

I am pretty sure some time ago you corrected me on "cat" stripping trailing newlines
when being placed into a variable and since then I check every command that does a
similar thing; "sed' seems no exception so far, unless I am missing something...
# 9  
Old 09-25-2013
Sorry, I didn't mean to tease you.
But, I'm sure the "last" newline is NOT removed by sed. What it does is it removes the ENTIRE last line - including THAT newline - but nothing else. So, the before last line - including its newline - is left intact.
# 10  
Old 09-25-2013
Quote:
Originally Posted by wisecracker
I was pointing out that when "sed" was used to place the file contents
into the variable "text"...
Code:
AMIGA:barrywalker~> text=$(sed '{1d;$d;}' < /tmp/txt)

...that the LAST newline was removed and to be aware of it. I always use "echo -n..."
as a double check to see if the trailing newline exists or not after such an event.
That's a pointless habit. There is no need to check. Rest assured, any trailing newlines will be stripped by command substitution. Always.

Note that the newlines are not discarded by the command (sed, cat, or what have you), but by the shell itself after it receives the output from the command. It not only strips a single trailing newline; it will strip as many trailing newlines as may be present.

POSIX - Command Substitution - Section 2.6.3:
Quote:
The shell shall expand the command substitution by executing command in a subshell environment (see Shell Execution Environment) and replacing the command substitution (the text of command plus the enclosing "$()" or backquotes) with the standard output of the command, removing sequences of one or more <newline>s at the end of the substitution. Embedded <newline>s before the end of the output shall not be removed; however, they may be treated as field delimiters and eliminated during field splitting, depending on the value of IFS and quoting that is in effect.
Regards,
Alister
This User Gave Thanks to alister For This Post:
# 11  
Old 09-25-2013
Hi alister...

So I am not going insane after all... ;o)

Now I know why and how I will take heed.

Thanks.
# 12  
Old 09-26-2013
Thanks to All!

Thanks to all, especially Rudi C. I was able to use the command
Code:
 sed '/NEW/ {1d;$d;}'

successfully and also save time because when I created the script it actually was removing and replacing the NEW lines more than once so it didn't work on all the files that I was feeding it. These tutorials really helped!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed script to delete the last word after a last pattern match

Hi Guys , I am having a file as stated below File 1 sa0 -- i_core/i_core_apb/i_afe0_controller/U261/A sa0 -- i_core/i_core_apb/i_afe0_controller/U265/Z sa1 -- i_core/i_core_apb/i_afe0_controller/U265/A sa1 -- i_core/i_core_apb/i_afe0_controller/U268/Z sa1 -- ... (7 Replies)
Discussion started by: kshitij
7 Replies

2. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

3. 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

4. UNIX for Dummies Questions & Answers

How to print line starts with specific word and contains specific word using sed?

Hi, I have gone through may posts and dint find exact solution for my requirement. I have file which consists below data and same file have lot of other data. <MAPPING DESCRIPTION ='' ISVALID ='YES' NAME='m_TASK_UPDATE' OBJECTVERSION ='1'> <MAPPING DESCRIPTION ='' ISVALID ='NO'... (11 Replies)
Discussion started by: tmalik79
11 Replies

5. 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

6. Shell Programming and Scripting

Sed or Grep to delete line containing patter plus extra line

I'm new to using sed and grep commands, but have found them extremely useful. However I am having a hard time figuring this one out: Delete every line containing the word CEN and the next line as well. ie. test.txt blue 324 CEN green red blue 324 CEN green red blue to produce:... (2 Replies)
Discussion started by: rocketman88
2 Replies

7. 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

8. UNIX for Advanced & Expert Users

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 (1 Reply)
Discussion started by: Aejaz
1 Replies

9. 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

10. Shell Programming and Scripting

Sed help to delete everything prior to WORD

How would I delete everything on a line in a file prior to a specific word? In other words, I have a file that contains the word SEARCH on various lines and would like to delete everything prior to SEARCH on all lines. Thanks for that help (2 Replies)
Discussion started by: drheams
2 Replies
Login or Register to Ask a Question