sed confusion

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers sed confusion
# 1  
Old 12-03-2016
sed confusion

Code:
#!/bin/bash
X=(0 2 4 6 7 0 0 0 0)

Let me just say from the start that sed confuses the hell out of me!

In the above line of code how can I use sed to remove all of the 0's except the first one?
I have tried sed -e 's/[0]*$//g' but it removes all of the 0's.

Thank you in advance for any and all suggestions.

Cogiz
# 2  
Old 12-03-2016
Here is one way:
Code:
$ echo '0 2 4 6 7 0 0 0 0' | sed 's/0/\n/g; s/\n/0/; s/\n//g'
0 2 4 6 7

# 3  
Old 12-03-2016
Not sure what you are after. If you want to used sed to act on the bit of shell code you posted as input file:

Try:
Code:
$ echo 'X=(0 2 4 6 7 0 0 0 0)' | sed 's/\( 0\)\{1,\})/)/'
X=(0 2 4 6 7)

or with -E (BSD sed) or -r (GNU sed)
Code:
$ echo 'X=(0 2 4 6 7 0 0 0 0)' | sed -E 's/( 0)+\)/)/'
X=(0 2 4 6 7)



--
Note: using \n in the replacement part of a substitution (s-command) is a GNU sed only extension.

Last edited by Scrutinizer; 12-03-2016 at 09:17 PM..
# 4  
Old 12-04-2016
Remove all 0's with a leading space
Code:
sed 's/ 0//g'

This User Gave Thanks to MadeInGermany 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

sed multilines + separator confusion !!

Hi Seders, i am new to this forum, but i think it's quite the best place to post. So, here is my pb : I have a csv file, with comma separator and text enclosed by ". First pb is with text in " ......... ", wich sometimes includes lines break, and commas And to complicate a little more,... (4 Replies)
Discussion started by: yogeek
4 Replies

2. UNIX for Dummies Questions & Answers

Confusion with the concept of wc -c and wc -m

Is wc -c and wc -m same ? Shellscript::cat file1 hello Shellscript::cat file1 | wc -c 6 Shellscript::cat file1 | wc -m 6 Shellscript::file file1 file1: ASCII text Shellscript::uname -a Linux was85host 2.6.27.45-0.1-vmi #1 SMP 2010-02-22 16:49:47 +0100 i686 i686 i386 GNU/LinuxAtleast... (5 Replies)
Discussion started by: shellscripting
5 Replies

3. Shell Programming and Scripting

Confusion with PS

Hello All, I have a problem in counting number of process getting run with my current script name.. Here it is ps -ef | grep $0 | grep -v grep This display just one line with the PID, PPID and other details when i print it in the script. But when I want to count the numbers in my... (11 Replies)
Discussion started by: sathyaonnuix
11 Replies

4. Homework & Coursework Questions

Server Confusion

I don't even know where to start with this one. There is so much out there about different aspects of this. I am starting with a basic Ubuntu 11.04 install. Do I need to configure a DNS? I am a little confused about that. What do I need to do for a domain name? I have followed various tutorials,... (1 Reply)
Discussion started by: polyglot0727
1 Replies

5. UNIX for Dummies Questions & Answers

crontab confusion

I come across an entry in cron which is in such: 0 * * * * What is the first 0 indicating? 0 minute? meaning a script cron as such will run every minute? :confused: (2 Replies)
Discussion started by: user50210
2 Replies

6. UNIX for Dummies Questions & Answers

'tr' confusion

Good day, everyone! Could anybody explain me the following situation. If I'm running similar script: Var="anna.kurnikova" Var2="Anna Kurn" echo $Var | tr -t "$Var" "$Var2" Why the output is : anna KurniKova instead of Anna Kurnikova? :confused: Thank you in advance for any... (2 Replies)
Discussion started by: Nafanja
2 Replies

7. Shell Programming and Scripting

Sed confusion

Hello all, I am trying to delete all the lines in a particular file having a pattern. The problem is that it has special characters and for some reason is not doing the job. For eg. src_file /home/test/filelist.txt :xxxx:ogog /home/test/RCH/ogogogg /home/test/RYHUJ/HHHH... (3 Replies)
Discussion started by: alfredo123
3 Replies

8. Shell Programming and Scripting

confusion with export

Hi, I have written the following two scripts. a.ksh ---> FPATH=/users/kushard autoload b b echo "From a.ksh::" $aa b ---> function b { typeset aa aa="TRUE." echo "From b::" $aa export aa } (1 Reply)
Discussion started by: kdipankar
1 Replies

9. UNIX for Dummies Questions & Answers

Please help me clear up some confusion

Hello All, I like this forum btw, and have only been lurking for about a day. Recently I purchased some new hardware (AMD Athlon 64 3200+ and a Asus K8V Deluxe Motherboard), and I want to find an OS that can take advantage of the 64 bit processor. Basically, what are the differences... (2 Replies)
Discussion started by: RoY_mUnSoN
2 Replies
Login or Register to Ask a Question