understanding sed command

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers understanding sed command
# 1  
Old 11-10-2011
understanding sed command

Hi Friends,

I need a small help in understanding the below sed command.
$ cat t4.txt
     1 root       1  58    0  888K  368K sleep    4:06  0.00% init
     1 root       1  58    0  888K  368K sleep    4:06  0.00% init  last

$ sed 's/[ ][^ ]*$//' t4.txt
     1 root       1  58    0  888K  368K sleep    4:06  0.00%
     1 root       1  58    0  888K  368K sleep    4:06  0.00% init

I am unable to understand how "sed 's/[ ][^ ]*$//' " is removing last character of each line.

Can someone please explain me.

Thanks in advance

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by vbe; 11-10-2011 at 09:56 AM..
# 2  
Old 11-10-2011
[ ] Means a space
[^ ]* Anything , but a space 0 o more times.
$ the end of the line
# 3  
Old 11-10-2011
Hi Klashxx,
thanks for the response, I understand what you said above, but together how it delete the last word.
Sorry, I am not that much expert. Can you please let me know how together it deletes the last word
and also how we can delete the 2nd last word.
# 4  
Old 11-11-2011
You're telling sed ( with the first s ) to substitute all strings that matches the first pattern (things at left of the second slash) whith nothing (right of the second slash).

So if you want to delete all the strings at the right of the % , you can use this :

sed  's/[^%]*$//' txt

This User Gave Thanks to Klashxx For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Quick help on Understanding sed Regex

Hi Guys, Could you please kindly explain what exactly the below SED command will do ? I am quite confused and i assumed that, sed 's/*$/ /' 1. It will remove tab and extra spaces .. with single space. The issue is if it is removing tab then it should be Ī right .. please assist.... (3 Replies)
Discussion started by: Nandy
3 Replies

2. Shell Programming and Scripting

Understanding sed

Hi, can some one suggest me,how "sed" is managed to delete the second field here. Any explanation on , how the below code is working would be appreciated. sed 's/^\(*\)::/\1::/' /etc/passwd sed 's/*:/:/2' /etc/passwd (14 Replies)
Discussion started by: panyam
14 Replies

3. Shell Programming and Scripting

help understanding regex with grep & sed

I have the following line of code that works wonders. I just don't completely understand it as I am just starting to learn regex. Can you help me understand exactly what is happening here? find . -type f | grep -v '^\.$' | sed 's!\.\/!!' (4 Replies)
Discussion started by: trogdortheburni
4 Replies

4. UNIX for Dummies Questions & Answers

Understanding nm command output

After running nm command on any object file from out put can we get to know that wheather a symbol is a call to a function or definition of function ? I am searching a class and function definitions inside many .so files. I have 3 files which contain the symbol but I don't know wheather they... (2 Replies)
Discussion started by: yatrik007
2 Replies

5. Solaris

Understanding 'du' command

Hi I have a questions related 2 commands : 'du' and 'ls'. Why is the difference between output of 'du' and 'ls' cmd's ? Command 'du' : ------------------ jakubn@server1 /home/jakubn $ du -s * 4 engine.ksh 1331 scripts 'du -s *' ---> shows block count size on disk (512 Bytes... (5 Replies)
Discussion started by: presul
5 Replies

6. Shell Programming and Scripting

understanding mv command

hi i was moving a file from one directory to another with the following cmmand mv /home/hsghh/dfd/parent/file.txt . while doing so i i accidently mv /home/hsghh/dfd/dfd . although i gave ctrl c and terminate the move command some of the file are missing in the parent directory and... (1 Reply)
Discussion started by: saravanan71184
1 Replies

7. UNIX for Dummies Questions & Answers

Help understanding sed

I am trying to create a basic script that converts an Oracle script into a Sybase script. The only things im changing are Datatypes and the to_char and to_date functions. I am not really 100% sure of the way it works. I have tried running the functions through a loop to replace each word line... (6 Replies)
Discussion started by: Makaer
6 Replies

8. Shell Programming and Scripting

understanding the kill command

Hi Guys, I like to know if i have a process which triggers 10 different child processes. How to identify out of the 11 processes running which is the parent process and what are the child process? And if i kill the parent process will the child process be killed.. if not is there a way to... (2 Replies)
Discussion started by: mac4rfree
2 Replies

9. Shell Programming and Scripting

Help Needed in understanding this command

Hi All, I search the forum for my query, Glad that got solution to it. But i really want to understand how does this command work. sed -e ':a' -e 's/\("*\),\(*"\)/\1~\2/;ta' Basically it is replacing all the comma(,) characters in between quotes with a tilde. Specially what does ':a' ,... (2 Replies)
Discussion started by: DSDexter
2 Replies

10. Shell Programming and Scripting

understanding the sed command

Guys, I am trying to understand the sed command here. adx001 $ a=/clocal/dctrdata/user/dctrdat1/trdroot/recouncil adx001 $ b=`echo $a | sed 's/\//\\\\\//g'` adx001 $ echo $b \/clocal\/dctrdata\/user\/dctrdat1\/trdroot\/recouncil The sed command i took it from the script. Please... (3 Replies)
Discussion started by: mac4rfree
3 Replies
Login or Register to Ask a Question

Featured Tech Videos