LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-22-2008, 05:20 AM   #1
Frayed
LQ Newbie
 
Registered: Apr 2008
Posts: 2

Rep: Reputation: 0
sed newbie question - strip leading character only if 0


I have a text file with several lines of 2 space delimited numbers.
e.g.
0123 98765
0456 67986
2345 76543

I need to remove the leading character from the first number, but only if it is a 0. Otherwise, leave it untouched.
I'm sure there's a simple sed syntax for this, but I'm brand new to it.

Thanks.
 
Old 05-22-2008, 05:49 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Code:
sed 's/^0\(.*\)/\1/' /path/to/text.file
 
Old 05-22-2008, 06:38 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,314

Rep: Reputation: 1032Reputation: 1032Reputation: 1032Reputation: 1032Reputation: 1032Reputation: 1032Reputation: 1032Reputation: 1032
Or maybe just "sed -i s/^0// your_file.txt" - easier to comprehend for a new user.
 
Old 05-22-2008, 07:16 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,506

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
To indent the output (that is padding with blank spaces instead of leading zeros), just substitute zero with a blank space, following the method suggested by syg00:
Code:
sed s/^0/\ /g file.txt
If you want a finer control over the output format, you can try printf or gawk, for example
Code:
while read one two
do
  printf "%4.0f %d\n" $one $two
done < file.txt
or
Code:
gawk '{printf "%4.0f %5d\n",$1,$2}' file.txt
 
Old 05-22-2008, 07:43 AM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Code:
awk 'NF=2 { printf "%-4d %5d\n", $1, $2}' data
123  98765
456  67986
2345 76543

awk 'NF=2 { printf "%4d %5d\n", $1, $2}' data
 123 98765
 456 67986
2345 76543

awk 'NF=2 { printf "%3d %5d\n", $1, $2}' data
123 98765
456 67986
2345 76543
 
Old 05-22-2008, 07:54 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,314

Rep: Reputation: 1032Reputation: 1032Reputation: 1032Reputation: 1032Reputation: 1032Reputation: 1032Reputation: 1032Reputation: 1032
And just in case the @Frayed hasn't figured it out yet, there's always more than one way to do it.
(that just has to unearth a perl contribution)
 
Old 05-22-2008, 10:30 AM   #7
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
Quote:
Originally Posted by syg00 View Post
And just in case the @Frayed hasn't figured it out yet, there's always more than one way to do it.
(that just has to unearth a perl contribution)
Fine, twist my arm:
Code:
 perl -i.bak -pe 's/^0//' filename
That changes the original and saves a backup as filename.bak. I'm not crazy about how the new version looks, but I'm heading out now. I'll see if I come up with a quick but better-looking version over lunch...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HP-UX newline character with sed jhwilliams Other *NIX 8 08-06-2007 05:13 PM
Need to strip words from front of line. sed/awk/grep? joadoor Linux - Software 6 08-28-2006 04:39 AM
Strip leading dot in filepaths (in tar archive) EcceVery Debian 2 04-13-2005 07:47 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
How sed strip /* comments aaronzh Programming 1 06-05-2003 05:16 PM


All times are GMT -5. The time now is 01:45 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration